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!!!!
-
2the 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 loopchrismclarke– chrismclarke2019年08月22日 13:44:08 +00:00Commented Aug 22, 2019 at 13:44
2 Answers 2
function change (status) {
if(status === 'success'){
for(var i = 0; i < numbers.length; i++){
numbers[i] += howMuch;
}
}
}
answered Aug 22, 2019 at 13:43
chrismclarke
2,11513 silver badges17 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Apolo
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
some
@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.
i would suggest map function together with an arrow callback function.
function change (word) {
if(word === 'success'){
numbers=numbers.map(item=>item+howMuch)
}
}
Comments
lang-js