0

Okay, I'm adding a value to each value in the array and I want to replace the sum of the two values for each array value with the existing array. If this function was initated again, it would add on top of the "new" array.

var howMuch = 1;

var numbers = [
    700,
    800,
    900,
    1000,
    1100,
    1200,
    1300,
    1400,
    1500,
    1600,
    1700,
    1800,
    1900
  ];

function change (string) {
  if(string === 'success'){
     for(var i = 0; i < numbers.length){
        return numbers[i] + howMuch;
      }
   }
}

change('success');


// I'm expecting the global numbers array to be 

    701,
    801,
    901,
    1001,
    1101,
    1201,
    1301,
    1401,
    1501,
    1601,
    1701,
    1801,
    1901
  ];

and if ran again: 

702,
802,
902,
1002,
etc........

THANK YOU IN ADVANCE!!!!
1
  • 2
    the code is mostly fine, although as you are operating on a global variable you don't need to return from the function, just modify the value. Also you're missing the increment i++ from the for loop Commented Aug 22, 2019 at 13:44

2 Answers 2

1
function change (status) {
  if(status === 'success'){
     for(var i = 0; i < numbers.length; i++){
        numbers[i] += howMuch;
      }
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is the best answer, but it would be nice not to use "string" as a parameter name since it is a restricted keyword in javascript
@Apolo Actually, "string" is not a reserved keyword, but it is still a bad name. It is better to give the variable a name that describes what it does instead of what type it is.
0

i would suggest map function together with an arrow callback function.

    function change (word) {
       if(word === 'success'){
          numbers=numbers.map(item=>item+howMuch)
       }
     }

Comments

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.