-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
48 lines (42 loc) · 1.2 KB
/
main.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
const readline = require('readline');
var rps = function(a,b) {
var a = a.toLowerCase();
var b = b.toLowerCase();
var choices = ['rock', 'paper', 'scissors']
if (choices.indexOf(a) == -1 || choices.indexOf(b) == -1) {
return new Error([
'Error! Choose one of the following: ' + choices.join(', ')
]);
};
if ((a == 'scissors' || b == 'scissors') && (a == 'rock' || b == 'rock')) {
return 'rock';
};
if ((a == 'scissors' || b == 'scissors') && (a == 'paper' || b == 'paper')) {
return 'scissors';
};
if ((a == 'rock' || b == 'rock') && (a == 'paper' || b == 'paper')) {
return 'paper';
};
if (a == b) {
return 'tie';
};
};
const rl = readline.createInterface( {
input: process.stdin,
output: process.stdout
});
rl.question('Player 1: Choose > ', (answer1) => {
rl.question('Player 2: Choose > ', (answer2) => {
var response = rps(answer1,answer2);
if (response instanceof Error){
console.log(response.message);
} else if (response == 'tie') {
console.log('tied game')
} else {
console.log('Winner: ' + response);
};
rl.close();
rl.close();
});
});
module.exports.rps = rps;