3

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) };
    }
}
3
  • Please open your console and view the error messages that appear. Commented Nov 17, 2015 at 6:52
  • Hello. this is the error I receive when I input the code gladsocc suggested: TypeError at line NaN: undefined is not an object (evaluating 'oldArray.length') Commented Nov 17, 2015 at 7:36
  • This means that oldArray is not defined. I am assuming that you changed oldArrray[i] in the parameter list to oldArray. This error message would then mean that you are not passing in oldArray to the function. You need to call it as plusFive(oldArray). Note: oldArray outside the function and inside the function are two different things! Commented Nov 17, 2015 at 8:40

2 Answers 2

8

Bug in your code is an additional parenthesis and closing brace in push statement line, just remove them. Also there is no need to set function parameter here since both array are accessible inside the function, if you want to pass then you need to change it to function plusFive(oldArray), and call the function with array as parameter.

newArray.push(oldArray[i] + 5) ;
//-----------------------^----^-

Working snippet :

var newArray = [];

function plusFive(oldArray) {
  for (var i = 0; i < oldArray.length; i++) {
    newArray.push(oldArray[i] + 5)
  };
}


plusFive([1,2,4,6,32,44]);

document.write(
  'New array :' +
  '<pre>' + JSON.stringify(newArray) + '</pre>'
);


Function without array as parameter

var oldArray = [12, 45, 6, 23, 19, 20, 20, 15, 30, 42];

var newArray = [];

function plusFive() {
  for (var i = 0; i < oldArray.length; i++) {
    newArray.push(oldArray[i] + 5)
  };
}


plusFive();

document.write(
  'Old array :' +
  '<pre>' + JSON.stringify(oldArray) + '</pre>' +
  'New array :' +
  '<pre>' + JSON.stringify(newArray) + '</pre>'
);


But it's better to use map() for creating a modified array from an existing array

var oldArray = [12, 45, 6, 23, 19, 20, 20, 15, 30, 42];

var newArray = oldArray.map(function(v) {
  return v + 5;
});

document.write(
  'Old array :' +
  '<pre>' + JSON.stringify(oldArray) + '</pre>' +
  'New array :' +
  '<pre>' + JSON.stringify(newArray) + '</pre>'
);

Sign up to request clarification or add additional context in comments.

4 Comments

While this is much better code, I don't think it really helps the OP understand for loops and iteration.
@gladsocc : you were already answered with the bug in for loop, I have added it as an alternative and better way
Thanks for the answer. Unfortunately, I am quite a newbie, and I haven't learned how to implement map() yet.
-1

Your code is almost right, but you closed parenthesises incorrectly, and you need to name the function argument correctly. For function arguments, you're just giving labels. You can't name a variable something[a], and an argument cannot be named something[a].

Try:

var oldArray = [12, 45, 6, 23, 19, 20, 20, 15, 30, 42];

var newArray = [];

function plusFive(oldArray) {
    for (var i = 0; i < oldArray.length; i++) {
        newArray.push(oldArray[i] + 5)
    }
}

plusFive();

4 Comments

Thank you! However, I tried to run the code in repl.it, and I got this error message: SyntaxError: Unexpected token '['. Expected a ')' or a ',' after a parameter declaration. Might you have any suggestions for a good website that will run raw Javascript codes?
Don't you need to pass oldArray to plusFive when you call it?
Hello. This is the error I receive when using this code: TypeError at line NaN: undefined is not an object (evaluating 'oldArray.length')
You either need to pass it oldArray or remove the argument, the oldArray in the argument is creating a new null (undefined) reference rather than taking the oldArray var that you created. Loosely typed languages like javascript are tough to learn programming on because of things like this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.