user3791463
user3791463

Reputation: 33

Explanation for some simple code

I'm trying to understand a little piece of code. Can someone explain what exactly is going on with it. Is it shifting the string on even pieces to the right?

salt = '40cf738d702c78d8939da5e8cfd324ae';
str_overral = salt;
str_overral = str_overral.replace(/[^a-z0-9]/gi, '').toLowerCase();
str_res='';
for (i=0; i<str_overral.length; i++) {
    l=str_overral.substr(i,1);
    d=l.charCodeAt(0);
     if ( Math.floor(d/2) == d/2 ) {
       str_res+=l;
    } else {
       str_res=l+str_res;
    }
}

Upvotes: 0

Views: 38

Answers (1)

arhak
arhak

Reputation: 2542

the new string if built with even characters to the end/right (and I mean the character's code) while odd characters go to the beginning/left

salt = '40cf738d702c78d8939da5e8cfd324ae';
str_overral = salt;
str_overral = str_overral.replace(/[^a-z0-9]/gi, '').toLowerCase();
str_res='';
for (i=0; i<str_overral.length; i++) {
    l=str_overral.substr(i,1);
    d=l.charCodeAt(0);
    console.log('str['+i+']: "' + l + '" ('+d+')');
     if ( Math.floor(d/2) == d/2 ) {
       console.log('even, pushing to the end/right/back');
       str_res+=l;
    } else {
       console.log('odd, pushing to the beginning/left/front');
       str_res=l+str_res;
    }
    console.log('res: "' + str_res + '"');
}

Upvotes: 1

Related Questions