8/11 why is this wrong?


#1

for (var i=1; i<4; i++) {
console.log(i);
}

var myName = "Dzemal"
while (myName) {
console.log("I love" + " " myName);
myName = "Gekic";
}

I get this: SyntaxError: Unexpected identifier 'myName'. Expected ')' to end a argument list.


#2

for (var i=1; i<4; i++) {
console.log(i);
}

var myName = "Dzemal"
var loop = true;
while (loop) {
console.log("I love " + myName);
loop = false;
}


#3

I get that... but shouldn't it work without the var loop?
doesn't the "myName = something else" show that myName = Dzemal is no longer true?


#4

while loops consider a true or false statement if the condition in the while loop is true anything inside will run. If its false then it wont .

no i think when the while loop checks the condition it just sees a different string not a true or false statement.

try this

for (var i=1; i<4; i++) {
console.log(i);
}

var myName = "Dzemal";
while (myName == "Dzemal") {
console.log("I love " + myName);
myName = "John";
}
The while loop now checks to see if myName is Dzemal if not it wont run the loop.


#5

that worked! thanks!


#6

No problem glad i could help.


#7

To extend this good answer. As you can read here:
http://www.sitepoint.com/javascript-truthy-falsy/
any value in a condition is interpreted as either true or false so in this case both "Dzemal" and "Gekic" are non-empty strings and are therefor treated as true which makes this an infinite loop.

@kal_el666 For the original error:

console.log("I love" + " " myName);

in this line you missed a + to concatenate myName with your string.


#8

var loop=function()
for (i=0;i>10;i++){
console.log(i);
}
var myName="Ahad"
while(myName){
console.log(myName);
myName="false";
};
Here is my code. I keep getting "Unexpected token for"


#9

you need to wrap the stuff that belongs to your function in {}. As they are mssing it complains about the first thing after function being for and not {