diff --git a/02week/pigLatin.js b/02week/pigLatin.js index 046434c94..024a73ed7 100644 --- a/02week/pigLatin.js +++ b/02week/pigLatin.js @@ -11,41 +11,81 @@ const rl = readline.createInterface({ function pigLatin(word) { // Your code here + // Declare pigLatin as a function and 'str' as a placeholder for a given string + const pigLatin = word => { -} + // Make a list of vowels to test each letter of str against + let vowels = ['a', 'e', 'i', 'o', 'u']; + // Turn the given string into an array and put it into 'result' var + let result = word.split(''); -function getPrompt() { - rl.question('word ', (answer) => { - console.log( pigLatin(answer) ); - getPrompt(); - }); -} + // If the vowels array includes the first indexed letter of str + if (vowels.includes(word[0])) { -// Tests + // Return the given string with 'yay' added at the end + return word += 'yay'; -if (typeof describe === 'function') { + // The vowels array DOES NOT include the first indexed letter of str + } else { - 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 attach "yay" if word begins with vowel', () => { - assert.equal(pigLatin('egg'), 'eggyay'); - assert.equal(pigLatin('emission'), 'emissionyay'); + // Loop over each letter in the string given + for (let i = 0; i < word.length; i++) { + + // Using the iterator declared above, test each letter in str + // If the iterator DOES NOT include a letter in the vowel array + if (!vowels.includes(word[i])) { + + // Using str in array form, remove the first letter on the array, then add it to the end + result.push(result.shift()); + + // Once the iterator DOES match a letter in the vowel array + } else { + + // Add 'ay' to the end of the str + result.push('ay'); + + // Turn the rearranged str (var result) back into a string and return it + return result.join(''); + } + } + } + } + + + + + function getPrompt() { + rl.question('word ', (answer) => { + console.log(pigLatin(answer)); + getPrompt(); }); - it('should lowercase and trim word before translation', () => { - assert.equal(pigLatin('HeLlO '), 'ellohay'); - assert.equal(pigLatin(' RoCkEt'), 'ocketray'); + } + + // Tests + + 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 attach "yay" if word begins with vowel', () => { + 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'); + }); }); - }); -} else { + } else { - getPrompt(); + getPrompt(); -} + }