+ 1

For...of loop?

Is it possible to use a for...of loop to sum a mathematical value to each element of an array? E.g. say I want to add 5 to each value of array [1,2,3,4,5] to make [6,7,8,9,10]. I can't seem to figure it out.

30th May 2022, 11:48 PM
Daniel
3 Réponses
+ 4
In which language? Most languages have some way to do this and judging from your profile you are primarily learning JavaScript. In that case, you can use a for...of loop, but also Array.map is designed specifically for the example you gave. Here's both: https://code.sololearn.com/cYwdm8SEoQgf/?ref=app If you would like this demonstrated in a different language, let me know.
31st May 2022, 12:05 AM
Daniel C
Daniel C - avatar
0
In Java ----------- int[] arr = {1,2,3,4,5}; for(int i=0;i<arr.length;i++){ arr[i] = arr[i] + 5; } System.out.println(Arrays.toString(arr));//[6, 7, 8, 9, 10] link => https://code.sololearn.com/cNZ6CrrA16Sc
31st May 2022, 5:19 AM
Thineshan Panchalingam
Thineshan Panchalingam - avatar