该代码用于根据收入( var income )选择联邦贫困水平( var fpl )百分比类别。当我运行这个脚本时,我的结果是"未定义"当我使用&& 逻辑运算符时。如果我使用 || 逻辑运算符,我会得到错误和相同的答案 - " 101-185%" - 无论我用什么数字 var income 。
-(IBAction)OkBtnAction:(id)sender
{
UIButton* btn=(UIButton*)sender;
NSLog(@"row: %ld",(long)btn.tag);
}
Else..If语句中的逻辑运算符是否在JavasScript中使用不同?
答案 0 :(得分:0)
if(result3>'1882.00'&& income<'2033.00')
“result3”确实未定义,请尝试“收入”
答案 1 :(得分:0)
您的income
值不属于您的任何if
语句(我想您想写if (income > /* instead of < */ "2033.00")
,因此fpl
仍未定义
答案 2 :(得分:0)
首先,让我们检查你的代码,因为你有一些错误,我会在它们的位置添加评论。
注意:在比较字符串时也要小心,例如"400" > "100000"
将返回true
。因此,如果要比较值,请在比较之前解析它们。
function FPL() {
var income = '4200.00';
var fs = '1';
//set a default value here to fpl or inside an else.
var fpl;
if(fs == '1') {
//on all these ifs, you dont have an else, then if your income doesnt fill your criteria, it will be undefined.
if(income < '1022.00')
fpl = "0-100%";
if (income > '1022.00' && income < '1882.00')
fpl = "101-185%";
if (result3 > '1882.00' && income < '2033.00') //result3 doesnt even exist, this will throw an error.
fpl = "186%-200%";
if (income < '2033.00')
fpl ="'201% & Over";
// if you return here, how do you expect the following code to execute?
// you just needed to continue the workflow
return fpl;
}
//this code is never executed, unless fs is different than 1 (but it is hardcoded)
result6 = 'Federal Poverty Level: ' + fpl;
document.getElementById("demo").innerHTML = result6;
}
这里你的代码没有错误并且已修复。
function FPL() {
//parse the value to get the number.
var income = parseFloat('4200.00');
var fs = '1';
var fpl;
if (fs == '1') {
if (income < parseFloat('1022.00'))
fpl = "0-100%";
else if (income > parseFloat('1022.00') && income < parseFloat('1882.00'))
fpl = "101-185%";
else if (income > parseFloat('1882.00') && income < parseFloat('2033.00'))
fpl = "186%-200%";
else if (income < parseFloat('2033.00'))
fpl = "'201% & Over";
else
fpl = 'default value';
}
result6 = 'Federal Poverty Level: ' + fpl;
document.getElementById("demo").innerHTML = result6;
}
FPL();
&#13;
<p id="demo"></p>
&#13;