简体   繁体   English

If-else在JavaScript中不起作用

[英]If-else not working in JavaScript

var num1 = prompt("Enter first number: ", "number");
var num2 = prompt("Enter second number: ", "number");

if (num1 > num2) {

    document.write("<p>" + num1 + " is greater than " + num2 + "</p>");
} else {

    document.write("<p>" + num2 + " is greater than " + num1 + "</p>");
}

When I execute this code at that time it executes else part everytime so what is solution for it? 当我当时执行此代码时,它每次都会执行其他部分,那么解决方案是什么? Thank you so much in advance. 提前非常感谢您。

You are comparing strings (which is what prompt returns, even if the user types in digits). 您正在比较字符串(即使用户输入数字, prompt也会返回该字符串)。 Convert the values to numbers first: 首先将值转换为数字:

if(+num1 > +num2){

If the user enters any non-digits into the prompt s, a NaN will be produced and you may see weird results. 如果用户在prompt s中输入任何非数字,将生成NaN ,您可能会看到奇怪的结果。 I would set it up this way: 我会这样设置:

var num1 = +prompt("Enter first number: ", "number"),
    num2 = +prompt("Enter second number: ", "number");

if (!isNan(num1) && !isNaN(num2)) {
    if (num1 > num2) {

    } else if (num2 > num1) {

    } else {
        // Numbers equal
    }
} else {
    // At least one of them isn't a number
}

Javascript has a funky definition of true/false when you compare arbitrary variables. 比较任意变量时,JavaScript的真假定义为true / false。 You are going to want to make 100% sure that the input of the two parameters you are prompting for has been cast to a number like you would expect it to be: 您将要确保100%确保将要提示的两个参数的输入强制转换为一个数字,就像您期望的那样:

var num1 = parseInt(prompt("Enter first number: ", "number"));
var num2 = parseInt(prompt("Enter second number: ", "number"));

Using the + operator is great shorthand, but aside from my quick answer above you are probably going to want to add some checking to make sure they aren't giving you garbage. 使用+运算符是很好的捷径,但是除了上面我的快速回答之外,您可能还想添加一些检查以确保它们不会给您带来垃圾。

Also, be sure to check out Ian's answer - it contains a great explanation of the + operator and how it can be used for coercion. 另外,请务必查看Ian的答案-它包含+运算符的很好解释以及如何将其用于强制。

You need to parse the input (a string) into a number (an integer/float) before comparing. 在比较之前,您需要将输入(字符串)解析为数字(整数/浮点数)。

Use parseInt or parseFloat like: (respectively) 使用parseInt或parseFloat分别为:

var num1 = parseInt(prompt("Enter first number: ", "number"));

var num2 = parseFloat(prompt("Enter second number: ", "number"));

prompt returns a string, not number. prompt返回一个字符串,而不是数字。 You'll need to convert the results of prompt to a number before comparing. 在比较之前,您需要将提示的结果转换为数字。

var num1 = parseFloat(prompt("Enter first number: ", "number"));
var num2 = parseFloat(prompt("Enter second number: ", "number"));

prompt returns a String value, which needs to be parsed to a number. prompt返回一个String值,该值需要解析为一个数字。

In your case variables num1 and num2 are STRING. 在您的情况下,变量num1num2为STRING。 Before compare need to convert to INT type: 比较之前需要转换为INT类型:

 var num1 = parseInt(prompt("Enter first number: ", "number")); var num2 = parseInt(prompt("Enter second number: ", "number")); 

Tip on future: you can will get type of your's variable by typeof() 关于未来的提示:您可以通过typeof()获得变量的类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM