# Iterate with Javascript For Loops Help Please

Tell us what’s happening:
Is this challenge not able to be done with Decrements? If so how would I change it from what I have here? I was able to look at the solution in the hints and see why that worked, but I like trying to come at the problem from the other side to see if I understand how it works.

Your code so far

``````var myArray = [];

// Only change code below this line
for (let x = 6; x > 0; x--) {
myArray.push(x);
}

``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36`.

Challenge: Iterate with JavaScript For Loops

Link to the challenge:

Not if you want the right answer. That will give you: `[6, 5, 4, 3, 2, 1]`, which is not what was requested.

You can make it work if you change your `for` loop a little and do a little math with what you push.

1 Like

I have tried 5 as well, 1 as an endpoint and 0 as an endpoint and they are not working. I got the problem solved, but have been trying to work through this part of it for my own curiosities sake. It is going to bug me if I cannot understand why this solution I cannot wrap my head around. It should not be more difficult for me to decrement than increment, and it has to be a hurdle in my own head I would rather see now.

I have been trying to work on this for about 30 minutes now. The way I see it

for = declares that I am going to be inputting data for a For Loop
Let x = Integer - Declares the initial value of X
x > 0 - this is the condition that is checked before the loop runs, so while X registers as higher than 0 it will run the next step after the step declared in the next line
x-- - decrements x by 1 integer

``````myArray.push(x);
``````

This tells the for loop that when the conditional trigger is met it pushes it to the array, as the last value of the array. (0 ->1 ->2)

Am I missing something or something not clicking on this one?

The challenge requires you to use `push`.

If you want to also decrement inside of your loop, then you will need to do some math to turn the numbers that are counting down into the values that are counting up.

``````for (let i = 5; i > 0; i--) {
console.log(i);
console.log(10 - i);
}
``````

5
5
4
6
3
7
2
8
1
9
Why does that return this? It does not make sense to me, could you please explain it? Is there a reason it is not functioning sequentially? I just pushed the code into a Replit to get access to a quick console if that is the problem.

The loop is processing `i` sequentially

``````for (let i = 5; i > 0; i--) {
console.log("Iteration: " + i);
console.log("10 - i = " + (10 - i));
}
``````

That would be 5 4 3 2 1 0 -1 -2 -3 -4 though wouldn’t it?

? why would it be that?

`i` goes from 5 to 1

Did you run with the modified console statements?

I ran it with the code as a copy paste from your initial post above.

``````// Setup
var myArray = [];

// Only change code below this line
for (let x = 5; x > 0; x --) {
myArray.push(x);
}

console.log(myArray)
``````

This works when I run it, but the challenge is still rejecting it. What am I missing please?

The challenge rejects it because the the elements of the array are in the wrong order. Did you try console logging the result and looking at the order of the elements? Do they match the requested order? There is a simple bit of math you can do to fix the numbers you push into the array…

Are we still trying to get this to work with a descending loop?

Right, the issue is that we have a descending count, but we want an ascending array. So, how do you mathematically transform 5-1 into 1-5. What mathematical operation would turn 5 into 1, 4 into 2, 3 into 3, etc,

So we have this:

``````for (let x = 6; x > 0; x--) {
var numToPush = x; // change this
myArray.push(numToPush);
}
``````

You just need to change that line with a very simple mathematical formula, a kind that most grade school students could handle. (I don’t mean that to put you down, just to keep you focussed on something simple.)

The other problem is that the way this loop is configured, you will have one too many elements.

There is also another way to do this with your decrementing loop - to build the right array that is in the wrong order and then put it in the right order. There is an array prototype method that will reverse the order of an array. There is also one that will sort an array.

1 Like

You can also use negative numbers and `Math.abs`. Although, that’s a bit of a weird way of doing it.

2 Likes

Yeah, but using a decrementing `for` loop to create an array of ascending numbers is already a bit odd. I mean, it’s an interesting intellectual puzzle, but already escaping the world of practicality. So, why not, add your idea to the list.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.