Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Javascript question

Name: Anonymous 2009-02-16 15:21

Hi /prog/, due to some stupid thread on /g/, I just wrote a little Javascript thing to do ROT translations for any given degree (typically 13).  It works fine, but I feel like the way I deal with ASCII values that go "out of range" is inelegant.  Any suggestions?

Also, I've never posted here before, so I apologize if the code formatting is fucked up.


function translate(orig, degree) {
    var output = "";
    var x, y;
    for(var i = 0; i < orig.length; i++) {
        x = parseInt(orig.charCodeAt(i));
        //alert(x);
        if (65 <= x && x <= 90) {
            y = parseInt(orig.charCodeAt(i) + parseInt(degree));
            if (y > 90) {
                y -= 26;
            }
            output += String.fromCharCode(y);
        } else if (97 <= x && x <= 122) {
            y = parseInt(orig.charCodeAt(i) + parseInt(degree));
            if (y > 122) {
                y -= 26;
            }
            output += String.fromCharCode(y);
        } else {
            output += orig.charAt(i)
        }
    }
    return output;
}

Name: Anonymous 2009-02-16 15:36

function rot(text, rotation)
{
    var tmp = '', rotate = function(c, n) {
        return String.fromCharCode(n + ((c - n + rotation) % 26));
    };

    for (var i in text) {
        var c = text.charCodeAt(i);

        if (c >= 97 && c <= 122)
            tmp += rotate(c, 97);
        else if (c >= 65 && c <= 90)
            tmp += rotate(c, 65);
        else
            tmp += text[i];
    }

    return tmp;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List