I'm writing a function to increase a string so, for example, "aac" becomes "aad" and "aaz" becomes "aba". The result is horribly inelegant, I can't get it simple enough and I feel I'm missing something. How to improve that code?
function inc_str(str){
var str = str.split("").map(function(a){ return a.charCodeAt(0); });
var n = 1;
while (n<str.length){
str[str.length-n]++;
if (str[str.length-n] > "z".charCodeAt(0))
str[str.length-n] = "a".charCodeAt(0),
++n;
else
break;
};
return str.map(function(a){ return String.fromCharCode(a); }).join("");
};
Extra points for a brief functional style solution! (Even if it needs to implement other functional functions such as zip or whatever.)