# What am I doing wrong? - Use a for loop to iterate through the array

I’m having a hard time figuring out what they expect me to do in this course.

Instructions: You are given an array of five numbers called increaseByTwo. Use a for loop to iterate through the array and increase each number by two. This means I need to change the screen from “1, 2, 3, 4, 5” to “3, 4, 5, 6, 7”.

CODE:

``````const increaseByTwo = [1, 2, 3, 4, 5];

// Uncomment the line below to check your work!
// console.log(increaseByTwo); // -> should print [3, 4, 5, 6,
7];
``````

What I’ve done so far: I’ve pushed two more numbers to the console, so now instead of 1-5, it’s 1-7.

In attempt 1, I tried to pop the first two numbers out of the const.

CODE:

``````const increaseByTwo = [1, 2, 3, 4, 5];
for (let i = 0; i < 1; i+=2)
increaseByTwo.push(6, 7)
for (let i = 0; i > 3; i--) {
increaseByTwo.pop(1, 2)
}
``````

In attempt 2, I almost got that result, but it was in columns and not rows. When I take “i” away from the console.log, it results into telling me 1-7, 7 different times.

CODE:

``````const increaseByTwo = [1, 2, 3, 4, 5];
for (let i = 0; i < 1; i+=2)
increaseByTwo.push(6, 7)
for (let i = 2; i < 7; i++) {
console.log(increaseByTwo[i])
}
``````

What am I doing wrong?

Hey and welcome to the forums! At the moment you are hard coding the values `6` and `7` in, now while this may work for that given input, where it would not work is if you had a different starting array. One way of doing it that would avoid this be to modify each element in-place in the array by accessing it’s index with `array[i]` and adding to that, as that would work for all inputs. If you want to give it a go, I’d suggest giving it a shot yourself first, but I’ll leave a hint using a different array/goal below if you’d like it.

Hint
``````// Tripling the elements in an array
for(let i = 0; i < array.length; i++) {
array[i] *= 3;
}
``````

Happy coding! 2 Likes

Oh my god that was it thank you so much

1 Like