Skip to content
Open
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
72 changes: 48 additions & 24 deletions 01week/rockPaperScissors.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,75 @@
'use strict';
"use strict";

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


function rockPaperScissors(hand1, hand2) {

// Write code here
hand1 = hand1.trim().toLowerCase();
hand2 = hand2.trim().toLowerCase();

if (hand1 === hand2) {
return "It's a tie!";
}

if (hand1 === "rock" && hand2 === "scissors") {
return "Hand one wins!";
}

if (hand1 === "rock" && hand2 === "paper") {
return "Hand two wins!";
}

if (hand1 === "paper" && hand2 === "rock") {
return "Hand one wins!";
}

if (hand1 === "paper" && hand2 === "scissors") {
return "Hand two wins!";
}

if (hand1 === "scissors" && hand2 === "paper") {
return "Hand one wins!";
}

if (hand1 === "scissors" && hand2 === "rock") {
return "Hand two wins!";
}
}

function getPrompt() {
rl.question('hand1: ', (answer1) => {
rl.question('hand2: ', (answer2) => {
console.log( rockPaperScissors(answer1, answer2) );
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!");
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('rock', 'scissors'), "Hand one wins!");
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!");
});
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("Paper", "SCISSORS"), "Hand two wins!");
assert.equal(rockPaperScissors("rock ", "sCiSsOrs"), "Hand one wins!");
});
});
} else {

getPrompt();

}