-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumbers.html
More file actions
66 lines (58 loc) · 1.41 KB
/
numbers.html
File metadata and controls
66 lines (58 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<!DOCTYPE html>
<html>
<body>
<script>
var intro = "I am thinking of a number between 0 and 9999.\n\nCan you guess the number?";
//start game
do_game();
function do_game()
{
count = 0
var isCorrect = false;
target_number = Math.floor(Math.random()*10000)
//show answer
alert(target_number);
while (!isCorrect)
{
guess = prompt(intro);
count++;
if (check_guess())
{
isCorrect = true;
}
}
}
function check_guess()
{
if (guess < 0 || guess > 9999 || isNaN(guess))
{
alert("Sorry, please enter a number within 0000-9999.\n\nPlease try again.");
return false;
}
else if(!guess)
{
alert("Sorry to see you go...\n\nThe answer was: " + target_number);
return true;
}
else if (guess > target_number)
{
alert("Nice try, but not quite!\n\nPick a lower number!");
return false;
}
else if (guess < target_number)
{
alert("Close, but not yet.\n\nPick a higher number!");
return false;
}
else
{
//correct
var congrats = "Congratulations! You have guessed the number!\n It took you " + count + " guesses to finish the game!\n";
document.body.style.backgroundColor = "green";
alert(congrats);
return true;
}
}
</script>
</body>
</html>