-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
50 lines (38 loc) · 1.02 KB
/
script.js
File metadata and controls
50 lines (38 loc) · 1.02 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
/**
NUMBER GUESING GAME
1. Player chooses a number;
2. Computer chooses a number;
- random number generates.
3. Compare choices;
- Greater number wins.
- If tie, player wins
4. Play game
*/
const playerChoice = (playerInput) => {
if (playerInput) {
return playerInput;
} else {
return null;
}
}
const computerChoice = () => {
let computerInput = Math.floor(Math.random() * 11)
return computerInput;
}
const compareChoices = (playerInput, computerInput) => {
if (playerInput >= computerInput) {
return 'Player wins!';
} else {
return 'Computer wins!';
}
}
const playGame = () => {
let playerInput = playerChoice(7);
if (playerInput === null) {
return console.log('Enter a number');
}
let computerInput = computerChoice();
let results = compareChoices(playerInput, computerInput);
return console.log(`Players number: ${playerInput}, Computers number: ${computerInput}. The results are in: ${results}`);
}
playGame();