forked from danilopgon/javascript-fs-pt-41
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaper-rock-scissors.js
60 lines (41 loc) · 1.35 KB
/
paper-rock-scissors.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
57
58
59
60
const prompt = require('prompt-sync')({ sigint: true });
let userNumber = 0;
let computerNumber = 0;
const promptForUserSelection = () => {
const userSelection = prompt(`
Utiliza el número para seleccionar:
1. Piedra
2. Papel
3. Tijera
`);
return Number(userSelection);
}
const selectionToString = (numero) => {
switch (numero) {
case 1:
return "piedra";
case 2:
return "papel";
case 3:
return "tijera";
default:
console.log("Selección incorrecta")
}
}
const returnResult = (userNumber, computerNumber) => {
if (userNumber === computerNumber) {
return '¡Es un empate!';
} else if (userNumber === 1 && computerNumber === 3 || userNumber === 2 && computerNumber === 1 || userNumber === 3 && computerNumber === 2) {
return '¡Has ganado!';
} else {
return '¡Has perdido!';
}
}
while (userNumber === 0 || userNumber > 3) {
userNumber = promptForUserSelection();
}
const generateComputerNumber = () => Math.floor((Math.random() * 3) + 1);
computerNumber = generateComputerNumber();
console.log(`Has elegido ${selectionToString(userNumber)}`);
console.log(`La máquina ha elegido ${selectionToString(computerNumber)}`);
console.log(returnResult(userNumber, computerNumber));