I need help with these two for loop lessons


#1

https://www.codecademy.com/courses/javascript-beginner-en-NhsaT/1/2?curriculum_id=506324b3a7dffd00020bf661#
https://www.codecademy.com/en/courses/javascript-beginner-en-NhsaT/1/3?curriculum_id=506324b3a7dffd00020bf661#

I don't get any error messages because when I press the 'Save and Submit Code' button, it doesn't run the code. It does this for both lessons. I click on the button and it won't run the code, and then I am not able to click anything else and have to exit the website. The code for both of the lessons is underneath. I don't know what is wrong, please help:confused:

// Example of infinite loop. THIS WILL CRASH YOUR
// BROWSER. Don't run the code without changing it!

for(var i = 10; i >= 5; i++){
}



// Write your very own for loop!
for(var i = 100; i >= 1; i + 5) {
    console.log(i);
}

#2

In order for the loop to begin executing, the conditional must yield true. But in order to terminate the loop, the conditional will need to finally yield false. This loop does not do that so is an infinite loop.


i is some value or state, given initially.

i = 10 is that value being set to start things off

i >= 5 is the conditional that must be satisfied in order to keep looping

i++ is the increment or state change that takes effect at the end of each iteration

To start a for loop at initial iterator value of 10, we need a condition comparison value that is greater than 10 if incrementing, or less than 10 if decrementing.

for (var i = 10; i < 20; i++) {
    console.log(i);
}

// 10 11 12 13 14 15 16 17 18 19  (except in some many lines)

Another example of infinite loop. We can correct it easily though. Set the direction to negative.

i -= 5    // short form of  i = i - 5

Then your loop will count downwards in fives.

// 95 90 85 80 75 70 65 60 55 50 45 40 35 30 25 20 15 10 5

Again, in separate lines.


#3

Thank you so much! You helped me solve the second one, but in the first one I want to decrement by 5 instead of increment up to a certain number.


#4

Remember this basic set of rules:

if the state is greater than the comparison value then the state change 
must be negative in direction to reach a conclusion.

if the state is less than the comparison value then the state change
must be positive in direction to reach a conclusion.

in either sense, a for loop is meant to render in a fixed number of iterations.

#5

This really helped, thank you!


#6

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