// Sample solution
// Paste the rotate function here:
String.prototype.rotate = function(n) { // n is an integer
n %= this.length; // too large numbers: modulo
var cut = n < 0 ? -n : this.length - n; // cutting point
return this.substr(cut) + this.substr(0,cut);
};
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var rot13_dict = {};
var newString = alphabet + "" + alphabet.toLowerCase();
var array1 = newString.split("");
var newString2 = alphabet.rotate(13) + "" + alphabet.rotate(13).toLowerCase();
var array2 = newString2.split("");
array1.forEach(function(element, index, array1){
rot13_dict[element]=array2[index];
});
//rot13_dict;
function rot13_map(somechar){
if(alphabet.indexOf(somechar)>=0 || alphabet.toLowerCase().indexOf(somechar)>=0){
return rot13_dict[somechar];
} else {
return somechar;
}
}
//console.log(rot13_map('?'));
//console.log(rot13_map('A'));
function rot13(somestring){
if(somestring.length>1){
return somestring.split("").map(function(n){ return rot13_map(n);}).join("");
} else {
return rot13_map(somestring);
}
}
No comments:
Post a Comment