|
| 1 | +/*************************************************************************************** |
| 2 | +* * |
| 3 | +* CODERBYTE BEGINNER CHALLENGE * |
| 4 | +* * |
| 5 | +* Letter Changes * |
| 6 | +* Using the JavaScript language, have the function LetterChanges(str) take the str * |
| 7 | +* parameter being passed and modify it using the following algorithm. Replace every * |
| 8 | +* letter in the string with the letter following it in the alphabet * |
| 9 | +* (ie. c becomes d, z becomes a). Then capitalize every vowel in this new string * |
| 10 | +* (a, e, i, o, u) and finally return this modified string. * |
| 11 | +* * |
| 12 | +* SOLUTION * |
| 13 | +* You have to realize that the string passed in may contain items other than letters * |
| 14 | +* of the alphabet. If you find a character that is not a-z then just pass it along * |
| 15 | +* to the newStr as is without any modification. I am going to compare each letter * |
| 16 | +* in the string to the alphabet string. If the letter is found then I am going to * |
| 17 | +* return the next letter in the string unless the letter is z and them I am going * |
| 18 | +* to return a. When finished I am going to use a RegExp to replace all lower case * |
| 19 | +* vowels with upper case. |
| 20 | +* * |
| 21 | +* Steps for solution * |
| 22 | +* 1) Create alphabet string that contains all letters of the alphabet * |
| 23 | +* 2) Loop through each letter in the string * |
| 24 | +* 3) If letter is Z then return a * |
| 25 | +* 4) If letter is not a-z then return letter as is to newStr * |
| 26 | +* 5) If letter is a-z then return the next character in the alphabet string * |
| 27 | +* 6) Replace all vowels with upper case with a RegExp replace() function * |
| 28 | +* * |
| 29 | +***************************************************************************************/ |
| 30 | + |
| 31 | +function LetterChanges(str) { |
| 32 | + |
| 33 | + var alphabet = "abcdefghijklmnopqrstuvwxyz"; |
| 34 | + var newStr = ""; |
| 35 | + var loc; |
| 36 | + |
| 37 | + for (var i = 0; i < str.length; i++) { |
| 38 | + loc = alphabet.indexOf(str[i]); |
| 39 | + if (loc === 25) { |
| 40 | + newStr = newStr + "a"; |
| 41 | + } else if (loc === -1) { |
| 42 | + newStr = newStr + str[i]; |
| 43 | + } else { |
| 44 | + newStr = newStr + alphabet[loc + 1]; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return newStr.replace(/[aeiou]/g, function(letter) {return letter.toUpperCase()}); |
| 49 | + |
| 50 | +} |
0 commit comments