Im doing the Letter Changes challenge in coderbyte but I'm confused as to why my code isnt working.
Have the function LetterChanges(str) take the str parameter being passed and modify it using the following algorithm.
Replace every letter in the string with the letter following it in the alphabet (ie. c becomes d, z becomes a).
Then capitalize every vowel in this new string (a, e, i, o, u) and finally return this modified string.
In the last else statement, i want to add any character that isnt a letter to the "newWord". When i pass the word "hello 3" I should be getting "ifmmp 3" instead I get "hhhhhhhihhhhhhhhhhhhhhhhhhheeeefeeeeeeeeeeeeeeeeeeeeeelllllllllllmllllllllllllllllllllllllllmllllllllllllllloooooooooooooopoooooooooooo 333333333333333333333333333undefined....." If i take that else statement out, I just get "ifmmp" without the 3. Im having trouble understanding why/how the else statement is messing it up and how could i fix this? Im new to coding so any help would be great.
function LetterChanges(str) {
var newWord = "";
var alphabet = "abcdefghijklmnopqrstuvwxyz";
for(var i = 0; i <= str.length; i++){
if( str[i] === "z"){
newWord += alphabet.charAt(0);
} else if(str[i] === " "){
newWord +=" ";
}
for (var j = 0; j <= alphabet.length; j++){
if(str[i] == alphabet.charAt(j)){
newWord += alphabet.charAt(j+1);
}
// this one -------------------> else {
newWord = newWord + str[i];
}
}
}
return newWord;
}
Aucun commentaire:
Enregistrer un commentaire