diff --git a/02week/exercises.js b/02week/exercises.js new file mode 100644 index 000000000..7b6f2a245 --- /dev/null +++ b/02week/exercises.js @@ -0,0 +1,46 @@ +// length +let cars = ['Ford', 'BMW', 'Audi', 'Toyota']; +console.log(cars.length); + +// conat +let moreCars = ['Buick', 'Mercedes', 'Lexus', "Honda"]; +let totalCars = cars.concat(moreCars); +console.log(totalCars); +// cant figure out how to rename the new array + +// indexOf and lastIndexOf +console.log(totalCars.indexOf('Honda')); +console.log(totalCars.lastIndexOf('Ford')); + +// join +let stringOfCars = totalCars.join(); +console.log(stringOfCars); + +// split +totalCars = stringOfCars.split(" "); +console.log(totalCars); + +// reverse +carsInReverse = totalCars.reverse(); +console.log(carsInReverse); +// cant get this one to reverse. still prints items in order + +// sort +carsInReverse.sort(); +console.log(carsInReverse); +// alert(carsInReverse.indexOf('Audi')); +// not working + +// slice + +// splice + +// push + +// pop + +// shift + +// unshift + +// forEach diff --git a/02week/tests.js b/02week/tests.js index e69de29bb..83fbd56d7 100644 --- a/02week/tests.js +++ b/02week/tests.js @@ -0,0 +1,86 @@ +'use strict'; + +const assert = require('assert'); +const readline = require('readline'); +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + + + +function rockPaperScissors(hand1, hand2) { + if (hand1 === hand2) { + return "It's a tie!" + } + if (hand1 === 'rock' && hand2 === 'paper') { + return "Hand2 wins!" + } + if (hand1 === 'rock' && hand2 === 'scissors') { + return "Hand1 wins!" + } + if (hand1 === 'paper' && hand2 === 'scissors') { + return "Hand2 wins!" + } + if (hand1 === 'paper' && hand2 === 'rock') { + return "Hand1 wins!" + } + if (hand1 === 'scissors' && hand2 === 'paper') { + return "Hand1 wins!" + } + if (hand1 === 'scissors' && hand2 === 'rock') { + return "Hand2 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('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!"); + }); + // my tests for checking which hand is the winner + it('should detect if hand1 won', () => { + assert.equal(rockPaperScissors('paper', 'rock'), "Hand one wins!"); + assert.equal(rockPaperScissors('rock', 'scissors'), "Hand one wins!"); + assert.equal(rockPaperScissors('scissors', 'paper'), "Hand one wins!"); + }); + it('should detect if hand2 won', () => { + assert.equal(rockPaperScissors('rock', 'paper'), "Hand two wins!"); + assert.equal(rockPaperScissors('scissors', 'rock'), "Hand two wins!"); + assert.equal(rockPaperScissors('paper', 'scissors'), "Hand two wins!"); + }); + }); +} else { + + getPrompt(); + +} \ No newline at end of file