[Solved] How can I convert this short PHP function into JavaScript? [closed]


php syntax is pretty similar to JS.

Just take the variables and remove the $, and declare them with the var keyword.

function esn_to_num(esn) {
    var tmp = [];
    if ((tmp = explode('-', $esn))) {
        if (sizeof(tmp) == 2
            && my_isnum(tmp[0])
            && my_isnum(tmp[1])
        ) {
            esn = ((tmp[0] << 23) | tmp[1]);
        } else {
            esn = -1;
        }
    } else {
        esn = -1;
    }
    return esn;
}

Then you need to replace all your php functions with equivalent JS ones. Look up “javascript equivalents of explode, sizeof.

Follow the same process to rewrite my_isnum in javascript (clearly not a native php function).

edit: after seeing your update, I realize there is probably already a good chunk of code on Stack Overflow (or elsewhere) which will what my_isnum does. Either search “js function to determine if string is number” or ask that in a seperate question here.

You can do all this in chromes javascript console, and keep iterating until you stop getting errors. If you run into a specific problem and are stuck, thats a good point to ask about it on Stack Overflow.

solved How can I convert this short PHP function into JavaScript? [closed]