Having some difficulty with this code for my assignment.
I'm supposed to create two functions.
the first function is called
calledInLoop
that will accept one parameter and log the parameter.calledInLoop = function (parameter) { console.log(parameter); }
the second function is called
loopThrough
that will accept an array, loop through each, and invoke thecalledInLoop
function. The result should be each element of the array is console logged.loopThrough = function (array) { for (var i = 0; i < array.length; i++){ calledInLoop(array[i]); }; } myArray = ['dog', 'bird', 'cat', 'gopher'];
console.log(loopThrough(myArray));
returns each element on its own console.log
line but then returns undefined
. Why is this?
The call to
console.log
inconsole.log(loopThrough(myArray));
is only printing outundefined
. It does this becauseloopThrough
does not return anything, so it defaults toundefined
.The elements in the array are printed by a call to
calledInLoop
inloopThrough
, which in turn callsconsole.log
.