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
99 lines (80 loc) · 2.89 KB
/
tests.js
File metadata and controls
99 lines (80 loc) · 2.89 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
89
90
91
92
93
94
95
96
97
98
99
'use strict';
const assert = require('assert');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function rockPaperScissors(hand1, hand2) {
const player1 = hand1.toLowerCase();
const player2 = hand2.toLowerCase();
const validAnswers = "rockpaperscissors";
if ((validAnswers.indexOf(player1) == 1) || (validAnswers.indexOf(player2) == -1)) {
return "Invalid answer(s)";
}
if ( player1 === player2 ) {
return "It's a tie!";
}
if ( player1 === "rock" ){
if (player2 === "scissors") {
return "Hand one wins!";
} else if ( player2 === "paper" ) {
return "Hand two wins!";
}
}
if ( player1 === "paper" ){
if (player2 === "scissors") {
return "Hand two wins!";
} else if ( player2 === "rock" ) {
return "Hand one wins!";
}
}
if ( player1 === "scissors" ){
if (player2 === "paper") {
return "Hand one wins!";
} else if ( player2 === "rock" ) {
return "Hand two 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', 'scissors'), "Hand one wins!");
assert.equal(rockPaperScissors('rock', 'paper' ), "Hand two wins!");
assert.equal(rockPaperScissors('paper', 'rock' ), "Hand one wins!");
assert.equal(rockPaperScissors('paper', 'scissors'), "Hand two wins!");
assert.equal(rockPaperScissors('scissors', 'paper' ), "Hand one wins!");
assert.equal(rockPaperScissors('scissors', 'rock' ), "Hand two 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!");
assert.equal(rockPaperScissors('RoCk', 'PaPeR' ), "Hand two wins!");
assert.equal(rockPaperScissors('', '' ), "Hand _ wins!");
});
it('should check for valid entry'), () => {
assert.equal(rockPaperScissors('roq','payper'), "Invalid answer(s)" )
assert.equal(rockPaperScissors('rawk','paeper'), "Invalid answer(s)" )
assert.equal(rockPaperScissors('rick','peaper'), "Invalid answer(s)" )
assert.equal(rockPaperScissors('rack','pappper'), "Invalid answer(s)" )
}
});
} else {
getPrompt();
}