Infinite Loops, Loops Excercise 7/7


#1

Hi, I’m having trouble with this exercise on infinite loops, particularly with dealing with the while loop.

My code

I read the hint, which mentioned something about using .pop or .shift, but I am confused as to why this loop is infinite. I don’t understand the explanation in step 2 of this exercise. What does “removing an element” have to do with the code running succesfully? I am very confused and have little idea of what this exercise is telling me.
Thank you for any help!


#2

The link you provided is to the exercise, which is very much appreciated so we can test your code, however you will need to post the code as we cannot see it otherwise. Only our own code is visible.


#3

I was able to pass the exercise by guessing the correct function shift(), but I don’t know why it was necessary. Here is my code, and if you could explain what is happening in the while loop in this case, I would be much appreciative:

let thingsToDo = ['do laundry', 'take out the garbage', 'make dinner', 'walk the dog', 'go to the bank'];

for (let thingsToDoIndex = thingsToDo.length - 1; thingsToDoIndex >= 0; thingsToDoIndex--) {
  console.log('I need to ' + thingsToDo[thingsToDoIndex] + '.');
}

while (thingsToDo.length > 3) {
console.log('I have too many things to do!');
thingsToDo.shift();
}

#4

.shift or .pop will have that same effect, essentially since they both shorten the array. The former removes from the top of the array, the latter from the bottom.


#5

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