I am a JavaScript newbie. I'm trying to practice some sample JavaScript problems. I'm a little stuck when it comes to this question about iterating over arrays. Can anyone point me in the right direction?
I am trying to take the values in oldArray, add 5 to each of them, and store in newArray.
var oldArray = [12, 45, 6, 23, 19, 20, 20, 15, 30, 42];
var newArray = [];
function plusFive(oldArray[i]) {
for (var i = 0; i < oldArray.length; i++) {
newArray.push(oldArray[i]) + 5) };
}
}
oldArrayis not defined. I am assuming that you changedoldArrray[i]in the parameter list tooldArray. This error message would then mean that you are not passing inoldArrayto the function. You need to call it asplusFive(oldArray). Note:oldArrayoutside the function and inside the function are two different things!