Search code examples
javascriptloopsfor-loopincrementdecrement

How to manipulate numbers in array with for loop in JS


I have a array:

let numbers = [5, 10, 15, 25, 30];

I want to increment them all by one using a for loop. The result should be 6, 11, 16, 26 and 31.

But I can do this:

let numbers = [5, 10, 15, 25, 30];

for (let i = 0; i < numbers.length; i++)
  console.log(numbers + 1);

Solution

  • Use this loop:

    for (let i = 0; i < numbers.length; i++){
        numbers[i] += 1
    }
    console.log(numbers)
    

    Output: [6, 11, 16, 26, 31]