Quotes help make search much faster. Example: "Practice Makes Perfect"

Friday, February 1, 2013

Codecademy: Wrap It!



// 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'));

No comments:

Post a Comment


This is an example of scrolling text using Javascript.

Popular Posts