I'm a beginner of JavaScript.I learn the course on Udacity. And there is a simple question about Array practice but I can't figure out why. I will be very appreciate if anyone can answer me,thanks!
practice:
Programming Quiz: Another Type of Loop (6-8)
QUIZ REQUIREMENTS
Use the existing
test
variable and write aforEach
loopthat adds 100 to each number that is divisible by 3.
Things to note:
-
- Inside the loop, you must use an
if
statement to verify code is divisible by 3
- Inside the loop, you must use an
-
- Inside the loop, you can update an element ONLY by using the arrayName[index]
-
- Outside the loop, you can use
console.log
to verify thetest
variable */
var test = [12, 929, 11, 3, 199, 1000, 7, 1, 24, 37, 4, 19, 300, 3775, 299, 36, 209, 148, 169, 299, 6, 109, 20, 58, 139, 59, 3, 1, 139 ];
- Outside the loop, you can use
Answer:
test.forEach(function (value,index,array){
if (value % 3 ===0){
array[index] +=100;
}
return value;
});
console.log(test);
My problem is why can't I use value +=100:
test.forEach(function (value,index,array){
if (value % 3 ===0){
value +=100;
}
return value;
});
what's the difference?
Thanks!!!!!!!!1