Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Variable number or array

Guide ,
Nov 16, 2009 Nov 16, 2009

I have a variable that could be a number or an array of 4 numbers to use. What I have is:

if (Number(mTBLR)) {

top = mTBLR;

bottom = mTBLR;

left = mTBLR;

right = mTBLR;

}

if (mTBLR.constructor == Array && mTBLR.length == 4) {

top = mTBLR[0];

bottom = mTBLR[1];

left = mTBLR[2];

right = mTBLR[3];

}

Is checking the constructor a good way to go I could not find much in my docs on this?

Also while on this is there a simple way to test the number is integer

TOPICS
Actions and scripting
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Guru ,
Nov 16, 2009 Nov 16, 2009

For core javascript objects, the Adobe scripting guides are not much help. I like http://www.w3schools.com/jsref/ and if that doesn't have what I'm looking for then Google.

To test for integers you could do lots of things. I would start with something like this

// make sure that it a number and an integer

if (mTBLR.constructor == Number && parseInt(mTBLR) ==mTBLR )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Nov 16, 2009 Nov 16, 2009

I would do it more like this:

if (mTBLR instanceof Array) {

   if (mTBLR.length == 4) {

    top = mTBLR[0];

    bottom = mTBLR[1];

    left = mTBLR[2];

    right = mTBLR[3];

} else {

   // it's an array but the length isn't 4

  }

} else if (Number(mTBLR)) {

    top = mTBLR;
   
bottom = mTBLR;
    left = mTBLR;
    right = mTBLR;
}

The only time you really need to do 'x.constructor' is when checking for Strings. String literals and String objects both have the constructor 'String'. However, literals are not proper objects and are, therefore, not an instance of the String class. But that's another rant...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 16, 2009 Nov 16, 2009

Google found me both 'constructor' &' instanceof' but google can also find stuff i've wrote and not always the greatest of examples.

So I thought I'd ask the pro's before getting myself into trouble

Thank you both

Midnight and Im still sat here trying to learn this stuff must be going mad

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Nov 16, 2009 Nov 16, 2009

xbytor2 wrote:

The only time you really need to do 'x.constructor' is when checking for Strings. String literals and String objects both have the constructor 'String'. However, literals are not proper objects and are, therefore, not an instance of the String class. But that's another rant..

You are right if he is sure that mTBLR can only be a Array or a Number, but I added it as a double check because '200px' or 2.35 would both test false with if(parseInt(mTBLR) == mTBLR) and it wouldn't be clear why. The first because parseInt returned NaN and the second because it's not an integer. I guess that if it doesn't matter why it failed then just if(parseInt(mTBLR) == mTBLR) would work.

But I am open to a better way to test for integer. Doesn't if(Number(2.75)) test true and if(Number(0)) test false?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Nov 16, 2009 Nov 16, 2009

Using the Number() function/constructor to test is incorrect unless your real condition is 'is this a valid non-zero number'. The correct test would be

if (!isNaN(mTBLR)) {

   //...

}

I also tend not to use parseInt. Either I know the entire string is supposed to be a number or I'm going to RegExp it to get the number-part out, then use Number for the conversion.

Also, parseInt("077") != Number("077").

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Nov 16, 2009 Nov 16, 2009

Sorry to be dim X, but doesn't !isNaN(7.98) test true. Mark wanted a test for integer values.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Nov 16, 2009 Nov 16, 2009

I'm the dim one today, Michael. I'm only half reading before I post stuff.

My test for an integer would be  !!x.toString().match(/^\d+$/); but the check in your post earlier would work as well.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Nov 16, 2009 Nov 16, 2009

Now I'm being nit picking, what about negative integers?

var mTBLR = -1;
alert((mTBLR.constructor == Number && parseInt(mTBLR) ==mTBLR ));
alert(!!x.toString().match(/^\d+$/));

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Nov 16, 2009 Nov 16, 2009

/[-+]?\d+/

🙂

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 17, 2009 Nov 17, 2009

Well thank you guys I was feeling a little thick that I could not see an obvious way to do this in JavaScript and your tests appear better thought thru than mine (thats why it's better to ask) I did try '!isNaN' with what Im used to this is easy. Its just sometimes the little things that can stop you in your tracks and have you scratching your head for the correct method.

set x to 1234.567

get class of x

-- returns real

set y to 1234567

get class of y

-- returns integer

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Nov 17, 2009 Nov 17, 2009

Muppet Mark wrote:

Its just sometimes the little things that can stop you in your tracks and have you scratching your head for the correct method

I agree and because it's something new you can be in for more head scratching if you are not careful. I know that I have done my share.

So the back and forth here is not about who is correct but what is correct test for a integer.

var mTBLR = "+20-10";
alert(!!mTBLR.toString().match(/[-+]?\d+/));

Even if you change the re to /^[-+]?\d+$/ gives a test for integers or correctly formatted string.

If there is a problem using parseInt in this case it can be replaced by one of several math methods like Math.round. Or the entire second expression can be replaced with !( mTBLR % 1) but you still need to make sure that mTBLR is not a string and those test can't do that because javascript is happy too convert a string to a number.

So I don't see a way to avoid x.constructor. I agree that both constructor and parseInt can be misused, but that doesn't mean they shouldn't never be used.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 17, 2009 Nov 17, 2009

There already has been and Im sure there will be plenty more head/desk banging going on. That's all part & parcel of the learning curve some are steeper than others the reason I posted was during one of my basic tests but I can't remember which now. Off the top of my head I think it was this…

#target photoshop

var x = [5]

if (isNaN(x)) {

alert('True');

}

else {

alert('False');

}

// returned false where I did NOT expect it too? Looks like [] were lost in translation but I did not know why…

set x to "123.456"

get class of x

-- returns "string" no attempt to convert unless I ask it to which some times is nice when you are at the bottom end of the curve

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Nov 17, 2009 Nov 17, 2009
LATEST

isNaN is a test for the special value NaN because normal == tests fail

var n = Number('r');// NaN
n == NaN;// false
isNaN(n);// true
isNaN(5);// false

So !isNaN(x) doesn't mean x is a number. It means it's not NaN.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines