-1

I have topdatedata value as 222

I have these 3 conditions specified

if((topDateData<250)&&(topDateData>25)) 
{
alert('one');
}

else if((topDateData>300)&&(topDateData<300)) 
{
alert('Two');
}

else
{
alert('Three');
}

My questions is why is it getting the value as alert(3) and not alert(one)??

6
  • 1
    are you sure the value 222 you are passing is passed as a number, and not a string? Commented Jun 6, 2011 at 14:42
  • topDateData is an integer variable? Commented Jun 6, 2011 at 14:42
  • 2
    Is the variable called topdatedata or topDateData? There is a difference. Commented Jun 6, 2011 at 14:42
  • 1
    Assuming that topDateData is being set correctly, this should work. Can we see the code where topDateData is getting set? Commented Jun 6, 2011 at 14:43
  • 8
    BTW, the second case can never evaluate to true, since any real value x cannot simultaneously less than and greater than any other value y. Commented Jun 6, 2011 at 14:45

5 Answers 5

2

When explicitly setting the value to 222, I see 'one' get alerted: http://jsfiddle.net/Wvjfa/

You should debug your actual value ( alert(topDateData); if you like) and see if it really is what you think it is.

Beyond that, Matt Ball is right, your second condition is borked. Also lonesomeday and Kerry are right about your variable case not matching between your question and the posted code.

Sign up to request clarification or add additional context in comments.

3 Comments

@Matt - You your alternative is? You can't say something is bad without backing it up.
@Matt Oh, I'm quite aware. But he's already using it in his current code and this isn't a question about how to debug JS.
@matt_ball I agree with Ash, don't just say "that's not good" without actually telling what to do instead. If you nee d more then 500 chars, maybe a simple alert() is better for a question not related to JS debugging then that method?
1

Javascript is case sensitive, is topdatedata = 222 or topDateData = 222?

Comments

0

it's much safer just to set one value - as you're second criteria looks dodgy

var topDateData = 222;

if(topDateData > 300)
{
  alert('two');
}

else if(topDateData > 250)
{
  alert('');
}

else if(topDateData > 25)
{
  alert('One');
}

else
{
  alert('Three');
}

start with the one that's hardest to satisfy (in this example the highest) and then work your way down. It's much easier to follow. Other than that it should work, as per the other comments on here

Comments

0

My guess is that you have a string.

var trueBool = '222' < '250'; //true
var falseBool = '222' > '25'; //false

To debug if topDateData is a String or not, do the following:

alert(topDateData + 1);//outputs '2221' if a string, and '223' if a number

Here's a fiddle showing the difference.

UPDATE:

I've tested alert(('222' < 250) && ('222' > 25)); and that outputs true. However, I'm not sure all JavaScript compilers will be smart enough to convert the string to a number first. You should run the same test and see if true is the output on your browser.

6 Comments

I think practically all JS engines would properly use the numeric value of the string in this case, however you do bring up a good point. Since we cannot see the code which sets the topDateData value, it could be that he read it in in string format and tried to increment via + 1 or something.
@JAAulde Yeah, I think you're right. Bottom line is that it would be helpful if @Pavan did an alert to see what topDateData actually has in it before it's used. Until then, we're throwing darts blindfolded.
@JAAulde, this is an interesting point. Will standard JavaScript force a particular order of type coersion? For example, always try string->int before trying int->string? Or do it based on the order of operands (e.g. coerce the RHS to LHS first)?
Tried it on FireBug. Seems that string->int is always attempted first.
@Stephen - In your Firebug console, run "200" + 1 and you'll get "2001" (string). Also try 200 + "1" and you'll still get "2001" (string). So if someone reads a numeric value from form field (or any string source) and attempts some addition with it before converting it to int, an unexpected answer will arise.
|
0

It looks like your topDateData variable contains a string value "222" instead of an integer.

You could try to cast to an integer this way:

topDateData = parseInt(topDateData);

...

1 Comment

Even a string val of "222" should be fine for the first condition: jsfiddle.net/Wvjfa/1 Also, it is best to always specify the radix param to parseInt ( developer.mozilla.org/en/JavaScript/Reference/Global_Objects/… )

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.