Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save adamsimonini/ca159df73cf2387407c3e0c95684efce to your computer and use it in GitHub Desktop.
Save adamsimonini/ca159df73cf2387407c3e0c95684efce to your computer and use it in GitHub Desktop.
Pig Latin Challenge
//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