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

Pages: 1-

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:22

Use modulo. In case you're wondering, that's %.

Name: Anonymous 2009-02-16 15:23

what you want to do is a map spline lambda fork

Name: Anonymous 2009-02-16 15:24

>>3
Don't forget "CUDDER".

Name: Anonymous 2009-02-16 15:25

>>3
Don't forget "GRUNNER"

Name: Anonymous 2009-02-16 15:26

>>5
"GRUNNUR"

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;
}

Name: Anonymous 2009-02-16 15:37

>>2
>>7
Awesome, thanks to you both.

Name: Anonymous 2009-02-16 15:42

>>7
Incidentally, that doesn't quite work.

Name: Anonymous 2009-02-16 15:45

>>9
Yes it does.

Name: Anonymous 2009-02-16 15:49

>>10
That's my line.

Name: Anonymous 2009-02-16 16:03

>>11
Yeah--I'm >>1, >>8, >>9, and >>10, and I realized I was an idiot.  I just wasn't passing in the rotation argument as an integer.

Name: Anonymous 2009-02-17 2:49

>>12
Everything's a float in JavaScript

Name: Anonymous 2009-02-17 4:03

>>13
'' is not a float.
{} is not a float.
function(){} is not a float.

Name: Anonymous 2009-02-17 11:37

>>13
OP here.  Putting aside >>14 's examples...if that's true, why was JS using my "rotation" argument of 13 as a character value of "13", and thus doing the wrong calculation of 97 + "13" = 98?  Using parseInt fixed it.  Serious question, I'm not sure what you meant or how you think it applied.

Name: Anonymous 2009-02-17 11:42

im going to write a scripting language called megascript

Name: Anonymous 2009-02-17 11:57

>>15
You have an unhealthy obsession with ParseInt.

Name: Anonymous 2009-02-17 12:11

>>17
That may be true.  I think I learned that habit from examples in the O'Reilly book.

What's a better approach?

Name: Anonymous 2009-02-17 12:16

>>18
c

Name: Anonymous 2011-02-03 1:02

Name: Anonymous 2014-01-21 20:45

>>20
>le pedophile sage

Name: Anonymous 2014-02-08 8:15

check 'em

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