0

The following js script receives the variable "datum" from an external source and evaluates what type of data it is. Please, PLEASE tell me there's a more elegant way to write that particular code:

// declare global variable

var datumEval


// is-datum-a-string function

var stringCheck = new function()    {
    if (isNaN(datum)    {
        datumEval = "string";
    }
    else    {
        return;
    }
};


// is-datum-a-bool function

var boolCheck = new function()  {
    if (datum === true || datum === false)  {
        datumEval = "bool";
    }
    else    {
        return;
    }
};


// is-datum-negative function

var negCheck = new function()   {
    if (abs(datum) === datum)   {
        datumEval = "positive";
    }
    else    {
        datumEval = "negative";
    }
};


// is-datum-integer-or-float function

var floatCheck = new function() {
    if (Math.floor(datum) === datum)    {
        datumEval += " integer";
    }
    else    {
        datumEval += " float";
    }
};


// main code block

if (stringCheck(datum)) {
    return datumEval;
}
else if (boolCheck(datum))  {
    return datumEval;
}
else    {
    negCheck(datum);
    floatCheck(datum);
    return datumEval;
}
3
Contributors
6
Replies
7
Views
4 Years
Discussion Span
Last Post by diafol
0

1)The isNaN() function doesn't check for string but rather for not-a-number. Anything that is not a number is not equal to String.
2)Your boolean check is not valid. In JavaScript, it is tricky when it comes to boolean. Have you ever tried 0==false or 1==true? Both will return true. That's one of the trickiest part of JavaScript.
3)Your negative/positive check is also invalid. There is no abs() function but there is Math.abs() function. You should check whether the datum you are checking is a number before you attempt to check for negative/positive.
4)This integer/float checking could also be invalid. You need to check whether the datum is a number. Also, it isn't that useful to check whether a variable is a float or int in JavaScript. The reason is that JavaScript is a loosy data type. In other words, a variable data type can be changed at anytime in the script without raising an error. There isn't really a primitive datatype (such as int, long, float, double).

If you really want to check data type/class, you should use isNaN(), typeof(), or instanceof() instead. Yet, I still see no need of type checking that much because it is against the idea of the language.

Edited by Taywin

0

Why is checking for data types against the idea of the language?

Thanks for your responses, btw.

0

The hard work is done for you with functions like typeOf() - why do you need to re-invent the wheel? You can just run the function once and use conditionals to branch off your code as opposed to making multiple complicated tests. The RollYourOwn tests will probably not work properly (as pointed out).

Edited by diafol

0

You're absolutely right, Diafol. Thanks. Something like this?

var datumType = typeOf(datum);
switch { etc}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.