strange javascript error

F

finecur

Hi,

In my javascript code I have the following code

var left, right;

left = ...
right = ..
if (right > left){
...
}else{
alert("left=" + left + " right=" + right + " right>left" + (right
}

I know in my code right should be larget than left. But the code goes
into the "else" block.
The message in the pop up window is :

left = 75 right=171 right>left=false

This is very strange. I know that must be something wrong in my code.
But I have now clue. Anyone know what kind of bug could trigger this?

Thanks,
fm
 
D

Doug Gunnoe

Hi,

In my javascript code I have the following code

var left, right;

left = ...
right = ..
if (right > left){
   ...}else{

    alert("left=" + left + " right=" + right + " right>left" + (right

}

I know in my code right should be larget than left. But the code goes
into the "else" block.
The message in the pop up window is :

left = 75 right=171 right>left=false

This is very strange. I know that must be something wrong in my code.
But I have now clue. Anyone know what kind of bug could trigger this?

Thanks,
fm

First, I need to know this, is ... > .. ?

Why do you assume that right should be greater than left?

Probably need to see a little more of your script.
 
D

David Mark

Hi,

In my javascript code I have the following code

var left, right;

left = ...
right = ..

Try posting a working example. You censored the most important parts
(e.g. the assignments.) Looks like you are dealing with strings to
me.

[snip]
 
L

Lasse Reichstein Nielsen

finecur said:
In my javascript code I have the following code

var left, right;

left = ...
right = ..
if (right > left){
...
}else{
alert("left=" + left + " right=" + right + " right>left" + (right
}

I know in my code right should be larget than left. But the code goes
into the "else" block.
The message in the pop up window is :

left = 75 right=171 right>left=false

(Incidentally, that's not the output of the code above - there is a "="
before the "false" that wasn't in the code!).
This is very strange. I know that must be something wrong in my code.

Very likely guess: You are comparing strings. It's easy to test:
alert([typeof left, typeof right]); // "string,string"
But I have now clue. Anyone know what kind of bug could trigger this?

String comparison: "171" < "75", lexically.
Remember to convert your inputs to numbers, e.g.:

var left = Number(...);
var right = Number(...);

/L
 
W

William James

finecur said:
Hi,

In my javascript code I have the following code

var left, right;

left = ...
right = ..
if (right > left){
...
}else{
alert("left=" + left + " right=" + right + " right>left" + (right
}

I know in my code right should be larget than left. But the code goes
into the "else" block.
The message in the pop up window is :

left = 75 right=171 right>left=false

This is very strange. I know that must be something wrong in my code.
But I have now clue. Anyone know what kind of bug could trigger this?

Using SpiderMonkey and jslibs:

LoadModule('jsstd') // Gives Print().

var left, right

left = "75"
right = "171"
if (right > left)
Print( "right > left\n" )
else
Print( "right isn't greater!\n" )
Print( left.toSource()," ",right.toSource(),"\n" )

left = 75
right = 171
if (right > left)
Print( "right > left\n" )
else
Print( "right isn't greater!\n" )
Print( left.toSource()," ",right.toSource(),"\n" )

--- output ---
right isn't greater!
(new String("75")) (new String("171"))
right > left
(new Number(75)) (new Number(171))
 
T

The Natural Philosopher

finecur said:
Hi,

In my javascript code I have the following code

var left, right;

left = ...
right = ..
if (right > left){
...
}else{
alert("left=" + left + " right=" + right + " right>left" + (right
}

I know in my code right should be larget than left. But the code goes
into the "else" block.
The message in the pop up window is :

left = 75 right=171 right>left=false

This is very strange. I know that must be something wrong in my code.
But I have now clue. Anyone know what kind of bug could trigger this?

Thanks,
fm

almost certainly its doing a string comparison.

171 is alphabetically less than 75.

I had a similar issue myself a few days ago.

I hate loosely typed languages.

Try if (Number(right) > Number(left))

If that works, find a more elegant way to achieve the same result.
 
K

Kenny

The said:
almost certainly its doing a string comparison.

171 is alphabetically less than 75.

I had a similar issue myself a few days ago.

I hate loosely typed languages.

Like Lisp?:

PQ(1): (< "171" "75")
Error: `"171"' is not of the expected type `REAL'
[condition type: TYPE-ERROR]

PQ(2): (string< 171 75)
Error: EXCL::STRING1 argument should be a string
[condition type: PROGRAM-ERROR]

Maybe blame operator overloading? Because even in C++ with strong static
typing you would get the same thing (assuming the string library
overloads <).

kt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,129
Messages
2,570,763
Members
47,324
Latest member
RicoBoxer

Latest Threads

Top