diff --git a/02week/pigLatin.html b/02week/pigLatin.html new file mode 100644 index 000000000..04538fee0 --- /dev/null +++ b/02week/pigLatin.html @@ -0,0 +1,21 @@ + + + + + + + PigLatin + + + +

Pig Latin Translator

+
+

Enter English Word:

+ + +
+
+

Your word in Pig Latin:

+

+ + diff --git a/02week/pigLatin.js b/02week/pigLatin.js index 046434c94..014a38c3e 100644 --- a/02week/pigLatin.js +++ b/02week/pigLatin.js @@ -1,51 +1,73 @@ -'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 detectVowel(word) { + let vowels = "aeiou"; + + for (let i = 0; i < word.length; i++) { + if (vowels.indexOf(word[i]) !== -1) { + return i; + } + } +} function pigLatin(word) { + word = word.toLowerCase().trim(); - // Your code here + let firstVowel = detectVowel(word); + if (firstVowel > 0) { + return word.slice(firstVowel) + word.slice(0, firstVowel) + "ay"; + } + return word + "yay"; } +function translateNow() { + var inputBox = document.getElementById("inputElement"); + + var englishWord = inputBox.value; + + var pigWord = pigLatin(englishWord); + + var nowYouSeeIt = document.getElementById("outputElement"); + + nowYouSeeIt.innerText = pigWord; +} function getPrompt() { - rl.question('word ', (answer) => { - console.log( pigLatin(answer) ); + rl.question("word ", answer => { + console.log(pigLatin(answer)); getPrompt(); }); } // Tests -if (typeof describe === 'function') { - - describe('#pigLatin()', () => { - it('should translate a simple word', () => { - assert.equal(pigLatin('car'), 'arcay'); - assert.equal(pigLatin('dog'), 'ogday'); +if (typeof describe === "function") { + describe("#pigLatin()", () => { + it("should translate a simple word", () => { + assert.equal(pigLatin("car"), "arcay"); + assert.equal(pigLatin("dog"), "ogday"); }); - it('should translate a complex word', () => { - assert.equal(pigLatin('create'), 'eatecray'); - assert.equal(pigLatin('valley'), 'alleyvay'); + it("should translate a complex word", () => { + assert.equal(pigLatin("create"), "eatecray"); + assert.equal(pigLatin("valley"), "alleyvay"); }); it('should attach "yay" if word begins with vowel', () => { - assert.equal(pigLatin('egg'), 'eggyay'); - assert.equal(pigLatin('emission'), 'emissionyay'); + assert.equal(pigLatin("egg"), "eggyay"); + assert.equal(pigLatin("emission"), "emissionyay"); }); - it('should lowercase and trim word before translation', () => { - assert.equal(pigLatin('HeLlO '), 'ellohay'); - assert.equal(pigLatin(' RoCkEt'), 'ocketray'); + it("should lowercase and trim word before translation", () => { + assert.equal(pigLatin("HeLlO "), "ellohay"); + assert.equal(pigLatin(" RoCkEt"), "ocketray"); }); }); } else { - getPrompt(); - } diff --git a/02week/tests.js b/02week/tests.js index e69de29bb..12ef1446a 100644 --- a/02week/tests.js +++ b/02week/tests.js @@ -0,0 +1,97 @@ +"use strict"; + +const assert = require("assert"); +const readline = require("readline"); +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + +const rockPaperScissors = (hand1, hand2) => { + hand1 = hand1.trim().toLowerCase(); + hand2 = hand2.trim().toLowerCase(); + + if (typeof hand1 !== "string" || typeof hand2 !== "string") { + return `Must enter "rock", "paper" or "scissors"`; + } else if ( + (hand1 !== "rock" && hand1 !== "paper" && hand1 !== "scissors") || + (hand2 !== "rock" && hand2 !== "paper" && hand2 !== "scissors") + ) { + return `Must enter "rock", "paper" or "scissors"`; + } else if (hand1 === hand2) { + return "It's a tie!"; + } else if (hand1 === "rock" && hand2 === "scissors") { + return `Hand 1 wins!`; + } else if (hand1 === "paper" && hand2 === "rock") { + return `Hand 1 wins!`; + } else if (hand1 === "scissors" && hand2 === "paper") { + return `Hand 1 wins!`; + } else if (hand1 === "scissors" && hand2 === "rock") { + return `Hand 2 wins!`; + } else if (hand1 === "rock" && hand2 === "paper") { + return `Hand 2 wins!`; + } else if (hand1 === "paper" && hand2 === "scissors") { + return `Hand 2 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 2 wins!`); + assert.equal(rockPaperScissors("paper", "scissors"), `Hand 2 wins!`); + assert.equal(rockPaperScissors("rock", "scissors"), `Hand 1 wins!`); + }); + it('should scrub input to ensure lowercase with "trim"ed whitepace', () => { + assert.equal(rockPaperScissors("rOcK", " paper "), `Hand 2 wins!`); + assert.equal(rockPaperScissors("Paper", "SCISSORS"), `Hand 2 wins!`); + assert.equal(rockPaperScissors("rock ", "sCiSsOrs"), `Hand 1 wins!`); + }); + it("should scrub input to only accept rock paper or scissors", () => { + assert.equal( + rockPaperScissors("hammer", " paper "), + `Must enter "rock", "paper" or "scissors"` + ); + assert.equal( + rockPaperScissors("Paper", "brick"), + `Must enter "rock", "paper" or "scissors"` + ); + assert.equal( + rockPaperScissors("ball ", "sCiSsOrs"), + `Must enter "rock", "paper" or "scissors"` + ); + }); + it("must enter string with no numbers", () => { + assert.equal( + rockPaperScissors("9776", " paper "), + `Must enter "rock", "paper" or "scissors"` + ); + assert.equal( + rockPaperScissors("Paper", "99999"), + `Must enter "rock", "paper" or "scissors"` + ); + assert.equal( + rockPaperScissors("4355", "sCiSsOrs"), + `Must enter "rock", "paper" or "scissors"` + ); + }); + }); +} else { + getPrompt(); +}