1

These two Javascript functions are supposed to convert serial numbers (2-9999) for example into a number , but the functions below are not working for some reason .. they were originally written in PHP ... Works in PHP but not for Javascript.

<html>
<head>
<script type="text/javascript">

    function my_isnum(str, negative=false, decimal=false)
{
    var has_decimal = false;
    var len = strlen(str);
    if (len > 0) {
        var valid = true;
        for (var i=0; valid && i < len; i++) {
            if (!(str[i] >= "0" && str[i] <= "9")) {
                if (str[i] == "-") {
                    if (!negative || i != 0) {
                        valid = false;
                    }
                } else if (str[i] == ".") {
                    if (!decimal || has_decimal) {
                        valid = false;
                    }
                } else {
                    valid = false;
                }
            }
        }
    } else {
        valid = false;
    }
    return valid;
}

function esn_to_num(esn) {
    var tmp = [];
    if ((tmp = esn.split("-")) {
        if (tmp.length == 2
            && my_isnum(tmp[0])
            && my_isnum(tmp[1])
        ) {
            esn = ((tmp[0] << 23) | tmp[1]);
        } else {
            esn = -1;
        }
    } else {
        esn = -1;
    }
    return esn;
}

alert(2-9999);

</script> </head>
</html>

Original PHP functions

<?php
function my_isnum($str, $negative=false, $decimal=false)
{
    $has_decimal = false;
    $len = strlen($str);
    if ($len > 0) {
        $valid = true;
        for ($i=0; $valid && $i<$len; $i++) {
            if (!($str[$i] >= '0' && $str[$i] <= '9')) {
                if ($str[$i] == '-') {
                    if (!$negative || $i != 0) {
                        $valid = false;
                    }
                } else if ($str[$i] == '.') {
                    if (!$decimal || $has_decimal) {
                        $valid = false;
                    }
                } else {
                    $valid = false;
                }
            }
        }
    } else {
        $valid = false;
    }
    return $valid;
}

function esn_to_num($esn)
{

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




?>
2
  • Why don't you use something like ParseFloat ? Commented Feb 1, 2013 at 23:37
  • Didn't you like the answers here and here? Commented Feb 1, 2013 at 23:42

2 Answers 2

4

There is no such thing as strlen in Javascript. Use str.length instead.

Also, as Jason Sperske suggested below, change this:

function my_isnum(str, negative=false, decimal=false)

To this:

function my_isnum(str, negative, decimal)
{
    if (typeof negative == "undefined") negative = false;
    if (typeof decimal == "undefined") decimal = false;
    ....
}
Sign up to request clarification or add additional context in comments.

13 Comments

Also, function my_isnum(str, negative=false decimal=false) is not valid JavaScript
Yeah I was looking at that after I posted the answer, but indeed! (For anyone who doesn't see it: missing comma between false and decimal )
Sorry the mistake is larger than that, functions in JavaScript don't have default values like that
I KNEW IT! I didn't know for sure, so I looked it up, and the first page I opened said it did... Stupid me for not looking further than that. Internet mistake #1...
Wait wait wait... jsfiddle.net/M9KTN JabaScript is currently blowing my mind, what am I not understanding here?
|
3

These two javascript functions are supposed to convert serial numbers (2-9999) for example into a number.

Why not just get rid of the - and parse as a decimal number?

function padToFourDigits(_, digits) {
  return "0000".substring(digits.length) + digits;
}

function serialToNum(serialNumStr) {
  return +(serialNumStr.replace(/-(\d{1,4})/g, padToFourDigits));
}

Then

serialToNum('2-9999') === 29999

serialToNum('2-999')  === 20999

3 Comments

Yes, the OP got this advice earlier, but for some reason decided not to take it.
@Mike .. It's an assignment .. I wish I could just do it like that
@JBa Did the assignment tell you to do a line for line port of the code or just implement the same functionality in javascript? Personally if I had giving this assignment and you returned with the answer above I would think very highly of it, as you would have greatly simplified the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.