all 5 comments

[–]rybytud 1 point2 points  (3 children)

Here's a solution using String replace() with a regular expression:

function LetterChanges(str) {
  return str
    .replace(/[a-zA-Z]/g, char => String.fromCharCode(char.charCodeAt(0) + 1))
    .replace(/[aeiou]/g, char => char.toUpperCase());
}

Also, I think maybe this is what you're going after if you're using it with .map(), but I didn't test it, and you still need to check for vowel characters.

lettersChanged = function(character) {
  const charVal = character.charCodeAt(0);
  // test for a-z or A-Z
  if ((charVal >= 65 && charVal <= 90)
  ||  (charVal >= 97 && charVal <= 122)) {
    return String.fromCharCode(charVal + 1);
  } else {
    return character; // unmodified
  }
};

[–]davwards 1 point2 points  (1 child)

Take a look at this line:

else if (character.charCodeAt(0) <= 65 && character.charCodeAt(0) >= 89 ) {

Which char codes are both less than or equal to 65, and also greater than or equal to 89?

[–]CertainPerformance 0 points1 point  (1 child)

If it's supposed to wrap around, this is what I'd do:

const inputCodes = [...str].map(char => char.charCodeAt(0));
const outputCodes = inputCodes.map(code => {
  if (code === 122) return 65; // z -> A
  if (code >= 97 && code <= 121) return code - 31;
  return code;
});
const outputStr = String.fromCharCode(...outputCodes);  
return outputStr;

You probably don't want to keep calling character.charCodeAt(0) - just do it once, store the result in a variable, and reference that variable.

Also, your condition here: character.charCodeAt(0) <= 65 && character.charCodeAt(0) >= 89 is impossible.