Ask coding questions

← Back to all posts
JS Assignment 16: Using Callbacks in Array Methods
vkajdacsi (2)

I'm pretty much stuck at one of the last exercises at Lambda Pre-Course, JS assignment 16. Could anyone please give me any hints on what I'm doing wrong here?

RE: excerciseOne - I get the error message 'names' is not defined, although the array has been given. Also, putting the same code i wrote, in a different console (with the addition of creating an array called names of course) works perfectly! Is there an obvious point I'm missing here?

Please see the instructions and my solution below:

function exerciseOne(names){
// Exercise One: In this exercise you will be given and array called names.
// Using the forEach method and a callback as it's only argument, console log
// each of the names.
console.log(names)
}
names.forEach(exerciseOne);

Thank you very much for any help in advance!

Commentshotnewtop
Tatyanadz23 (1)

names.forEach(function(callback){
console.log(callback);
});
This worked for me. Just write it inside the function

vkajdacsi (2)

@Tatyanadz23 Thank you for all the help, much appreciated :)

JustARatherRidi (189)

If you read the problem description carefully, you'll see that the names argument you get is the array you need to work on, but you're trying to use the names argument outside the function. This doesn't work because the arguments passed into a function can only be used within the function, and not outside it like you've attempted to do on line 7.

Here are a couple of hints to help you out

  • You need to put the names.forEach(...) inside the exerciseOne function because you have to make use of the names array within the function.
  • You'll have to figure out what ... is exactly. If you are a little shaky on how forEach works, i suggest you to look up the documentation.

Let me know if i can help more :)

vkajdacsi (2)

@JustARatherRidi Thanks a lot for the hints!

It's pretty mind-blowing that I solved excerciseTwo, but I can't handle this simple one haha. I gave enough hint, yet I still can't see the forest for the trees! Still trying to solve it.

vkajdacsi (2)

@JustARatherRidi I see that now and although it can be frustrating sometimes, I really begin to see the beauty of it! :)

I didn't see that link, but I'm taking a look at it now! Thank you all for the help, much appreciated!

Vandesm14 (505)

Do you have a repl link?

I'm not sure how exactly the program is supposed to produce the expected result. Can you put all of the code (not just the snippet) into a repl and send me the link?

Vandesm14 (505)

@vkajdacsi That is a classroom link, which I am not able to access. You have to create a repl: https://repl.it/languages/nodejs, paste in the code, and send me the https://repl.it/@vkajdacsi/REPL-NAME-HERE link.

Vandesm14 (505)

@vkajdacsi Is there an array called "names" anywhere in your code? In the assignment description or something?

vkajdacsi (2)

@Vandesm14 yeah, description says the array called names was given, that 's why I tried to call the forEach method outside the function. Which obviously won't work, someone pointed it out. I got some other hints too, but to be honest, the more i "duckduckgo" the forEach method, the more confused I get.

Appreciate the help, still working on it.

Vandesm14 (505)

@vkajdacsi For Each works by running the specified function "for each" item in the array. It will also pass the current array item as a parameter to the specified function as well. You can do so like this:

var names = ['John','Kevin','Donna','William','Samantha','Rodney','Hector'];
function exerciseOne(names){
// Exercise One: In this exercise you will be given and array called names.
// Using the forEach method and a callback as it's only argument, console log 
// each of the names.
  console.log(names);
}
names.forEach(exerciseOne);

It should spit out the names like so:

John
Kevin
Donna
William
Samantha
Rodney
Hector

Please mark this as answered if I helped your problem 👍

vkajdacsi (2)

@Vandesm14 Thank you so much for all the help! Just getting back to this now.

MIAlien (0)

@Vandesm14 Hello Everyone , this worked for me:

names.forEach(function(names){
console.log(names);
});
}