diff --git a/02week/pigLatin.js b/02week/pigLatin.js index 046434c94..0bbeb1a3f 100644 --- a/02week/pigLatin.js +++ b/02week/pigLatin.js @@ -3,49 +3,57 @@ const assert = require('assert'); const readline = require('readline'); const rl = readline.createInterface({ - input: process.stdin, - output: process.stdout + input: process.stdin, + output: process.stdout }); function pigLatin(word) { - - // Your code here - + word = word.toLowerCase().trim(); + if (word.charAt(0).match(/[aeiouy]/)) { + return word.concat('yay'); + } + return ( + word + .slice(word.search(/[aeiouy]/)) + .concat(word.slice(0, word.search(/[aeiouy]/))) + 'ay' + ); } +pigLatin('Irene'); + function getPrompt() { - rl.question('word ', (answer) => { - console.log( pigLatin(answer) ); - getPrompt(); - }); + 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'); + 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'); + }); }); - 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 { - getPrompt(); + getPrompt(); }