0

Note: Javascript Noob (just started learning today)

For practice with loops/dialogs, I'm making a simple "what number am I thinking of" program.

        <script language="javascript" type="text/javascript">
        var num = 7;
        while(num = 7)
        {
            num = prompt("What number am I thinking of? From 1-10");
            if(num != 7)
            {
                document.write("try again <br/>");
            }
            else
            {
                document.write("you got it");
            }
        }
    </script>

So I partially have it working. When the user inputs 7 (which is what I'm thinking of) it will return "you got it" but it keeps prompting the user for input. I think I've created an infinite loop because my browser gets a bit messed up every time I run it.

How would I be able to stop the program from running after the user inputs the correct number? Also if you have any suggestions on how I could do this differently or better, I'm all ears.

3 Answers 3

1

I think that you should try refactoring your code in general to something like this:

var num=prompt('Enter Guess:','');
while(num!=7){
    alert('Sorry, incorrect');
    num=prompt('Enter Another Guess:','');
}
alert('You got it!');

This basically asks the user for input, then, for as long as the input is incorrect, it tells the user and asks again. When they finally guess correctly, it continues out of the loop.

Sign up to request clarification or add additional context in comments.

5 Comments

The 'while' loop works in that it tells the user that every input that is not 7 is incorrect, however it also does it for 7.
This example would have the user enter their first guess, and then if it isn't 7 it would make them enter again until it is 7. When it is 7, the loop exits and tells them they were right. Isn't that what you want?
Exactly what I want but I'm not getting that. From looking at the code you wrote up definitely seems like it would work, but for some reason it isn't working. I tried it on chrome and FF
sorry, I fixed the issue (there shouldn't have been a strict equality sign, only normal equality
As you are new to JavaScript, I'll just explain what that bug was. JavaScript has automatic type conversion. When the user enters something into the prompt, it is a string. 7, however, is a number. JavaScript automatically converts strings to numbers and vice versa when necessary, however, so, in JavaScript, 7=='7' is true. However, I was using !==, which, like ===, means that both sides must be the same type. 7==='7' is false. Therefore, even when num is '7', num is not equal to 7
1

Change:

 while(num = 7)

to:

 while(num == 7)

2 Comments

while (num != 7), surely? (Edit: Although setting num to 7 before the while loop would have defeated that....)
@squeamishossifrage not according to the initialization of num... it's not perfectly clear to me what the OP is trying to do - but it's pretty obvious that the error is due to the fact that he's assigning a value to num inside the while instead of running a comparison. BTW, downvoting means that an answer is not useful - is it ?
0

This works:

Change:

while(num = 7)

to:

 while(num != 7)

And change the first line to:

num = prompt("What number am I thinking of? From 1-10");

3 Comments

Appreciate the help but doing that gives me a blank page. No prompt dialog box shows up
Please post your new code, I tested it myself in jsfiddle and it worked
Still working on it. Markasoftware's answer above got me closer but still doesn't work perfectly.

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.