Introduction to while loops: lesson 11 - Help!


#1

I am trying to write the three loops together, for, while and do/while. I keep getting, there is a problem with your syntax, unexpected token }. Can't for the life of me figure out where I've gone wrong!

for (var n = 34; n <= 42; n++) {
console.log(n);
}

var myWeight = true;
while(myWeight <= 51.5) {
console.log("Happy weight");
}
myWeight = false;

var myHeight = false;
do {
console.log("What is your height?");
} while(myHeight<=170);

myHeight();

};


#2

Okay so far here is where I found problems with your code @nataliemp.

var myWeight = true;
while(myWeight <= 51.5) {
console.log("Happy weight");
}
myWeight = false;

You have registered "myWeight = true" yet when you called it into the while loop you had it as a number.

var myHeight = false;
do {
console.log("What is your height?");
} while(myHeight<=170);

myHeight();

};

Also "myHeight = false" had the same issue, when you registered it it is false yet when you called it into the do/while loop you had it as a number.
Try making it so when you call "myWeight" into your while loop it starts as true yet after "console.log"ed it turns false with re-registering it inside the function.
With "myHeight" try calling it into the do/while loop as it being false like you registered it to be.
If this does not work tell me and I will try to help you find what the problem is.


#3

Thanks so much for your help! I've been dipping in and out of this which is not helping as its not sticking!
Is this what you meant:

var myWeight = 51;
while(myWeight <= 51.5) {
console.log("Happy weight");
myWeight = false;
}
var myHeight = 158;
do {
console.log("What is your height?");
while(myHeight<=170);
myHeight = false;
}
myHeight();

};


#4

I apologize for the confusion but that is not what I meant, what I meant is something like this.

var myWeight = true;
while(myWeight = true) {
console.log("Happy weight");
myWeight = false;
}
var myHeight = false;
do {
console.log("What is your height?");
while(myHeight = false);
}

What I meant is you registered your variables as true or false and when calling on them in your function turned them into a number, instead have them true or false and call on them as true or false. You only need to turn myWeight = false in the function because a while loop will not stop until it finds a reason to.


#5

Ahh I see. I'm still getting unexpected token } though


#6

@nataliemp I believe your issue is that you have a random and unneeded "};" try removing it and then see.


#7

I now have the below, which results in unexpected end of input!!

for (var n = 34; n <= 42; n++) {
console.log(n);
}

var myWeight = true;
while(myWeight = true) {
console.log("Happy weight");
myWeight = false;
}

var myHeight = false;
do {
console.log("What is your height?");
while(myHeight = false);

}


#8

@nataliemp I took your answer and put it into the problem to work out the problem myself and I found after I did this to it it worked just fine.

for (var n = 34; n <= 42; n++) {
console.log(n);
}

var myWeight = true
while(myWeight) {
console.log("Happy weight");
myWeight = false;
}
var myHeight = false;
do {
console.log("What is your height?");
} while(myHeight = false);

Now I believe your issue is when you put a ; after registering the variable, putting "(myWeight = true)" not "(myWeight)", and that you put your end statement after the while in your do/while loop. While some of this is at the fault of mine I apologize for taking so much time.