Reputation: 81
What I'm trying to do is write a function that modifies each of the words in a string into piglatin. For each of the words, it needs to change it depending on if it starts witha vowel or not. It needs to add way at the end of each word with a consonant, and each word that starts with a vowel, needs to have ay added to the end. Lastly, for every word, it needs to put the first letter of the word at the end before adding those sufixxes. Any advice?
function pigLatin(whatWeTitle) {
var alertThis = " ";
var whatWeTitle = document.getElementById("isLeaper").value;
var splitArray = whatWeTitle.split(" ");
var finalString = "";
for ( i = 0; i < splitArray.length; i++) {
finalString += splitArray[i] + "ay ";
}
alert(finalString);
}
Upvotes: 0
Views: 60
Reputation: 147413
This looked like a bit of fun, here's a function based on String.prototype.replace
, which can accept a function for the replacement:
function textToPig(t) {
var vowels = {a:'a',e:'e',i:'i',o:'o'};
return t.replace(/\w+/g, function(s){
var first = s.substring(0,1);
var rest = s.substring(1);
return rest + first + (first in vowels? 'ay' : 'way')
});
}
textToPig('foo bar each other ') // "oofway arbway acheay theroay "
Is that what you want? Be careful of hyphenated words.
Upvotes: 1