8 When to 'while' and when to 'for


#1

I need help please. Can't understand what is wrong with my code
I get an error
" Syntax error: Unexpected token"
when I run the code below

var statement = true;
var testing = function()
{
while(statement=== true)
{
console.log("Hurray!");
statement === false;
}
}
testing();

var food = ["burger"; "pasta"; "rice"; "fish"];
for(var i=0; i< food.length; i++) 
{
    console.log("I want to eat" + food [i]);
}
}
food ();


#2

HI your whille statement

while(statement=== true)
{
    console.log("Hurray!");
    statement === false;
}

this line

statement === false;

you should set statement to false so that it will stop the loop so instead of === you should use =
______
to asign a value to variable =
to compare two variable or value == or ===


#3

your while lop is infinite. you want to update your statement variable to false, to do so use a single equal sign:

statement = false

using 3 equal signs is for comparing.

array items are separated by comma's (,) not semi-colons (;)

this code:

for(var i=0; i< food.length; i++) 
{
    console.log("I want to eat" + food [i]);
}
}
food ();

i get the first closing curly bracket, but the second one doesn't match anything? and calling the food function (food();) doesn't work either, since you don't have a food function


#4

Thanks, appreciate your help. Something I have to be careful of


#5

Thanks for your help. Have managed to solve it with your help


#6

@stetim94

Appreciate your help. Was able to solve it with your help. Still trying to get my head around


#7

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