Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 90 additions & 12 deletions 01week/rockPaperScissors.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,80 @@
'use strict';

const assert = require('assert');
const assert = require('assert').strict;
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});


function rockPaperScissors(hand1, hand2) {
function rockPaperScissors(a, b) {

// define variables for win states:
const tie = "It's a tie!";
const player1Wins = "Hand one wins!";
const player2Wins = "Hand two wins!";
// take the function's arguments (player choices) and make them lowercase so they still run even if a player capitalizes a letter:
let hand1 = a.toLowerCase().trim();
let hand2 = b.toLowerCase().trim();

// Write code here
// check if the choices are valid/create a boolean so it can be compared in the results stage:
function choiceChecker(hand1, hand2) {
switch(hand1){
// if player 1 picks a valid turn then break and move on to checking player 2, or else return that it's a false move:
case 'rock':
case 'paper':
case 'scissors':
break;
default:
return false;
}
// check player 2's choice and if both player's choices are valid then return true, if player 2's choice is not valid then return false:
switch(hand2){
case 'rock':
case 'paper':
case 'scissors':
return true;
default:
return false;
}
}
// define a boolean variable created from the validity checking function:
let isValid = choiceChecker(hand1, hand2);

// !!! Win States !!!
// if the choices aren't valid, say so:
if (!isValid) {
return 'Try again, choose either rock, paper, or scissors!';
}
// if the choices are the same, the game results in a tie:
else if (hand1 === hand2) {
return tie;
}
// or else if player one picks rock, determine winner based off player 2's choice:
else if (hand1 === 'rock') {
if (hand2 === 'paper') {
return player2Wins;
} else {
return player1Wins;
}
}
// or else if player 1 picks paper, determine winner based off player 2's choice:
else if (hand1 === 'paper') {
if(hand2 === 'rock') {
return player1Wins;
} else {
return player2Wins;
}
}
// finally, if player 1 picks scissors, determine winner based off player 2's choice:
else if (hand1 === 'scissors') {
if (hand2 === 'rock') {
return player2Wins;
} else {
return player1Wins;
}
}
}

function getPrompt() {
Expand All @@ -29,19 +92,34 @@ 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!");
assert.strictEqual(rockPaperScissors('rock', 'rock'), "It's a tie!");
assert.strictEqual(rockPaperScissors('paper', 'paper'), "It's a tie!");
assert.strictEqual(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('rock', 'scissors'), "Hand one wins!");
assert.strictEqual(rockPaperScissors('rock', 'paper'), "Hand two wins!");
assert.strictEqual(rockPaperScissors('paper', 'scissors'), "Hand two wins!");
assert.strictEqual(rockPaperScissors('rock', 'scissors'), "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!");
assert.strictEqual(rockPaperScissors('rOcK', ' paper '), "Hand two wins!");
assert.strictEqual(rockPaperScissors('Paper', 'SCISSORS'), "Hand two wins!");
assert.strictEqual(rockPaperScissors('rock ', 'sCiSsOrs'), "Hand one wins!");
});
it('should detect all scenarios in which hand one wins', () => {
assert.strictEqual(rockPaperScissors('rock', 'scissors'), 'Hand one wins!');
assert.strictEqual(rockPaperScissors('paper', 'rock'), 'Hand one wins!');
assert.strictEqual(rockPaperScissors('scissors', 'paper'), 'Hand one wins!');
});
it('should detect all scenarios in which hand two wins', () => {
assert.strictEqual(rockPaperScissors('rock', 'paper'), 'Hand two wins!');
assert.strictEqual(rockPaperScissors('paper', 'scissors'), 'Hand two wins!');
assert.strictEqual(rockPaperScissors('scissors', 'rock'), 'Hand two wins!');
});
it('should only accept valid inputs', () => {
assert.strictEqual(rockPaperScissors('wreck', 'paper'), 'Try again, choose either rock, paper, or scissors!');
assert.strictEqual(rockPaperScissors('rock', 'proper'), 'Try again, choose either rock, paper, or scissors!');
assert.strictEqual(rockPaperScissors('nothing', 'wrong'), 'Try again, choose either rock, paper, or scissors!');
});
});
} else {
Expand Down
86 changes: 43 additions & 43 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"http-server": "^0.11.1",
"javascripting": "^2.6.1",
"jsdom": "^11.6.2",
"mocha": "^5.0.0",
"mocha": "^5.2.0",
"postcss-html": "^0.34.0",
"stylelint": "^7.13.0"
}
Expand Down