From 09bc91b1d354fe39b1b2542c8724714824b72d63 Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Tue, 15 Jan 2019 18:30:49 -0600 Subject: [PATCH 01/10] started --- 02week/pigLatin.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/02week/pigLatin.js b/02week/pigLatin.js index 046434c94..08cccf2e8 100644 --- a/02week/pigLatin.js +++ b/02week/pigLatin.js @@ -8,9 +8,15 @@ const rl = readline.createInterface({ }); -function pigLatin(word) { +function pigLatin(a) { // Your code here + let word = a.toLowerCase().trim(); + let regex = /aeiou/gi; + if (word[0].match(regex)){ + word = word + 'yay'; + return word; + } } From 44492acebd3811d77115bae379bdc59405122bc2 Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Tue, 15 Jan 2019 23:22:02 -0600 Subject: [PATCH 02/10] did the assignment, tests are passing --- 02week/pigLatin.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/02week/pigLatin.js b/02week/pigLatin.js index 08cccf2e8..f87a54213 100644 --- a/02week/pigLatin.js +++ b/02week/pigLatin.js @@ -10,12 +10,22 @@ const rl = readline.createInterface({ function pigLatin(a) { - // Your code here + // make input lowercase and trim it: let word = a.toLowerCase().trim(); - let regex = /aeiou/gi; + // check if first letter is a vowel, and if so append 'yay' and return the new piglatin word: + let regex = /[aeiou]/gi; if (word[0].match(regex)){ word = word + 'yay'; return word; + } + // or if the word starts with a consonant(s) then split the word at the first vowel, move the beginning of the word to the end and append 'ay', then return the new piglatin word: + else { + // get the index number of the first vowel in the word: + let indexedVowel = word.indexOf(word.match(regex)[0]); + // make the new word equal to a substring of the input beginning at the index number of the first vowel, plus a substring of the input beginning at 0 which ends at the index number of the first vowel, plus 'ay': + word = word.substring(indexedVowel) + word.substring(0, indexedVowel) + 'ay'; + // return the new piglatin word!: + return word; } } From 0e63b0a3ebdadf0e35d77971acc4abcbcd15473a Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Tue, 15 Jan 2019 23:41:27 -0600 Subject: [PATCH 03/10] changed a few minor things & added more comments --- 02week/pigLatin.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/02week/pigLatin.js b/02week/pigLatin.js index f87a54213..0acaafde8 100644 --- a/02week/pigLatin.js +++ b/02week/pigLatin.js @@ -12,22 +12,20 @@ function pigLatin(a) { // make input lowercase and trim it: let word = a.toLowerCase().trim(); + // define search letters (vowels) as a variable: + let regex = /[aeiou]/g; // check if first letter is a vowel, and if so append 'yay' and return the new piglatin word: - let regex = /[aeiou]/gi; if (word[0].match(regex)){ - word = word + 'yay'; - return word; + // make piglatin word & return it: + return word = word + 'yay'; } // or if the word starts with a consonant(s) then split the word at the first vowel, move the beginning of the word to the end and append 'ay', then return the new piglatin word: else { // get the index number of the first vowel in the word: let indexedVowel = word.indexOf(word.match(regex)[0]); - // make the new word equal to a substring of the input beginning at the index number of the first vowel, plus a substring of the input beginning at 0 which ends at the index number of the first vowel, plus 'ay': - word = word.substring(indexedVowel) + word.substring(0, indexedVowel) + 'ay'; - // return the new piglatin word!: - return word; + // make the new word equal to a substring of the input beginning at the index number of the first vowel, plus a substring of the input beginning at 0 which ends at the index number of the first vowel, plus 'ay', then return it: + return word = word.substring(indexedVowel) + word.substring(0, indexedVowel) + 'ay'; } - } From 37f1fc8e77c05ef0ccafda923dca883c1868881c Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Tue, 15 Jan 2019 23:41:54 -0600 Subject: [PATCH 04/10] changed a spacing issue --- 02week/pigLatin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/02week/pigLatin.js b/02week/pigLatin.js index 0acaafde8..b1abcd10c 100644 --- a/02week/pigLatin.js +++ b/02week/pigLatin.js @@ -16,7 +16,7 @@ function pigLatin(a) { let regex = /[aeiou]/g; // check if first letter is a vowel, and if so append 'yay' and return the new piglatin word: if (word[0].match(regex)){ - // make piglatin word & return it: + // make piglatin word & return it: return word = word + 'yay'; } // or if the word starts with a consonant(s) then split the word at the first vowel, move the beginning of the word to the end and append 'ay', then return the new piglatin word: From 42329979ba9b47f0fec590dd56227f21efb7aaff Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Wed, 16 Jan 2019 15:17:49 -0600 Subject: [PATCH 05/10] checked if regex still works without global flag (it does) --- 02week/pigLatin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/02week/pigLatin.js b/02week/pigLatin.js index b1abcd10c..6e43b5030 100644 --- a/02week/pigLatin.js +++ b/02week/pigLatin.js @@ -13,7 +13,7 @@ function pigLatin(a) { // make input lowercase and trim it: let word = a.toLowerCase().trim(); // define search letters (vowels) as a variable: - let regex = /[aeiou]/g; + let regex = /[aeiou]/; // check if first letter is a vowel, and if so append 'yay' and return the new piglatin word: if (word[0].match(regex)){ // make piglatin word & return it: From 12c25cba1d66c2dabdf6625b8179a3ad24a08fd3 Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Wed, 16 Jan 2019 15:48:42 -0600 Subject: [PATCH 06/10] was curious if the [0] after match(regex) could be deleted since i removed the g flag from regex. it can --- 02week/pigLatin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/02week/pigLatin.js b/02week/pigLatin.js index 6e43b5030..fcabf12d4 100644 --- a/02week/pigLatin.js +++ b/02week/pigLatin.js @@ -22,7 +22,7 @@ function pigLatin(a) { // or if the word starts with a consonant(s) then split the word at the first vowel, move the beginning of the word to the end and append 'ay', then return the new piglatin word: else { // get the index number of the first vowel in the word: - let indexedVowel = word.indexOf(word.match(regex)[0]); + let indexedVowel = word.indexOf(word.match(regex)); // make the new word equal to a substring of the input beginning at the index number of the first vowel, plus a substring of the input beginning at 0 which ends at the index number of the first vowel, plus 'ay', then return it: return word = word.substring(indexedVowel) + word.substring(0, indexedVowel) + 'ay'; } From f178263320a6db3dc0b43125bd5b5c44cb8c25e9 Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Wed, 16 Jan 2019 18:52:18 -0600 Subject: [PATCH 07/10] redid assignment with for loops out of curiosity, left regex method intact but commented out --- 02week/pigLatin.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/02week/pigLatin.js b/02week/pigLatin.js index fcabf12d4..8824cc8c1 100644 --- a/02week/pigLatin.js +++ b/02week/pigLatin.js @@ -7,7 +7,29 @@ const rl = readline.createInterface({ output: process.stdout }); +function pigLatin(a) { + // make input lowercase and trim it: + let word = a.toLowerCase().trim(); + // define array containing vowels: + const vowels = ['a', 'e', 'i', 'o', 'u']; + // use a for loop to iterate through the letters of the word: + for ( let i = 0; i <= word.length; i++ ) { + //use a for loop to iterate through the vowels in the array: + for ( let v = 0; v <= vowels.length; v++ ) { + // if the first letter in the word is a vowel, append 'yay' to the word: + if ( word[0] == vowels[v] ) { + return word + 'yay'; + } + // or else check each letter in the word against each vowel in the array, if a letter is a vowel then create a substring based on its position within the word: + else if ( word[i] == vowels[v] ) { + return word = word.substring(i) + word.substring(0, i) + 'ay'; + } + } + } +} +// vvv !!! Alternate method with regex !!! vvv !!! vvv !!! Alternate method with regex !!! vvv +/* function pigLatin(a) { // make input lowercase and trim it: @@ -27,6 +49,7 @@ function pigLatin(a) { return word = word.substring(indexedVowel) + word.substring(0, indexedVowel) + 'ay'; } } +*/ function getPrompt() { From b0ab57a6c618d08f1c6554b3e59884e90d8f54b9 Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Wed, 16 Jan 2019 19:27:19 -0600 Subject: [PATCH 08/10] changed up the if/else to make it shorter --- 02week/pigLatin.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/02week/pigLatin.js b/02week/pigLatin.js index 8824cc8c1..7ac786c9f 100644 --- a/02week/pigLatin.js +++ b/02week/pigLatin.js @@ -17,13 +17,13 @@ function pigLatin(a) { //use a for loop to iterate through the vowels in the array: for ( let v = 0; v <= vowels.length; v++ ) { // if the first letter in the word is a vowel, append 'yay' to the word: - if ( word[0] == vowels[v] ) { - return word + 'yay'; + if ( word[0] !== vowels[v] ) { + if ( word[i] == vowels[v] ) { + return word = word.substring(i) + word.substring(0, i) + 'ay'; + } } // or else check each letter in the word against each vowel in the array, if a letter is a vowel then create a substring based on its position within the word: - else if ( word[i] == vowels[v] ) { - return word = word.substring(i) + word.substring(0, i) + 'ay'; - } + else return word + 'yay'; } } } From e3383b7e5f26b9db01774ed812ac22308247dfb8 Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Wed, 16 Jan 2019 19:32:32 -0600 Subject: [PATCH 09/10] updated comments to reflect previous if/else change --- 02week/pigLatin.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/02week/pigLatin.js b/02week/pigLatin.js index 7ac786c9f..d226c95de 100644 --- a/02week/pigLatin.js +++ b/02week/pigLatin.js @@ -16,13 +16,13 @@ function pigLatin(a) { for ( let i = 0; i <= word.length; i++ ) { //use a for loop to iterate through the vowels in the array: for ( let v = 0; v <= vowels.length; v++ ) { - // if the first letter in the word is a vowel, append 'yay' to the word: + // if the first letter in the word is not a vowel, find the first vowel by checking each letter of the input word against each vowel within the array: if ( word[0] !== vowels[v] ) { if ( word[i] == vowels[v] ) { return word = word.substring(i) + word.substring(0, i) + 'ay'; } } - // or else check each letter in the word against each vowel in the array, if a letter is a vowel then create a substring based on its position within the word: + // or else if the first letter in the word is a vowel: else return word + 'yay'; } } From cecc0d6d14c7fd9de773451d352d0a9d51046a2e Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Wed, 16 Jan 2019 21:32:41 -0600 Subject: [PATCH 10/10] added if statement to properly render words with 'qu' --- 02week/pigLatin.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/02week/pigLatin.js b/02week/pigLatin.js index d226c95de..38617f56e 100644 --- a/02week/pigLatin.js +++ b/02week/pigLatin.js @@ -18,7 +18,12 @@ function pigLatin(a) { for ( let v = 0; v <= vowels.length; v++ ) { // if the first letter in the word is not a vowel, find the first vowel by checking each letter of the input word against each vowel within the array: if ( word[0] !== vowels[v] ) { - if ( word[i] == vowels[v] ) { + // if the first vowel is 'u' check if it has a 'q' before it and if so split the word after the 'u': + if ( word[i] == 'u' && word[i - 1] == 'q') { + return word = word.substring(i+1) + word.substring(0, (i+1)) + 'ay'; + } + // otherwise just split the word at the first vowel and make the new pig latin word: + else if ( word[i] == vowels[v] ) { return word = word.substring(i) + word.substring(0, i) + 'ay'; } }