There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
const animals = [‘Grizzly Bear’, ‘Sloth’, ‘Sea Lion’];
for (let i = 0; i < animals.length; i++) {
console.log(animals[i]);
}
I’m having a conceptual problem with how this can be the stopping condition:
i < animals.length
So, assuming:
[‘index 0’, ‘index 1’, ‘index 2’]
[‘Grizzly Bear’, ‘Sloth’, ‘Sea Lion’]
then it seems to me that the code block should stop when i (index value) is less than animals.length (3), which always has to be true, because beyond i=2, there are no values for i.
stopping condition is not really the term to describe the conditional expression here, but so be it if that is how the teacher wishes to describe it being that it is a conditional that effects termination of the loop. Perhaps stopping conditional would be more apt so we don’t read in that it is the condition that will stop the loop. Quite the opposite, it is the condition that allows the loop to continue.
As it is written in the code, we could see it as the looping condition which is dependent upon ‘true’ to keep going.
MTF’s thought to see it as a
“continuing condition”, rather than a “stopping condition”, helped me out a little, and I think I get it now. i is not equivalent to a value in the code block, rather to an index position. So it’s better to think of i as a generated number (starting off at ‘0’, the iterator) which will serve as index positions in the code block.
If i=0, the operation will perform, since it’s less than 3. (and then the the index position of ‘0’ will be put into the code block, which will then log to the console whatever is in that index position in the array: i.e., ‘Grizzly Bear’.)
If i=1, the operation will perform, since it’s less than 3. (and then the the index position of '1" will be put into the code blcok, and the console will log whatever is in that index position in the array: ‘Sloth’.)
If i=2, the operation will perform, since it’s less than 3. (and then the the index position of ‘2’ will be put into the code blcok, which will then pointt to what’s in that index position in the array: ‘Sea Lion’.)
if i=3, the length of the array,the code block will not run, and the loop is terminated.
Ugh, not sure if that helped. May have caused more confusion! But I think the word “value” points to where your issue is.
Thanks for the explanations and step-by-step walk-through! I understand it much better now. My main misunderstanding I think was assuming the stopping condition (more aptly referred to as the looping/continuing condition) stopped the block from running if it outputted TRUE but, if I understand correctly, an output of TRUE continues the block, whereas FALSE stops it from running.
let gives variables block scope which keeps them from leaking into their parent scope, or worse into global scope. It’s actually a very important concept in JS.
ok, i get it, this would be true if it would have been a stop condition. it would go on, till it saw it did it already 3 times. but since this is not a ‘stop condition’ , but rather a looping condition, it will go on and on, because it will always answer true
It’s not a problem, per se, only that it is a legacy keyword that has gone out of fashion in ES6. let is intended to overcome a weakness that var presented by introducing true block scope. The author is likely hoping to help learners get away from using it. let is the more preferred keyword, these days and moving forward.
I keep getting an error when trying to run this exercise. The only way I could proceed was by getting the solution, which to me looks exactly like my code. Is it a bug?
for (let i = 0; i < vacationSpots.length; i++) {
console.log('I would love to visit ' + vacationSpots[i]);
}
Even when getting the solution, copying the result, resetting the exercise and trying to run the solution, I get an error.