FAQ: Code Challenges: Intermediate JavaScript - shoutGreetings()

Doesn’t make sense since both ways log (print on the console), and the console.log method itself always returns undefined. In both cases there is a log method hence it’s logs the returned the value in both cases. So both cases are equal.

However if you do write out the following on the console, it prints out the returned value and not undefined :

const shoutGreetings = arr => {
  const newArr = [];
  	arr.map(element => {
  	newArr.push(element.toUpperCase().concat('!'));
  })
  return newArr;
}

const greetings = ['hello', 'hi', 'heya', 'oi', 'hey', 'yo'];
shoutGreetings(greetings)

Another way to go about it to say:

console.log(solution.toString());

doesn’t, itself, return a value (it outputs a value, but doesn’t return one).

Please correct me if I’m missing something here.

in the first example:

const shoutGreetings = arr => {
  const newArr = [];
  	arr.map(element => {
  	newArr.push(element.toUpperCase().concat('!'));
  })
  return newArr;
}

const greetings = ['hello', 'hi', 'heya', 'oi', 'hey', 'yo'];
console.log(shoutGreetings(greetings))

your shoutGreetings returns an array, in the second example:

return console.log(newArr);

where you use .log(), now your shoutGreetings returns undefined. No way they are the same.

logging to the console is a dead-end, now we can no longer use use the variable later on

your shoutGreetings returns an array, in the second example:
return console.log(newArr);

where you use .log() , now your shoutGreetings returns undefined. No way they are the same.
logging to the console is a dead-end, now we can no longer use use the variable later on

Agree 100%.
However, you’d think that if we only wanted to return (and nothing more), we’d use:

shoutGreetings(greetings)

But we are not, since we are logging (console.log method) the value that is returned.

So let’s agree that even though both examples log out the value, only the first one returns AND logs the value?!

I think I have solved this challenge but the error message keeps saying, “Does your function return an array.”

Can someone help me with this? Why is it saying that I need an array?

because that is the assignment?

.log() will log to the console, but has a void signature. So no return value. so now your function returns undefined.

also, you will need to construct a new array, not a string (don’t use .join)

I didn’t understand how my code was a string vs. an array. I was able to solve the problem with your help. Thank you!

For anyone that gets stuck on this task. I think I have found a good solution to it:

const shoutGreetings = arr => arr.map(e => e.toUpperCase() + ‘!’);

Over here, we are declaring a function and then creating a parameter for it that takes in an array (arr). In this case it takes in greetings. The we take that parameter and manipulate it. Frist we have to declare a variable for it (e). And we capitalise it (to Uppercase). We capitalise every string (.map) .map is better to use than a for loop in this case. We then add the exclamation mark at the end (’!).