Code does not open JavaScript alert and prompt windows


#1



Why does this html file not open the alert and prompt window? What is wrong with this code? Please help!


<!doctype html>
<html>
<head>
<title>Colors</title>
</head>
<body onload="do_game()">
<script type="text/javascript">
var guess;
var required = false;
function do_game(){
    var colors = ['aqua', 'black', 'blue', 'brown', 'cyan', 'gold', 'gray', 'green', 'magenta', 'orange', 'pink', 'red', 'yellow', 'white'];
    var num = math.floor(math.random() * 14)
    var color = colors[num]
    alert("The target color is " + color)
    while !required{
        guess = prompt("I am thinking of one of these colors"/n/n + colors /n/n + "What color am I thinking of?");
        if guess == color {required = true};
    }
}
</script>
</body>
</html>


#2

you call function before define, you should insert this in the <head>...</head>

<script type="text/javascript">
var guess;
var required = false;
function do_game(){
    var colors = ['aqua', 'black', 'blue', 'brown', 'cyan', 'gold', 'gray', 'green', 'magenta', 'orange', 'pink', 'red', 'yellow', 'white'];
    var num = math.floor(math.random() * 14)
    var color = colors[num]
    alert("The target color is " + color)
    while !required{
        guess = prompt("I am thinking of one of these colors"/n/n + colors /n/n + "What color am I thinking of?");
        if guess == color {required = true};
    }
}
</script>

#3

Still not working bro :confused:


#4

What do you want to do?????
@abdulahhamzic


#6

ommm now i check your code, you should change some things,
semicolon( ' ; ' ) after all state line . for example: var color = colors[num];

another thing is use '(' and ')' . for example while(!required){...}

and another thing is Capitalize sensitive. there is no math object, that are Math

and special characters is wrong /n/n, this is \n that should be covered by " ".
now your code should be this:

<!doctype html>
<html>
<head>
<title>Colors</title>

<script type="text/javascript">
var guess;
var required = false;
function do_game(){
    var colors = ['aqua', 'black', 'blue', 'brown', 'cyan', 'gold', 'gray', 'green', 'magenta', 'orange', 'pink', 'red', 'yellow', 'white'];
    var num = Math.floor(Math.random() * 14);
    var color = colors[num];
    alert("The target color is " + color);
    while(!required){
        guess = prompt("I am thinking of one of these colors\n\n" + colors + "\n\n What color am I thinking of?");
        if (guess == color) {required = true};
    }
}
</script>

</head>
<body onload="do_game()">
</body>
</html>

test it,
i dont remember that use fuctions that are define after is work or not(in js) but is better to use this.

good job


#7

Thanks mate, you did the job for me, appreciate it!