+ 1

What are updated array values not saved to array in for each loop

js https://code.sololearn.com/cBNYT6gTAk2O/?ref=app

22nd May 2021, 6:00 PM
Edward Jackson Jr
Edward Jackson Jr - avatar
2 Respuestas
+ 2
because the 'element' variable only hold the value of each items, not the array reference ^^ to update the array using forEach method, use other callback arguments: arr.forEach((value,index,array) => { if (value>15) array[index] += 2; });
22nd May 2021, 6:12 PM
visph
visph - avatar
+ 1
however, you could assign to arr a mapping of arr: arr = arr.map( val => val>15 ? val + 2 : val );
22nd May 2021, 6:24 PM
visph
visph - avatar