Javascript - 查找数字是正数还是负数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27294941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript - Find if number is positive or negative
提问by Pizzaman
I see other solutions to my question but none that help me.
我看到了我的问题的其他解决方案,但没有一个对我有帮助。
I want to create a function to find if a number is positive/negative. The function should take an integer argument and return true if the integer is positive and false if it is negative.
我想创建一个函数来查找数字是否为正/负。该函数应该接受一个整数参数,如果整数为正则返回真,如果为负则返回假。
Also, prompt the user again and again if anything other than a number is entered
此外,如果输入了数字以外的任何内容,请一遍又一遍地提示用户
Here's the code so far
这是到目前为止的代码
When I enter a number, it keeps alerting me it is true or false but won't let me enter another.
How do I control my loop so I can ask until -1
is entered? It is not giving me a chance to enter -1
当我输入一个数字时,它会不断提醒我这是真的还是假的,但不会让我输入另一个。我如何控制我的循环,以便我可以询问直到-1
输入?它没有给我输入 -1 的机会
function isPositive(num) {
var result;
if (num >= 0) {
result = true;
} else if (num < 0) {
result = false;
}
return result;
}
var num;
num = parseInt(prompt("Enter a number"));
while (num != -1) {
alert(isPositive(num));
if (isNaN(num)) {
alert("No number entered. Try again");
num = parseInt(prompt("Enter a number"));
isPositive(num);
while (num != -1) {
alert(isPositive(num));
}
}
}
回答by Niet the Dark Absol
There's a few things wrong with your code, so here's a rewrite with comments:
你的代码有一些问题,所以这里是一个带有注释的重写:
function isPositive(num) {
// if something is true return true; else return false is redundant.
return num >= 0;
}
// when you want to keep doing something until a condition is met,
// particularly with user input, consider a while(true) loop:
var num;
while (true) {
num = prompt("Enter a number");
// check for null here
if (num === null) {
alert("No number entered. Try again.");
continue; // return to the start of the loop
}
num = parseInt(num, 10); // second argument is NOT optional
if (isNaN(num)) {
alert("Invalid number entered. Try again.");
continue;
}
// once we have a valid result...
break;
}
// the loop will continue forever until the `break` is reached. Once here...
alert(isPositive(num));
回答by Praveen Kumar Purushothaman
The number 0
is neither positive, nor negative! :P
这个数字0
既不是正数也不是负数!:P
function isPositive(num)
{
if(num < 0)
return false;
else
return true;
}
Or a simple way,
或者简单的方法,
function isPositive(num)
{
return (num > 0);
}
回答by treeseal7
Math.sign(number)
which returns either a 1
, -1
or 0
返回 a 1
,-1
或0
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign
回答by DripDrop
You are testing if it isn't-1. Try this:
您正在测试它是否不是-1。试试这个:
if(num < 0){
...IS NEGATIVE...
}else{
...IS POSITIVE...
}
This checks if it is less than or greater than 0.
这将检查它是小于还是大于 0。