Mini Linter project, working but printing strange 'undefined'

I am currently doing Step 4 of the Mini Linter project (https://www.codecademy.com/paths/create-video-games-with-phaser/tracks/game-dev-learn-javascript-higher-order-functions-and-iterators/modules/game-dev-learn-javascript-iterators/projects/mini-linter).

In that step you are asked to iterate though an array of many strings, compare it to another array of a few strings and return how many times each of those strings are found in the longer array.

I have gone about it in two ways:

//arr is the long strings array and overusedWords is the array to cross check for.
const overusedCount = arr =>
{
    let wordCount = 0;
    for (let j = 0; j < arr.length; j++)
    {
      if (arr[j] === overusedWords[i])
      {
        wordCount++;
      }
    }
    console.log(`You have used the word "${overusedWords[i]}"`, wordCount, "times.");
  }
};

And

const overusedCount = arr =>
{
  for (let i = 0; i < overusedWords.length; i++)
  {
    const overusedTerm = arr.filter(word => word === overusedWords[i]);
    console.log(`You have used the word "${overusedWords[i]}"`, overusedTerm.length, "times.");
  }
};

Both print:

You have used the word "really" 2 times.
You have used the word "very" 5 times.
You have used the word "basically" 1 times.
undefined

I have no idea where this undefined is coming from. Reducing the loop iterations by 1 does not get rid of it. Any ideas?

This is because the function prints something to the console, yet does not return anything. This means, when you print the function output:

console.log(overusedCount(["a", "random", "array"]));

The function is called, so the console.logs in the function print, yet nothing is explicitly returned from the function. When nothing is returned, undefined is automatically returned.

1 Like

Ah that makes sense, thank you. So how do I make it stop printing that undefined? I tried adding return to the end of the functions, to no avail.

Either replace the console.logs with return, or remove the console.log when you call the function.

1 Like

Oh dang, must have done something similar so many times in previous lessons and it didn’t even occur to me to just return the string. Thank you very much!

1 Like

Curiously enough, just removing console.log and returning the string instead, and then console.loging the function, for some reason, only prints “times”.

Just leaving the console.log inside the function and then calling the function works, though.

Consider the following examples:

//this function only returns the last item in the list of comma separated values
const doStuff = () => ("string1", "string2", "string3");
console.log(doStuff());

//this function returns a single value (interpolated string - everything inside the back ticks)
const doOtherStuff = () => `string1 string2 ${doStuff()}`;
console.log(doOtherStuff());

//this function again only returns the last item in the list
const doMoreStuff = () => (`string1`, "string2", doStuff());
console.log(doMoreStuff());

//console.log() will print all values supplied in the argument list
console.log("string1", "string2", doStuff());

Output:

string3
string1 string2 string3
string3
string1 string2 string3

1 Like

Very interesting, thank you.

1 Like