0

My apologies for the n00b question, I've tried looking through infinite loop related issues but they're way more complex:

var replay = 1;
while (replay = 1) {
replay = prompt("Yes(1) or No(0) ?");
}

How come this is an infinite loop?

I thought this while loop would only continue iterating while the replay variable has a value of 1.

However it doesn't stop even when user input is 0, or anything else for that matter.

Thanks in advance for any of your input!

2
  • 5
    Consider using a linter like jshint.com to help you find common mistakes. It would have told you "Expected a conditional expression and instead saw an assignment.". Commented Aug 9, 2013 at 23:27
  • Wow thanks everyone so much for the warm welcome with my 1st question. My new family is here :D Can't believe I mixed up the operands... @CrazyTrain: thanks for sharing! Commented Aug 10, 2013 at 0:33

5 Answers 5

4

You're doing an assignment instead of a comparison.

Change:

while (replay = 1) { // Will always have a value of 1

to:

while (replay == 1) { // Will have a value of true or false
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! btw, I'd really want to thank every single person, but would that be an appropriate thing to do on this site ?
3

Use == instead of = in the while part.

Comments

1

You are assigning not checking in (replay = 1)

You need double equal signs ==, or better yet triple equal signs === which will also check the equality in types of the operands.

Besides, your code can be changed to this (preview: http://jsfiddle.net/nabil_kadimi/RfdA5/):

var replay;
while ((replay = window.prompt("Yes(1) or No(0) ?")) === '1') {
  /* player wants to replay */;
}

Or even better (preview: http://jsfiddle.net/nabil_kadimi/pdV4M/):

var replay;
while (replay = window.confirm("Replay?")) {
  /* player wants to replay */;
}

5 Comments

@user2669369, Welcome to SO, feel free to "upvote" using the top arrow my answer and any answer that helped you and don't forget to "accept" the one that was the most helpful.
I apparently don't have enough reputation to upvote anyone's comment yet xD
I ran it dozens of times yesterday just fine, and today I got this: 'TypeError: Property 'confirm' of object [object global] is not a function' How come ? :/
That's the thing. Nothing. Nada. It doesn't work with Chrome's console, or inside a Codecademy lesson. It however works fine at labs.codecademy.com, and jsfiddle.net. Would this be a matter of whether the right library is included ?
This is native JS, you don't need to include anything, the error is somewhere else.
1

You need to use == (equality) instead of = (assignment) in your while loop

while(replay == 1) {
  //code
 }

JavaScript is doing what it is supposed to. You are reassigning the value of 1 to replay every time the loop iterates. You really want to check if replay is equal to one before proceeding.

1 Comment

At some point I had a feeling the 2 "replay" variables aren't the same, started to get all mixed up with local global variables as well lol.
0

You want to use the === comparison operator instead of the = assignment operator in the while loop.

Also, since prompt returns a string, you should compare against a string:

var replay = "1";
while (replay === "1") {
  replay = prompt("Yes(1) or No(0) ?");
}

1 Comment

Thank you for pointing out the string comparison issue as well!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.