Skip to content

Instantly share code, notes, and snippets.

@YanivHaramati
Created January 12, 2015 05:40
Show Gist options
  • Save YanivHaramati/3531a3c365112e06560d to your computer and use it in GitHub Desktop.
Save YanivHaramati/3531a3c365112e06560d to your computer and use it in GitHub Desktop.
Letter Changes
// 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) {
var lowerStart = "a".charCodeAt(0);
var upperStart = "A".charCodeAt(0);
var letterCount = ("z".charCodeAt(0) - lowerStart) + 1; // 26;
var lowerUpperDiff = "a".charCodeAt(0) - upperStart; // 32
var vowels = "aioue";
var strArray = str.split('');
var valueMap = strArray.reduce(function (p, c) {
p[c] = c.charCodeAt(0);
return p;
}, {});
return strArray.map(function nextChar(c) {
var isLetter = /\w/g.test(c);
if (!isLetter) return c;
var isLower = /[a-z]/.test(c);
var start = (isLower) ? lowerStart : upperStart;
return String.fromCharCode(((valueMap[c] + 1 - start) % letterCount) + start);
})
.map(function lowerVowelToUpper(c) {
var isLetter = /\w/g.test(c);
var isVowel = vowels.indexOf(c) >= 0;
return (isLetter && isVowel)
? String.fromCharCode(c.charCodeAt(0) - lowerUpperDiff)
: c;
})
.join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment