I've been trying to debug this for a couple of days...cannot see why it performs the way it does. Help is much appreciated.
Forgive me about how lengthy the assignment details are.

-Create a function named whileTest(). Inside the function, create a variable named number and assign it a value between 1 and 10.
-Create another variable named answer and assign it a value of 0 (zero).
-Then create a while loop. Create code that will cause the loop to execute as long as the number variable does not equal the answer variable.
-Inside the loop, assign the answer variable the return value from a prompt dialog box.
-The prompt will ask the user to guess a number between 1 and 10. The loop will continue until the proper answer is entered.
-After the loop exits, use an alert dialog box to inform the user of a correct guess.
-Once you have the code working properly, create code that will allow the user only three guesses. If, after three guesses, the user has not entered the correct answer, exit the function and alert the user that he or she is out of guesses via an alert dialog box.
-Ensure that only one dialog box appears after the function is exited, one with a correct guess message, or one asking the user to try again.
-Experiment with different methods that you have seen for calling the function. You can use the load event or the onclick event handler of a form button.

Here is my code:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>While Test</title>
    <script type="text/javascript">

        function whileTest()
        {
            var number = 5;
            var answer = 0;
            var chances = 0;
            answer = prompt('Guess a number between 1 and 10. ');

            //I've tried the if statement for correct choice here...page doesn't load.
            while(answer != number)
            {
                answer = prompt('Wrong! Please Guess Again! ');
                chances++
                if(answer == number && chances >=1)
                    {
                        alert('You have chosen correctly!! ');
                        return;
                    };//Works here, but if the first guess is correct, page stops loading.

                if(chances == 2) 
                    {
                        alert('You have guessed incorrectly 3 times...Goodbye! ');
                        return;
                    };
            //Any time I try to use an if statement outside of the while loop,
            //the page will not load, or doesn't load anything. I can't understand why.

              };
        };
    </script>
  </head>
  <body onload="whileTest()">
  </body>

</html>

Recommended Answers

All 9 Replies

I apologize for all of the indents...my professor's insistence.
As long as the correct number is not chosen first, the program seems to run like it should. I cannot figure out why it stops loading if the correct choice is picked first. That's really what I need explained more than anything.

Member Avatar for Zagga

Hi.

Your professor is right about indenting your code. It makes it much easier to read so stick with it.

Lets run through your code:
You open the function and ask the user for a guess.
You have defined what happens if the user is wrong (it starts the WHILE loop) but you haven't defined what happens if the user is right, so the function simply ends.

Move the first prompt (line 13) into the WHILE loop (after line 17). The WHILE loop will still start because initially answer doesn't equal number.

Enclose what happens with a wrong answer (lines 18 & 19) in an IF statement similar to you do for a correct answer.

Remove the second condition from the IF statement on line 20 (because it will fail if the user guesses correct on the first run).

The updated Javascript part of your code:

<script type="text/javascript">
    function whileTest()
    {
        var number = 5;
        var answer = 0;
        var chances = 0;
        //I've tried the if statement for correct choice here...page doesn't load.
        while(answer != number)
        {
            answer = prompt('Guess a number between 1 and 10. ');
            if(answer != number)
                {
                    answer = prompt('Wrong! Please Guess Again! ');
                    chances++;
                }
            if(answer == number)
                {
                    alert('You have chosen correctly!! ');
                    return;
                };//Works here, but if the first guess is correct, page stops loading.
            if(chances == 2) 
                {
                    alert('You have guessed incorrectly 3 times...Goodbye! ');
                    return;
                };
        //Any time I try to use an if statement outside of the while loop,
        //the page will not load, or doesn't load anything. I can't understand why.
          };
    };
</script>

Thanks,
I just realized that I missed his instruction about the prompt being inside the while loop.
I should state, I wasn't saying I was against indents, It's just his indentation rules are quite different from my C++ and Java Instructors.
I have one small comment about your code structure...when it runs, if correct isn't found it actually loops 4 times before terminating.
I adjusted it a bit further.
Here's what I got:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>While Test</title>
    <script type="text/javascript">

        function whileTest()
            {
                var number = 5;
                var answer = 0;
                var chances = 0;
                while(answer != number)
                    {
                        answer = prompt('Guess a number between 1 and 10. ');
                        chances++;
                            if(answer == number)
                                {
                                    alert('You have chosen correctly!! ');
                                    return;
                                };
                            if(chances == 2) 
                                {
                                    alert('You have guessed incorrectly 3 times...Goodbye! ');
                                    return;
                                };
                            if(answer != number)
                                {
                                    answer = prompt('Wrong! Please Guess Again! ');
                                };

                    };
            };
    </script>
  </head>
  <body onload="whileTest()">
  </body>
</html>

I realized that the reason the suggested code ran 4 times on incorrect answers because the loop met with the "if(wrong)" statement and increased the counter by 1 before it checked to see if the three chances had been used up. I also tried it before I swapped the order of the other two if statements, but it didn't work as intended either, just can't remember what it did exactly.
Thank you a ton for your help and instruction. I try my best to exhaust my thought process before asking for guidance. DaniWeb has always been the best source for solid help.

Member Avatar for Zagga

Hi again,
It's absolutely fine to ask for help if you get stuck after you try. That's what we're here for.

The code you have now does work but after the second incorrect guess you aren't told you were wrong, you just get asked to try again.

I originally used the code structure you supplied and made a few changes but didn't actually test the code, sorry.
I see now why the code seemed to run too many times. It was due to the second prompt call (line 18 of your original code) that should have been an alert.
If you change the prompt to an alert, change the check statement to see if chances are 3 instead of 2 (we want it to run 3 times after all), rearrange the order of the IF statements slightly to prevent being shown a 3rd 'wrong answer' alert followed by a 'goodbye' alert and move where you increment the chances (like you already have) it should work and may be a little clearer to understand and slightly more user friendly.
Updated javascript:

<script type="text/javascript">
    function whileTest()
    {
        var number = 5;
        var answer = 0;
        var chances = 0;

        while(answer != number)
        {
            answer = prompt('Guess a number between 1 and 10. ');
            chances++;
// If correct
            if(answer == number)
                {
                    alert('You have chosen correctly!! ');
                    return;
                };
// If too many tries.
            if(chances == 3) 
                {
                    alert('You have guessed incorrectly 3 times...Goodbye! ');
                    return;
                };
// If incorrect.
            if(answer != number)
                {
                    answer = alert('Wrong! Please Guess Again! ');
                }
          };
    };
</script>

Give it a try and see which version you prefer.

I noticed that last night when I was testing it. I played with it more but it got so late that I forgot to repost.
I appreciate all of the help very much. I also kept running into a problem where every other wrong answer would bring up the original prompt. I fixed that by placing it into it's own if statement at the beginning of the while loop, with the condition being that chances == 0;
For some reason, it still runs an extra time if I have the exit conditional set to a limit of 3.
Here is my latest revision:

<html>
    <head>
    <meat charset = <"utf-8">
    <title>While Test</title>
        <script type="text/javascript">
    function whileTest()
            {
                var number = 5;
                var answer = 0;
                var chances = 0;

                while(answer != number)
                    {
                        if(chances == 0)
                            {
                                answer = prompt('Guess a number between 1 and 10. ');
                            };

                        if(answer != number)
                            {
                                answer = prompt('Wrong! Please Guess Again! ');
                                chances++;
                            };
                        if(answer == number)
                            {
                                alert('You have chosen correctly!! ');
                                return;
                            };
                        if(chances == 2) 
                            {
                                alert('You have guessed incorrectly 3 times...Game Over! Please Try Again. ');
                                return;
                            };
                    };
            };
        </script>
    </head>
    <body onload="whileTest()">
    </body>
</html>
Member Avatar for Zagga

The reason it sometimes seems to run more times than it should is because you still have 2 prompts (line 16 and line 21) so with certain conditions you are asked a question twice on the same itteration of the WHILE loop.
The loop should:

1) start
2) ask you the question (once)
3) perform 1 of 3 actions based on your reply
3a) Either alert the user that they guessed wrong, increment the counter
3b) or alert the user that they guessed right, return out of function
3c) or alert the user that that have tried too many times, return out of function
4) return to the start of the loop

Yes but my last code will only run the first prompt one time, when the loop starts for the first time, since either the correct answer is entered first and the loop and function exits, or the wrong answer is entered, changing the value of the chances variable to 1. As far as I can see, there is nothing inside the code that can change the value of the chances variable back to 0. I only tested this in google chrome, but I'm going to test it in firefox and IE as well, like I already should have. Thanks a ton!

Member Avatar for Zagga

You don't want to reset the chances variable. Just remove the IF statement around the prompt and it will ask the question on every iteration of the loop.
Don't forget to change that second prompt to an alert.

I see what you are saying after reading the assignment again. I was under the impression that he wanted a prompt for the beginning, and a different prompt when a wrong guess was entered...or maybe I subconsciously wanted to overcomplicate what needed to be done (which is probably the case). Thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.