7 Infinite Loop / 2 White Loop

Hi all !
can anyone explain me why my .shift() method doesn’t remove any element from the array ?
and print the 5 five element of thingsToDo ? yet I click on RUN many times .
NO ERROR message .
JS Infinite Loop Prob
thanks guys !

What makes you say it doesn’t? Have you, for example, printed the length before and after?

Yes I did . Look the result is printed on the Terminal , isn’t it ? (with removing any one of them ) . Do I have to reset ?

Your output seems to say that elements do get removed. What is your argument for that they don’t?

What would happen if no elements are removed? What would happen if they are removed?
If no elements are removed, then you should get the same effect if you comment out that line, shouldn’t you?

Yes but isn’t it supposed to remove an element each time the block is executed , is it ? So we should have the array minus the first (.shift) or the latter (.pop) element of the array .
Am I right Ionatan ? So always 4 elements should appear on the Terminal , not 5 . On my joined screen , you see that there are all the element of the thingsToDo array .
Did I understood ?
thanks Ionatan for your explanation !

Add console.log calls to find out

let a = [1];

You print them all out before removing anything, you have two loops, completely separate, one runs, then the other runs. The first one prints them out. The second does some removals.
And, if the removals happened first (basically reading the file backwards) then you’d not end up with 4 elements, you’d end up with a different number

let a = [1, 1, 1, 1, 1, 1];
while (a.length > 3) { a.shift(); }

In your second case , what is it supposed to be printed on the Terminal ?

You said there should be 4 elements left after the removals. That loop does not leave 4 elements.

And what you seem to be proposing is that your code essentially reads as:

// first remove some
// then print what remains

But if you read your code it’s in the opposite order:

// first print all values
// then remove some

And you really shouldn’t be jumbling up the order like that. Get the formatting right so that everything is neat and readable, and then, if things don’t behave like you expect, start adding console.log calls to find out what happens differently from what you expect. Make it print out what it’s doing step by step

thanks a lot for explanation !

You would for example do something like:

console.log('thingsToDo before shift:', thingsToDo);
console.log('thingsToDo after shift:', thingsToDo);

You’d now see that the array gets smaller each time that happens, you’d see how many times it happens, and you would see when it happens (after printing)

I thought the opposite order of the array became from the thingsToDo.length -1 method ? Was it the reason of the opposite order it appears ?

You can loop from length-1 to 0, or you can do like me and reverse the array and loop forwards instead (easier to read). Using a for…of loop instead of indexes also means no “start at this number, increase by 1, stop at this here other number” and “use this index to look up the value we want” – instead it’s just “take each value from the reversed array” which is really easy to understand, it’s easy to see that it’s correct and not off-by-1, and it’s really hard to get it wrong in the first place too

ok thanks a lot Ionatan . It’s very interesting to work on this matter .

I actually introduced a bug in that though. I meant to create a new array which is the reverse of the original. Turns out I reversed the original instead. Some refinement needed to that, clearly.

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