forked from AustinCodingAcademy/javascript-workbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.js
More file actions
92 lines (78 loc) · 2.58 KB
/
tests.js
File metadata and controls
92 lines (78 loc) · 2.58 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'use strict';
const assert = require('assert');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function rockPaperScissors(hand1, hand2) {
hand1 = hand1.trim().toLowerCase();
hand2 = hand2.trim().toLowerCase();
const validEntries = ['rock', 'paper', 'scissors'];
if (!validEntries.includes(hand1) || !validEntries.includes(hand2)) {
return "Please enter rock, paper, or scissors.";
}
if (hand1 === hand2) {
return("It's a tie!");
}
else if (hand1 === 'rock') {
if (hand2 === 'paper') {
return("Hand two wins!");
}
else if (hand2 === 'scissors') {
return("Hand one wins!");
}
}
else if (hand1 === 'paper') {
if (hand2 === 'rock') {
return("Hand one wins!");
}
else if (hand2 === 'scissors') {
return("Hand two wins!");
}
}
else {
if (hand2 === 'rock') {
return("Hand two wins!");
}
else if (hand2 === 'paper') {
return("Hand one wins!");
}
}
}
function getPrompt() {
rl.question('hand1: ', (answer1) => {
rl.question('hand2: ', (answer2) => {
console.log( rockPaperScissors(answer1, answer2) );
getPrompt();
});
});
}
// Tests
if (typeof describe === 'function') {
describe('#rockPaperScissors()', () => {
it('should detect a tie', () => {
assert.equal(rockPaperScissors('rock', 'rock'), "It's a tie!");
assert.equal(rockPaperScissors('paper', 'paper'), "It's a tie!");
assert.equal(rockPaperScissors('scissors', 'scissors'), "It's a tie!");
});
it('should detect which hand won', () => {
assert.equal(rockPaperScissors('rock', 'paper'), "Hand two wins!");
assert.equal(rockPaperScissors('paper', 'scissors'), "Hand two wins!");
assert.equal(rockPaperScissors('scissors', 'rock'), "Hand two wins!");
assert.equal(rockPaperScissors('rock', 'scissors'), "Hand one wins!");
assert.equal(rockPaperScissors('paper', 'rock'), "Hand one wins!");
assert.equal(rockPaperScissors('scissors', 'paper'), "Hand one wins!");
});
it('should scrub input to ensure lowercase with "trim"ed whitepace', () => {
assert.equal(rockPaperScissors('rOcK', ' paper '), "Hand two wins!");
assert.equal(rockPaperScissors('Paper', 'SCISSORS'), "Hand two wins!");
assert.equal(rockPaperScissors('rock ', 'sCiSsOrs'), "Hand one wins!");
});
it('should only accept "rock, paper, or scissors"', () => {
assert.equal(rockPaperScissors('ball', 'hat'), "Please enter rock, paper, or scissors.");
})
});
} else {
getPrompt();
}