7 Infinite Loop / 2 White Loop


#1

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 .
https://www.codecademy.com/courses/learn-javascript-loops/lessons/loops/exercises/infinite-loops?action=lesson_resume&course_redirect=introduction-to-javascript


thanks guys !


#2

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


#3

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 ?


#4

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?


#5

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 !


#6

Add console.log calls to find out

let a = [1];
a.shift();
console.log(a);

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(); }
console.log(a);

#7

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


#8

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
removeSomeThings();
// then print what remains
printAllTheThings();

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

// first print all values
printAllTheThings();
// then remove some
removeSomeThings();

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


#9

thanks a lot for explanation !


#10

You would for example do something like:

console.log('thingsToDo before shift:', thingsToDo);
thingsToDo.shift();
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)


#11

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 ?


#12

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


#13

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


#14

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.


#15

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