-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomNumberGuessingGame.html
More file actions
86 lines (64 loc) · 2.17 KB
/
randomNumberGuessingGame.html
File metadata and controls
86 lines (64 loc) · 2.17 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Test Page</title>
<link rel="stylesheet" href="css/normalize.css">
<!-- <link rel="stylesheet" href="css/style.css"> -->
</head>
<body class="style">
<div class="content" style="text-align: center; margin-top: 50px;">
<script type="text/javascript">
var randomNumber = Math.floor(Math.random () * 6) + 1;
var guess = prompt ("I am thinking of a number between 1 and 6. Quess which one.");
var correctGuess = false;
guess = parseInt(guess);
if (guess === randomNumber) {
correctGuess = true;
}
if ( correctGuess ) {
alert ("You guessed right! The number is " + randomNumber + ".");
} else if (parseInt(guess) < randomNumber) {
alert ("Close! I'm thinking of a number greater than " + guess + ".");
} else if (parseInt(guess) > randomNumber) {
alert ("Close! I'm thinking of a number less than " + guess + ".");
}
var guess2 = prompt ("Enter a second guess.");
correctGuess2 = false;
guess2 = parseInt(guess2);
if ( guess2 === randomNumber) {
correctGuess2 = true;
}
if ( correctGuess2 ) {
alert ("You guessed right! The number is " + randomNumber + ".");
} else {
alert ("Sorry! I was thinking of the number " + randomNumber + ".");
}
/* ANSWER KEY
It kept only one boolean variable correctGuess and added two more variables guessMore and guessLess.
var correctGuess = false;
var randomNumber = Math.floor(Math.random() * 6 ) + 1;
var guess = prompt('I am thinking of a number between 1 and 6. What is it?');
if (parseInt(guess) === randomNumber ) {
correctGuess = true;
} else if ( parseInt(guess) < randomNumber ) {
var guessMore = prompt('Try again. The number I am thinking of is more than ' + guess);
if (parseInt(guessMore) === randomNumber) {
correctGuess = true;
}
} else if ( parseInt(guess) > randomNumber ) {
var guessLess = prompt('Try again. The number I am thinking of is less than ' + guess);
if (parseInt(guessLess) === randomNumber) {
correctGuess = true;
}
}
if ( correctGuess ) {
document.write('<p>You guessed the number!</p>');
} else {
document.write('<p>Sorry. The number was ' + randomNumber + '.</p>');
}
*/
</script>
</div>
</body>
</html>