1

So basically, I'm just trying to add 100 to the elements of the array test that are divisible by 3 and print out the test array. What I don't understand is that when I use console.log(test) in the function, it prints out the array with the condition met ie 100 is added to the elements that are divisible by 3, but when I use it outside of the function, it doesn't. Why is that? And is there a way to print out the test array with the condition met outside the function? I just started learning javascript, so I'm kinda a beginner.

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
];

test.forEach(function(test){
    if(test % 3 === 0){
        test+=100;
    }
    console.log(test);//prints out test array if condition met
});

//console.log(test); //doesn't print test array with if condition met????
Blue Eagle
  • 55
  • 8
  • You need to store back the updated value to your array, which you are not doing. For your case, you can use `array#map`. – Hassan Imam Dec 29 '17 at 03:54
  • 3
    `forEach` doesn’t change the array, and returning a value from the function you pass to `forEach` doesn’t do anything. – Ry- Dec 29 '17 at 03:54
  • Your variable array test and elements are also named test. It would be a good idea to call the array tests with an s – random-forest-cat Dec 29 '17 at 03:55
  • lfender6445 that's a good point, but it doesn't really solve my problem :/. – Blue Eagle Dec 29 '17 at 04:03
  • Ryan okay, but how can I return the value in the function to use it outside of it? – Blue Eagle Dec 29 '17 at 04:05
  • Hassan Imam `array#map` ?? – Blue Eagle Dec 29 '17 at 04:06
  • 1
    @Phil That won't work, OP wants the whole `test` array adding 100 to the numbers divisible by 3. – Gerardo Furtado Dec 29 '17 at 04:11
  • @GerardoFurtado right you are. I misread the intent of the code – Phil Dec 29 '17 at 04:15
  • 1
    @BlueEagle please read the original question I linked, it shows how to use `forEach` to change the array in place. If you want to use `map`, it will return a new array. Here is a very verbose code, for you to see how to do it: https://jsfiddle.net/gmzm51hw/ – Gerardo Furtado Dec 29 '17 at 04:19
  • @GerardoFurtado I'm reading them now. I'm actually reading both @Mamun answer and the answer you sent through the link to understand how the `for each` works and the array.map you sent. thanks – Blue Eagle Dec 29 '17 at 04:29

1 Answers1

1

The forEach method does not directly modify the original array, but the callback function might modify it. The callback is passed with the element, the index, and the array itself.

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
];

test.forEach(function(item, index, test){
    if(item % 3 === 0){
        test[index] = item+=100;
    }
});

console.log(test);
Mamun
  • 66,969
  • 9
  • 47
  • 59
  • It works, but does the function's parameters has to be in a specific order?because when I did this `function(test,item,index)`, the code didn't work. – Blue Eagle Dec 29 '17 at 04:42
  • @BlueEagle, yes of course, all the parameters has to be in specific order. First one indicates the `current item` of the `iteration`, second one is for `index` and the third one is for the `original array`. – Mamun Dec 29 '17 at 04:47