2011-02-10 125 views
0

我想提示,然后比较两个值:JavaScript的比较失败

var x,y; 
x = prompt("enter the first value",""); 
x = prompt("enter the second value",""); 

if(x > y) 
{ 
    alert("x>y"); 
} 
else if(x < y) 
{ 
    alert("y>x") 
} 
else 
{ 
    alert("error"); 
} 

每次我跑这一点,alert("error")线被击中。我究竟做错了什么?

+0

你正在使用`x =`两次 – vol7ron 2011-02-10 00:59:22

回答

4

您还没有分配y

x=prompt("enter the first value",""); 
x=prompt("enter the second value",""); 

两个任务分配x

+0

哦不,谢谢你 – 2011-02-10 00:55:16

0

你的第二行应该设置y而不是x。

1

错字:

x=prompt("enter the first value",""); 
y=prompt("enter the second value",""); 
0
x=prompt("enter the first value",""); 
x=prompt("enter the second value",""); 

应该是:

x=prompt("enter the first value",""); 
y=prompt("enter the second value",""); 
0

也许你没有intentioanlly写X =两次?

0

您正在提示和分配x两次,因此yundefined

任何undefined将不被视为真实。

var x,y; 
x = prompt("enter the first value",""); 
y = prompt("enter the second value",""); 

if  (x > y) { alert("x>y"); } 
else if (x < y) { alert("y>x"); } 
else    { alert("error"); }