如果有条件的话,这个代码在javascript中有什么问题呢?

时间:2017-10-12 11:56:17

标签: javascript if-statement

当我运行此代码时,它总是给我相同的结果“你的余额是$ 250.00。” 我改变了变量以获得不同的答案,但我认为有些错误我不知道。 附件是描述代码和流程的流程图。enter image description here

enter code here

**// change the values of `balance`, `checkBalance`, and `isActive` to test 
your code
var balance = 250;
var checkBalance = false;
var isActive = true;
// your code goes here
if (checkBalance = false){
    console.log("thank you! Have a nice day.");
}
else if (isActive = true && balance > 0){
    console.log("your balance is $ " + balance.toFixed(2) + ".");
}
else if (isActive = false){
    console.log("Your account is no longer active.");
}
else if (balance = 0){
    console.log("Your account is empty.");
}
else if (balance < 0) {
    console.log("Your account is negative. Please, contact bank.");
}**

1 个答案:

答案 0 :(得分:2)

您需要在语句中使用比较(=====),而不是分配=。使用boolean类型,您可以发出===部分,并使用带或不带!的值。每个值都将转换为boolean类型,因此checkBalance === true仅相当于checkBalance,而checkBalance === false仅相当于!checkBalance

&#13;
&#13;
var balance = 250;
var checkBalance = false;
var isActive = true;

if (!checkBalance){
    console.log("thank you! Have a nice day.");
}
else if (isActive && balance > 0){
    console.log("your balance is $ " + balance.toFixed(2) + ".");
}
else if (!isActive){
    console.log("Your account is no longer active.");
}
else if (balance === 0){
    console.log("Your account is empty.");
}
else if (balance < 0) {
    console.log("Your account is negative. Please, contact bank.");
}
&#13;
&#13;
&#13;