From 97f8f9a4599d438b9fc20153a9628f88f115348e Mon Sep 17 00:00:00 2001 From: Abby Gottlich Date: Thu, 18 Oct 2018 21:43:44 -0500 Subject: [PATCH 1/2] pigLatin Project --- 02week/pigLatin.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/02week/pigLatin.js b/02week/pigLatin.js index 046434c94..7c2818617 100644 --- a/02week/pigLatin.js +++ b/02week/pigLatin.js @@ -7,10 +7,39 @@ const rl = readline.createInterface({ output: process.stdout }); +// my code starts here + +// find first vowel of word: 'findFirstVowel' (for loop) + // if first vowel is first letter of word, add "yay" to end of word + // if first vowel is in another position: 'translateWord', + // remove all consinents before it (.split and .splice) + // add those consinents to the end of the word (.join) + // add "ay" to the end of all of that + +const findFirstVowel = (word) => { + for (let i = 0; i < word.length; i++) { + const charPosition = word[i]; + if(charPosition == 'a' || charPosition == 'e' || charPosition == 'i' || charPosition == 'o' || charPosition == 'u'){ + return i;} + } +} + +const translateWord = (word, vowelPosition) => { + if(vowelPosition == 0){ + return word + 'yay' + }else{ + const separateLetters = word.split(''); + const removeConsonants = separateLetters.splice(0, vowelPosition); + return separateLetters.join('') + removeConsonants.join('') + 'ay'; + } +} + function pigLatin(word) { // Your code here + const vowelPosition = findFirstVowel(word) + return translateWord(word, vowelPosition) } From 7fb48809416bfb5fc69104b4d8a567d0fedc8ad7 Mon Sep 17 00:00:00 2001 From: Abby Gottlich Date: Wed, 31 Oct 2018 22:07:56 -0500 Subject: [PATCH 2/2] added the '.includes' method to my conditional --- 02week/pigLatin.js | 78 +++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 42 deletions(-) diff --git a/02week/pigLatin.js b/02week/pigLatin.js index 7c2818617..2dc37d198 100644 --- a/02week/pigLatin.js +++ b/02week/pigLatin.js @@ -1,7 +1,7 @@ -'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 @@ -10,71 +10,65 @@ const rl = readline.createInterface({ // my code starts here // find first vowel of word: 'findFirstVowel' (for loop) - // if first vowel is first letter of word, add "yay" to end of word - // if first vowel is in another position: 'translateWord', - // remove all consinents before it (.split and .splice) - // add those consinents to the end of the word (.join) - // add "ay" to the end of all of that +// if first vowel is first letter of word, add "yay" to end of word +// if first vowel is in another position: 'translateWord', +// remove all consinents before it (.split and .splice) +// add those consinents to the end of the word (.join) +// add "ay" to the end of all of that -const findFirstVowel = (word) => { +const findFirstVowel = word => { for (let i = 0; i < word.length; i++) { const charPosition = word[i]; - if(charPosition == 'a' || charPosition == 'e' || charPosition == 'i' || charPosition == 'o' || charPosition == 'u'){ - return i;} + if (charPosition.includes("a", "e", "i", "o", "u")) { + return i; + } } -} +}; const translateWord = (word, vowelPosition) => { - if(vowelPosition == 0){ - return word + 'yay' - }else{ - const separateLetters = word.split(''); + if (vowelPosition == 0) { + return word + "yay"; + } else { + const separateLetters = word.split(""); const removeConsonants = separateLetters.splice(0, vowelPosition); - return separateLetters.join('') + removeConsonants.join('') + 'ay'; + return separateLetters.join("") + removeConsonants.join("") + "ay"; } -} - +}; function pigLatin(word) { - // Your code here - const vowelPosition = findFirstVowel(word) - return translateWord(word, vowelPosition) - + const vowelPosition = findFirstVowel(word); + return translateWord(word, vowelPosition); } - 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(); - }