I'm doing a coderbyte challenge with these instructions:
Using the JavaScript language, 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.
My code does what the instructions say, but it doesn't retain the spaces of the original sentence.
function LetterChanges(str) {
var chars = str;
var newStr = [];
for (var i = 0; i < str.length; i++) {
if (/[a-y]/ig.test(chars[i])) {
newStr[i] = String.fromCharCode(chars[i].charCodeAt(0) + 1);
if (/[aeiou]/ig.test(newStr[i])) {
newStr[i] = newStr[i].toUpperCase();
}
} else if (/[z]/ig.test(chars[i])) {
newStr[i] = "A";
}
}
return newStr.join('');
}
LetterChanges("Argument goes here"); // result should be: BshvnfOUhpftIfsf
Is there a way to keep the spaces from the original string?
Add
else newStr[i]= chars[i];
To the end of the if/else
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments