-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguess_a_number.js
56 lines (50 loc) · 1.19 KB
/
guess_a_number.js
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
//Simple guess a number game created in weekend Intro to Coding course; prompt command does not work in sublime, but works via browser
var computerChoice = Math.floor(Math.random() * 10 + 1);
var userGuess;
var guessCount = 3;
do {
userGuess = guessEnter();
var i = game(guessCount, userGuess, computerChoice);
guessCount = i;
} while (i >= 1);
console.log(computerChoice);
function game(x, y, z) {
if (y > 10 || y < 1) {
return x;
} else {
while (x >= 1) {
if (y === z) {
alert("You guessed " + y);
guessCheck(x);
gameWin(true);
break;
} else if (y > z) {
alert("You guessed " + y);
x--;
alert("Too High! Guesses left: " + x);
guessCheck(x);
return (x);
} else {
alert("You guessed " + y);
x--;
alert("Too Low! Guesses left: " + x);
guessCheck(x);
return (x);
}
}
}
}
function guessEnter() {
return +prompt("Please enter a number between 1 and 10");
}
function guessCheck(i) {
while (i < 1) {
alert("Out of guesses. You lose!");
break;
}
}
function gameWin(i) {
if (i === true) {
alert("CONGRATULATIONS!!! YOU GOT THE NUMBER!!!");
}
}