Created
April 15, 2017 03:42
-
-
Save adamsimonini/ca159df73cf2387407c3e0c95684efce to your computer and use it in GitHub Desktop.
Pig Latin Challenge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//if the word starts with a vowel, so vowel test returns index 0, then simply add "way" to the end of the word | |
//grab the first consonant of str by checking where the first vowel resides | |
var finishedWord = ""; | |
var string = ""; | |
var a = ""; | |
var e = ""; | |
var i = ""; | |
var o = ""; | |
var u = ""; | |
var vowelPositionArray = []; | |
function minimumPosition(vowelPositionArray){ | |
return vowelPositionArray >= 0; | |
} | |
function vowelPosition(){ | |
a = string.indexOf("a"); | |
vowelPositionArray.push(a); | |
e = string.indexOf("e"); | |
vowelPositionArray.push(e); | |
i = string.indexOf("i"); | |
vowelPositionArray.push(i); | |
o = string.indexOf("o"); | |
vowelPositionArray.push(o); | |
u = string.indexOf("u"); | |
vowelPositionArray.push(u); | |
console.log(a); | |
console.log(e); | |
console.log(i); | |
console.log(o); | |
console.log(u); | |
console.log(vowelPositionArray); | |
var filteredPositions = vowelPositionArray.filter(minimumPosition); | |
var min = Math.min.apply(null, filteredPositions); | |
return min; | |
} | |
function isFirstVowel(firstCharacter){ | |
switch (firstCharacter){ | |
case "a": | |
case "e": | |
case "i": | |
case "o": | |
case "u": | |
console.log("It starts with a vowel"); | |
return true; | |
default: | |
console.log("Doesn't start with a vowel"); | |
} | |
} | |
function translatePigLatin(str) { | |
string = str; | |
console.log(string); | |
var firstCharacter = str[0]; | |
if(isFirstVowel(firstCharacter)){ | |
finishedWord = str + "ay"; | |
console.log(finishedWord = str + "way"); | |
return str + "way"; | |
}else{ | |
console.log("converting word staring with a consonant..."); | |
var firstVowelLocation = vowelPosition(string); | |
var firstChunk = string.substr(0, firstVowelLocation); | |
console.log("The first vowel appears at index " + firstVowelLocation); | |
console.log("The first bit is: " + firstChunk); | |
var remainingPieces = string.substr(firstVowelLocation, string.length - firstChunk.length); | |
console.log("The remaining pieces are: " + remainingPieces); | |
console.log("The Pig Latin version of " + str + " is " + remainingPieces + firstChunk + "ay"); | |
return remainingPieces + firstChunk + "ay"; | |
} | |
} | |
translatePigLatin("glove"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment