forked from vlee90/guessing-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
guess-game.html
31 lines (31 loc) · 1.15 KB
/
guess-game.html
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
<script>
var guess; //Where user's guess is stored.
var guessList = new Array(); //Remember user's guesses.
var answerNumber = "6"; //Correct answer in numeric form (must be string because prompt returns string).
var answerWord = "SIX"; //Correct answer in numeric form.
var outputMessage; //Tells users the result.
var attempt; //Guess number
var maxAttempts = 3; //Maximum allowed guesses.
var correct = false; //Used to break for-loop when user gets
for(attempt = 1; attempt <= maxAttempts; attempt++){
guess = prompt("How many protons are in carbon-12 atom?").toUpperCase();
switch(guess){
case answerNumber:
outputMessage = "6 is correct!";
correct = true;
guessList[attempt - 1] = guess;
break;
case answerWord:
outputMessage = "6 is correct!";
correct = true;
guessList[attempt - 1] = guess;
break;
default:
outputMessage = "Incorrect. You have " + (maxAttempts-attempt).toString() + " attempts left.";
guessList[attempt - 1] = guess + " ";
}
alert(outputMessage);
alert("Your previous guesses are " + guessList);
if(correct === true) break;
}
</script>