Hi all!
Currently working on this code challenge:
https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/web-dev-intermediate-javascript-practice/lessons/intermediate-javascript-coding-challenge/exercises/shout-greetings
This is what I have:
const shoutGreetings = (greetings) => {
let newArray = [];
for (let i of greetings) {
console.log (`${i}!`.toUpperCase());
}
return newArray;
}
const greetings = ['hello', 'hi', 'heya', 'oi', 'hey', 'yo'];
console.log(shoutGreetings(greetings))
// Should print [ 'HELLO!', 'HI!', 'HEYA!', 'OI!', 'HEY!', 'YO!' ]
My output looks like this:
HELLO!
HI!
HEYA!
OI!
HEY!
YO!
[]
I have an extra so it’s not accepting the answer.
Would someone be able to give me some clarity on this?
Specifically, why is this creating the extra ? And how to solve this?
If I delete the empty array and the return array it prints:
HELLO!
HI!
HEYA!
OI!
HEY!
YO!
undefined
I think it’s the return newArray bit. You create an empty array and return it. Without it, it would still log those items.
2 Likes
I think you’ve fallen into a common pitfall that many learners encounter - confusing sending output to the console and returning data from a function.
Inside of your shoutGreetings()
function, all you’re doing is iterating over the greetings
array you’ve passed in and logging them to the console. You don’t use the newArray
value at all, but you return it at the end of the function.
As a result, when you eventually get to this - console.log(shoutGreetings(greetings))
- your function logs the greetings to the console, because that’s what your for
loop does. However, the outer call to console.log()
also logs to the console the return value of the function, which in your original code is the empty array newArray
.
When you’ve removed the newArray
variable and the return statement, you’re getting undefined
at the end because you’re still logging the return value to the console. In the absence of a defined return value, JavaScript functions return undefined
.
The task asks you to write a function which takes an array as input, and returns a new array with those items capitalized. Whatever you output to the console is irrelevant. You can log the capitalized values to the console to check that your code is working, sure, but it’s not the output which is being asked for. 
2 Likes
Ok that makes sense thanks!
I’ve gotten rid of the console.log and i’ve put
newArray.push(i.toUpperCase())
Which makes it upper case, but im struggling to get the ! on the end
return newArray += ‘!’ just adds a ! at the end of YO!
return newArray[i] +="!" gives me a reference error 
Ive got it!!!
const shoutGreetings = (greetings) => {
let newArray = ;
for (let i of greetings) {
i += '!'
newArray.push(i.toUpperCase())
}
return newArray
}
1 Like
I used .forEach() here and also get an extra array element. Here is the code:
const shoutGreetings = (arr) => {
const shout = arr.forEach(element => console.log(element.toUpperCase() + '!'));
return arr.map(shout)
}
const greetings = ['hello', 'hi', 'heya', 'oi', 'hey', 'yo'];
console.log(shoutGreetings(greetings))
This is my output:
HELLO!
HI!
HEYA!
OI!
HEY!
YO!
/home/ccuser/workspace/intermediate-javascript-coding-challenge-shout-greetings/main.js:6
return arr.map(shout)
^
TypeError: undefined is not a function
at Array.map (native)
at shoutGreetings (/home/ccuser/workspace/intermediate-javascript-coding-challenge-shout-greetings/main.js:6:14)
at Object.<anonymous> (/home/ccuser/workspace/intermediate-javascript-coding-challenge-shout-greetings/main.js:17:13)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
I’ve been getting this extra undefined element with just about all my loops or when I need to create a new array. I’ve read it’s probably because my code is somehow redundant, but I guess I need more specific guidance.
Hi @geojam224
shout
is undefined. With forEach you do not store anything in the variable. Instead you log each element as uppercase to a separate console.
The method map requires a function. It is not obvious to me what exactly your intended output for arr.map(shout) was. Can you specify that?
1 Like
The objective of the task is to create the function shoutGreetings()
which takes an array, and returns a new array with the items from the input capitalised and ending with a !
.
I presume that shout
is meant to be the callback function for map which does the capialisation, but instead it’s just outputting the “shouts” to the console. That’s the bit which needs correcting, as with a functional callback using map
is a great way to solve that one. (It’s the way I did it as a one-liner.)
@geojam224 your issue is with your const shout
line; I’ve also correctly formatted your code. 
2 Likes
I don’t see a difference in the edits. I copy/pasted the new one into the project and I still got the same error
I didn’t change your actual program, I changed the way that it was pasted into the topic so that the forum correctly rendered it. 
You originally used quotes, like this:
this is a quote
but the forum has a specific formatting option for codeblocks ( more info here ) which looks like this:
this is a preformatted code block
the forum will observe whitespace
and maintain your code "as it was written"
1 Like
if I get rid of that line, the error goes away, but I still get this as my output:
HELLO!
HI!
HEYA!
OI!
HEY!
YO!
undefined
The prompt suggests using .map() though. It isn’t passing me based on this output and I’m assuming because of the “undefined” at the end, which I get often. Sometimes it’ll still let me to the next lesson though, this time it won’t.
Oh haha. I used a blockquote instead of the preformat. Thanks 
Yes; the undefined
is coming from this line (presuming that you have removed the return arr.map(shout)
which is the line that @mirja_t was referring to):
console.log(shoutGreetings(greetings))
Your shoutGreetings()
function is meant to return an array of shouted greetings, not simply write them to the console.
Your function doesn’t do that, which is why the test is not passing. 