Intro to 'While' Loops ; JS ; Objective 8 While vs. For Loop


Task instruction: Write two loops in the editor: one while, one for. No restrictions on this one; just make sure your loops are syntactically correct, and be careful to avoid infinite loops!

var myCondition = true
while (myCondition === true) {
    console.log ("Here's to hoping this goes off!")
    var myCondition = false }   

var secretNumber = 0
for (var secretNumber = 0 ; secretNumber >= 100 ; secretNumber + 5 ) {
    console.log (secretNumber)

^ This is my code.
I have tried numerous way to get the second loop, the 'For' loop, to be printed out in the interface, but it never logs that one for some reason. I read elsewhere it was a condition conflict, so I changed, at least I thought I did, my variable to a different one for the for loop vs. the while loop variable, but still I have made no group.
Slightly confused, feeling defeated. Sadface.


This works for me

for (var secretNumber = 0; secretNumber < 100; secretNumber++) {

Rather than counting in 5 it counts in 1. When I had +5 in there it created an infinite loops, unsure as to why though.
Try my solution and let me know if it doesn't work.

Get rid of this because you're declaring it in the for loop.



for ([initialization]; [condition]; [final-expression]) {


In your initialization you are assigning zero to the variable secretNumber


then in your for-loop-condition you are testing if the number Value_ of secretNumber
is greater-equal than 100.
This will never happen and results in a Boolean false
and the FOR-loop exits.
So change the comparison into a less than < operator.


In your final-expression you want to increment secretNumber by 5
secretNumber = secretNumber +5


@leonhard_wettengmx_n and @benjnev ,
I reset the entire code after trying what you guys suggested and still getting infinite loops.

I rewrote this one,

for (var secretNumber = 0 ; secretNumber <= 100 ; secretNumber = (secretNumber + 5)) { console.log (secretNumber) }

var arturo = true
while (arturo) {
    console.log ("He was the greatest we had ever seen...")
    arturo = false }

It worked on the first shot this time around, after trying what you guys had said, noticing little things that we has all three still overlooked [ which may or may not have mattered either way ], and still getting the infinitelooping crash I was quite pleased that this one was a one-off wonder.

My question though, now, is this!
Does it matter that in this instance I switched the sequence in which the if / while loops occurred?