diff --git a/01week/rockPaperScissors.js b/01week/rockPaperScissors.js index 16f58790a..bae11ba91 100644 --- a/01week/rockPaperScissors.js +++ b/01week/rockPaperScissors.js @@ -1,6 +1,6 @@ 'use strict'; -const assert = require('assert'); +const assert = require('assert').strict; const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, @@ -8,10 +8,73 @@ const rl = readline.createInterface({ }); -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() { @@ -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 { diff --git a/package-lock.json b/package-lock.json index bd07c0e56..1c067a546 100644 --- a/package-lock.json +++ b/package-lock.json @@ -300,9 +300,9 @@ "integrity": "sha1-Ql1opY00R/AqBKqJQYf86K+Le44=" }, "browser-stdout": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.0.tgz", - "integrity": "sha1-81HTKWnTL6XXpVZxVCY9korjvR8=" + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/browser-stdout/-/browser-stdout-1.3.1.tgz", + "integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==" }, "browserslist": { "version": "1.7.7", @@ -581,6 +581,11 @@ "delayed-stream": "~1.0.0" } }, + "commander": { + "version": "2.15.1", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.15.1.tgz", + "integrity": "sha512-VlfT9F3V0v+jr4yxPc5gg9s62/fIVWsd2Bk2iD435um1NlGMYdVCq+MjcXnhYq2icNOizHr1kK+5TI6H0Hy0ag==" + }, "commandico": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/commandico/-/commandico-2.0.2.tgz", @@ -1627,6 +1632,11 @@ "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.11.tgz", "integrity": "sha1-Dovf5NHduIVNZOBOp8AOKgJuVlg=" }, + "growl": { + "version": "1.10.5", + "resolved": "https://registry.npmjs.org/growl/-/growl-1.10.5.tgz", + "integrity": "sha512-qBr4OuELkhPenW6goKVXiv47US3clb3/IbuWF9KNKEijAy9oeHxU9IgzjvJhHkUzhaj7rOUD7+YGWqUjLp5oSA==" + }, "har-schema": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/har-schema/-/har-schema-2.0.0.tgz", @@ -1690,6 +1700,11 @@ } } }, + "he": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/he/-/he-1.1.1.tgz", + "integrity": "sha1-k0EP0hsAlzUVH4howvJx80J+I/0=" + }, "hoek": { "version": "2.16.3", "resolved": "https://registry.npmjs.org/hoek/-/hoek-2.16.3.tgz", @@ -1728,15 +1743,14 @@ "version": "github:kevincolten/htmllint-cli#5901ac1f6cd612f484f40c7f4f7515f22a2ba52d", "from": "github:kevincolten/htmllint-cli", "requires": { - "bluebird": "^3.4.7", - "chalk": "^1.1.3", + "bluebird": "^3.5.1", + "chalk": "^2.3.0", "cjson": "^0.5.0", "glob": "^7.1.1", - "htmllint": "^0.6.0", - "liftoff": "^2.3.0", - "promise": "^7.1.1", - "semver": "^5.3.0", - "yargs": "^6.6.0" + "htmllint": "^0.7.0", + "liftoff": "^2.5.0", + "semver": "^5.4.1", + "yargs": "^10.0.3" }, "dependencies": { "ansi-styles": { @@ -2823,27 +2837,23 @@ "integrity": "sha1-3j5fiWHIjHh+4TaN+EmsRBPsqNc=" }, "mocha": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/mocha/-/mocha-5.0.0.tgz", - "integrity": "sha1-zMrJiLC8VHcRnLoOQ9569tatj04=", + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/mocha/-/mocha-5.2.0.tgz", + "integrity": "sha512-2IUgKDhc3J7Uug+FxMXuqIyYzH7gJjXECKe/w43IGgQHTSj3InJi+yAA7T24L9bQMRKiUEHxEX37G5JpVUGLcQ==", "requires": { - "browser-stdout": "1.3.0", - "commander": "2.11.0", + "browser-stdout": "1.3.1", + "commander": "2.15.1", "debug": "3.1.0", - "diff": "3.3.1", + "diff": "3.5.0", "escape-string-regexp": "1.0.5", "glob": "7.1.2", - "growl": "1.10.3", + "growl": "1.10.5", "he": "1.1.1", + "minimatch": "3.0.4", "mkdirp": "0.5.1", - "supports-color": "4.4.0" + "supports-color": "5.4.0" }, "dependencies": { - "commander": { - "version": "2.11.0", - "resolved": "https://registry.npmjs.org/commander/-/commander-2.11.0.tgz", - "integrity": "sha512-b0553uYA5YAEGgyYIGYROzKQ7X5RAqedkfjiZxwi0kL1g3bOaBNNZfYkzt/CL0umgD5wc9Jec2FbB98CjkMRvQ==" - }, "debug": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", @@ -2853,24 +2863,14 @@ } }, "diff": { - "version": "3.3.1", - "resolved": "https://registry.npmjs.org/diff/-/diff-3.3.1.tgz", - "integrity": "sha1-qoVnpu7QPFMfyJ0/cRzQ5SWd7HU=" - }, - "growl": { - "version": "1.10.3", - "resolved": "https://registry.npmjs.org/growl/-/growl-1.10.3.tgz", - "integrity": "sha512-hKlsbA5Vu3xsh1Cg3J7jSmX/WaW6A5oBeqzM88oNbCRQFz+zUaXm6yxS4RVytp1scBoJzSYl4YAEOQIt6O8V1Q==" + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-3.5.0.tgz", + "integrity": "sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==" }, "has-flag": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-2.0.0.tgz", - "integrity": "sha1-6CB68cx7MNRGzHC3NLXovhj4jVE=" - }, - "he": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/he/-/he-1.1.1.tgz", - "integrity": "sha1-k0EP0hsAlzUVH4howvJx80J+I/0=" + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", + "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=" }, "minimist": { "version": "0.0.8", @@ -2886,11 +2886,11 @@ } }, "supports-color": { - "version": "4.4.0", - "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-4.4.0.tgz", - "integrity": "sha1-iD992rwWUUKyphQn8zUt7RldGj4=", + "version": "5.4.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.4.0.tgz", + "integrity": "sha512-zjaXglF5nnWpsq470jSv6P9DwPvgLkuapYmfDm3JWOm0vkNTVF2tI4UrN2r6jH1qM/uc/WtxYY1hYoA2dOKj5w==", "requires": { - "has-flag": "^2.0.0" + "has-flag": "^3.0.0" } } } diff --git a/package.json b/package.json index aff08712c..59b08f4de 100644 --- a/package.json +++ b/package.json @@ -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" }