Hello I have been struggling to find a solution for my current issue. The problem lies within an exercise that requires changing only alphabetical characters, but test cases also include numbers and special characters which are being altered unintentionally.
Below is the code snippet I've been working with:
function LetterChanges(str) {
let newTxt = ""; // stores string after changing each character to the next in the alphabet
let newTxt2 = '';
//iterate through the string
for(let i=0;i<str.length;i++){
switch(str[i]){
case ' ':
break;
case 'z':
newTxt = 'a';
break;
case 'Z':
newTxt = 'A';
break;
default:
newTxt = newTxt.concat(String.fromCharCode(str.charAt(i).charCodeAt(0) + 1));
break;
}
//uppercase vowels
switch(newTxt.charAt(i)){
case 'a': case 'e': case 'i': case 'o': case 'u':
newTxt2 += newTxt.charAt(i);
break;
default:
newTxt2 += newTxt.charAt(i);
break;
}
}
str = newTxt;
return str;
}
In order to handle special characters or numbers without altering them, I am considering using if/else statements or nested switches based on regular expressions. This would ensure that only alphabetical characters are modified while others remain unchanged. Do you think this approach would be effective?
I'm relatively new to JavaScript and have spent hours searching Google for a suitable solution.
Thank you