hi everyone!!!
how are you?
i'm trying to make some game using while loop

this is my code but, Unfortunately it doesn't work
:(
please help me

<html>
<SCRIPT LANGUAGE='javaScript'>
var puzzle=rabbit;
var input;
var i=0;
input=window.prompt('White and fast with four legs ??guess the answer',' ');
while(puzzle != input){

alert('hehe try againe please');
input=window.prompt('White and fast with four legs ??guess the answer',' ');

alert(' i will give u a hint');
input=window.prompt('White and fast with four legs ??guess the answer',' ');

alert('its kind of animal');
input=window.prompt('White and fast with four legs ??guess the answer',' ');

alert(' i dont like it');
input=window.prompt('White and fast with four legs ??guess the answer',' ');

alert('with big ears');
input=window.prompt('White and fast with four legs ??guess the answer',' ');
i++
}

alert('thats right');

</html>
</SCRIPT>

Recommended Answers

All 5 Replies

General:

>> Line 2 and 29 Dont use caps for elements or attributes, use lower case: <script type="text/javascript"></script> >> 28/29 There is a mismatch, the script tag needs to be closed before you close the html tag. This is wrong: <b><i></b></i>, this is correct: <b><i></i></b>

>> A script needs to stand within the head or body elements

About the script:

>> Line 3 - You can not assign a string to a variable like that, use: var puzzle = "rabbit"; >> If you run that while-loop, it will display all messages when the user entered a wrong answer at line 6

>> Line 6, 10, 13, 16, 19, 22 - You left a white space in the second argument, this can be annoying for the user when he only clicks on the box instead of selecting the entire line.

This is how you should write the while-loop:

var puzzle = "rabbit";
var input;
input=window.prompt('White and fast with four legs? Quess the answer!',' ');
var i = 1;
while (puzzle != input && input != "stop") {
alert("That answer was wrong. You have already made " + i + " mistakes, if you want to stop, please enter 'stop' into the answerbox. Perhaps you want to try again?");
input=window.prompt('White and fast with four legs? Quess the answer!',' ');
i++;
}

~G

Hi,

I really dont know whether you are looking for this. I have changed the script block to suit ur requirement.

<SCRIPT LANGUAGE='javaScript'>
var puzzle='rabbit';
var input;
var i=0;
input=window.prompt('White and fast with four legs ??guess the answer',' ');
while (puzzle != input)
{
    if (i == 0)
    {
        alert('hehe try againe please');
        input=window.prompt('White and fast with four legs ??guess the answer',' ');
    }

    if (i == 2)
    {
        alert(' i will give u a hint');
        input=window.prompt('White and fast with four legs ??guess the answer',' ');
    }

    if (i == 3)
    {
        alert('its kind of animal');
        input=window.prompt('White and fast with four legs ??guess the answer',' ');
    }


    if (i == 4)
    {
        alert(' i dont like it');
        input=window.prompt('White and fast with four legs ??guess the answer',' ');
    }

    if (i == 5)
    {
        alert('with big ears');
        input=window.prompt('White and fast with four legs ??guess the answer',' ');
    }
    i++;
    
    if (i > 5) i = 0;
}

alert('thats right');
</SCRIPT>

thank you very much for your help..
but it ain't work well...
the program will ask for the answer for ever
i want to make condition at the end to finish it
please help me

Alright then, a new version of your riddle script:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Rabit riddle</title>
<script type="text/javascript">
var puzzle = "rabbit";
var input;
alert("A riddle will follow shortly, if you want to stop, please enter 'stop' into the textfield.");
input=window.prompt('White and fast with four legs? Quess the answer!',' ');
var i = 1;
while (input != puzzle && input != "stop") {
switch (i) {
case 1 :
input=window.prompt("Try again?", "");
break;
case 2 :
input=window.prompt("Hint: it also has a tail?", "");
break;
case 3 :
input=window.prompt("Hint: it is smaller than a tree?", "");
break;
case 4 :
input=window.prompt("Hint: it is a animal?", "");
break;
case 5 :
input=window.prompt("Hint: it has big ears?", "");
break;
case 6 :
input=window.prompt("Hint: there are alot of them in Scotland?", "");
break;
case 7 :
input=window.prompt("Hint: it lives in a hole?", "");
break;
case 8 :
input=window.prompt("Hint: it eats vegetables?", "");
break;
case 9 :
input=window.prompt("Hint: alot of people think it only eats carrots?", "");
break;
case 10 :
input=window.prompt("Are you even paying attention?", "");
break;
case 11 :
input = "stop";
alert("It was a rabbit, how couldn't you have know it!");
break;
}
i++;
}
if (input == "rabbit") {
alert("Good job, you have guessed the right answer: 'rabbit'");
}
</script>
</head>
<body>
</body>
</html>

It works perfect in my browser, so if it doesn't in yours: please update your browser.

~G

thank you all for help :)

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.