This repository was archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
61 lines (46 loc) · 1.92 KB
/
main.js
File metadata and controls
61 lines (46 loc) · 1.92 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
51
52
53
54
55
56
57
58
59
60
61
// uses strict mode so strings are not coerced, variables are not hoisted, etc...
'use strict';
// brings in the readline module to access the command line
const readline = require('readline');
// use the readline module to print out to the command line
const rpsInterface = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log("Welcome to Rock Paper Scissors!")
// the function that will be called by the unit test below
function logic(userChoice, computerChoice) {
if (userChoice === computerChoice) {
console.log('This was a draw');
console.log("If you want to settle this, rerun 'node main.js'");
return;
}
else if ((userChoice === "rock" && computerChoice === "scissors") || (userChoice === "paper" && computerChoice === "rock") || (userChoice === "scissors" && computerChoice === "paper")
) {
console.log('Congratulations, you won!');
console.log("Wanna play again? Rerun 'node main.js'");
return;
} else {
console.log('Nice try but I WIN sucker');
console.log("Wanna give another shot? Rerun 'node main.js'");
return;
}
};
// the first function called in the program to get an input from the user
// to run the function use the command: node main.js
// to close it ctrl + Cre
rpsInterface.question('Pick a hand: ', (userInput) => {
const userChoice = userInput.toLowerCase().trim();
if (userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors") {
console.log("invalid input provided, has to be rock, paper, or scissors");
console.log("Rerun 'node main.js' and try again");
rpsInterface.close();
return;
}
const possibleChoices = ["rock", "paper", "scissors"];
const computerChoice = possibleChoices[Math.floor(Math.random() * possibleChoices.length)];
console.log(`You picked: ${userChoice}`);
console.log(`I picked: ${computerChoice}`);
logic(userChoice, computerChoice);
rpsInterface.close();
});