2
JS Assignment 16: Using Callbacks in Array Methods
vkajdacsi (1)

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!

You are viewing a single comment. View All
2
Vandesm14 (486)

@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 👍