8. When to "while" and when to "for"


#1

what am I supposed to do. I have no clue what I did wrong.


#2

You have to do both a for loop and a while loop

while(condition) {
// Do while condition is true
}

for(start; end; increment) {
// Do something!
}


#3

Check:
-That you are using the semicolon ";" in your for loop.
-That your while loop has got a variable declared outside the loop. "var variable = true;"
-The for loop doesn't need an already declared variable, so as an example here.

var variable = true;

while(variable) {
console.log("Hey there!");
variable = false;
}

for(i = 1; i > 5; i++) {
console.log(i);
}


#4

this might be confusing as you won't see a result from it because the condition is already false in the first loop.


#5

count = true;

for (var i = 0; i < 3; i++){
console.log("for looping");
};
while (count){
console.log("while loop!");
count ++;
count = false;
}

this prints both loops, 3 times the for loop and one true and one false while loop, no hidden loops....


#6

Why do you use the count++ in your while loop?


#7

ahh good question, now when i think about it, theres no need for it, as i progressed ive learnt more, also check a thread i started yesterday on my code not running please


#8

As far as I can see ionatan already gave the answer which is that you're not calling the function in which you wrapped all your code so you need a jb(); at the end of your code.


#9

browser crashes when i call jb();