Code Interview – Letter Changes – Code challenge – JavaScript Solution Source Code

Coderbyte
Letter Changes

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.

function LetterChanges(str) { 

  let charArr = str.split('');

  for (let i = 0; i < str.length; i++){
    
    if (charArr[i].match(/[a-z]/)){ ///[a-zA-Z]/ if uppercase in string to handle

      //hadle z to a case 
      if (charArr[i] === 'z' ){
        charArr[i] = 'A'; // A is vowel - uppercase right away or let followw logic to upper - remove cont.
        continue;
      }

      //change letter to next in alphabet
      charArr[i] = String.fromCharCode(charArr[i].charCodeAt(0) + 1 );

      //uppercase vowels
      if (charArr[i].match(/[aeiou]/gi)){
        charArr[i] = charArr[i].toUpperCase();
      }

    }
    
  }

  return charArr.join(''); 

}
   
// keep this function call here 
console.log(LetterChanges(readline()));

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *