简体   繁体   English

Javascript-不止一种情况

[英]Javascript - More than one condition

I just started to learn JS and I want to ask about a task that I could not complete. 我刚开始学习JS,我想问一个我无法完成的任务。

var balance = 325.00;
var checkBalance = true;
var isActive = false;

//The first question
if (checkBalance = true) {
        // the second question
        if(isActive = false) {
            console.log("Your account is no longer active.");

        } else if(isActive = true) {

            if(balance > 0) {
                console.log("Your balance is " + balance.tofix(2) +"$.");
            } else if(balance = 0) {
                console.log("Your Accunt is empty");  
            } else {
                console.log("Your balance is negetive, please contant bank");
            }
        }
    else {
        console.log("Thank you. Have a nice day");

The goal is to write an ATM and in order to do that I want to write more than one condition in the same time (as you can see in the code) . 目的是编写一个ATM,为了做到这一点,我想同时编写多个条件(如代码中所示)

  1. Why this code doesn't work? 为什么此代码不起作用?
  2. Is it possible to write if statement inside another if statement? 是否可以在另一个if语句中写入if语句?
  3. Is there a better solution? 有更好的解决方案吗?

In javascript you should use === for condition equal your code: 在javascript中,应使用===表示条件等于您的代码:

if (checkBalance = true) {

correct is: 正确的是:

if (checkBalance === true) {

same for 一样

if(isActive = false) {

correct is: 正确的是:

if(isActive === false) {

=是分配,==用于检查,将=更改为==,===用于检查相等性和类型。

   if (checkBalance = true) {

You are on the right track, it is indeed possible to write an if statement inside another one. 您走在正确的道路上,确实有可能在另一个if statement编写if statement But, you're missing a bracket and the way you check equality should be done differently. 但是,您缺少括号,应该以不同的方式检查平等性。 I edited your code so I can explain: 我编辑了您的代码,因此可以解释:

var balance = 325.00;
var checkBalance = true;
var isActive = false;

//The first question
if (checkBalance) {
    // the second question
    if(isActive = false) {
        console.log("Your account is no longer active.");

    } else if(isActive) {

        if(balance > 0) {
            console.log("Your balance is " + balance.tofix(2) +"$.");
        } else if(balance === 0) {
            console.log("Your Accunt is empty");  
        } else {
            console.log("Your balance is negetive, please contant bank");
        }
    }
else {
    console.log("Thank you. Have a nice day");
}

I mainly changed 2 things in your code. 我主要在您的代码中更改了2件事。 The first is changing 首先是变化

if (checkBalance = true)

Into this: 变成这个:

if (checkBalance)

Edit 1: Shorter if statements 编辑1:较短的if语句

You can omit the = true part because checkBalance is already a boolean value. 您可以省略= true部分,因为checkBalance已经是一个布尔值。 This means that it is already true or false , which are values an if statement accepts. 这意味着它已经是truefalse ,它们是if statement接受的值。 This brings me onto my second edit, which is the most important one. 这使我进入了第二个编辑,这是最重要的一个。

Edit 2: Checking for equality 编辑2:检查是否相等

In your code, you use = true inside your if statements . 在代码中,在if statements使用= true Using only one = sign unfortunately is not checking for equality, but instead is your to assign values. 不幸的是,仅使用一个=符号不是检查是否相等,而是要分配值。 You can only use one = when your assigning values like var a = 1; 当您分配诸如var a = 1;类的值时,只能使用one = var a = 1; . Instead, you should use three = sings like === . 相反,您应该使用===类的三个= This actually checks two things. 这实际上检查了两件事。 First, it checks if the type of values are the same. 首先,它检查值的类型是否相同。 Then, it checks if the values are equal. 然后,它检查值是否相等。 You can also use two = sings like == , this will check equality more loosely because it doesn't check if the types are the same. 您还可以使用==类的两个= ,这将更加宽松地检查相等性,因为它不会检查类型是否相同。 As noted in other answers, === is preferable here. 如其他答案所述,此处===更好。

I hope this answers your question. 我希望这回答了你的问题。 If not, please comment below. 如果没有,请在下面评论。

First one: 第一:

= : is assign 
=== :is compare

One more thing wrong is : 还有一件事是错误的:

balance.tofix(2)

It should be: 它应该是:

balance.toFixed(2)

and I just edited your code like this: 我只是这样修改了您的代码:

var balance = 325.00;
var checkBalance = true;
var isActive = false;

//The first question
if(!checkBalance) console.log("Thank you. Have a nice day");
else {
  if(!isActive) console.log("Your account is no longer active.");
  else{
    if(balance > 0) {
      console.log("Your balance is " + balance.toFixed(2) +"$.");
    } else if(balance = 0) {
        console.log("Your Accunt is empty");  
    } else {
        console.log("Your balance is negetive, please contant bank");
    }
  }
}

You've been given good answers on the syntax errors. 您已获得有关语法错误的良好答案。 Regarding 'is there a better way', the need for equality checking of true and false using === is not necessary, because the true/false is already implied in the if condition. 关于“有没有更好的办法”,抽查的需要平等的truefalse使用===是没有必要的,因为真/假已经隐含了if条件。 You can use it as the variable by itself. 您可以单独将其用作变量。 Also, since it is a boolean that can only be true or false, using if and then else is totally fine to do. 另外,由于它是一个布尔值,只能为true或false,因此使用if and then else完全可以做到。 No need to do if and else if . 不需要, ifelse if

Using a helper function to handle your complex case makes your code much more readable. 使用辅助函数来处理复杂的案例可以使代码更具可读性。

var balance = 325.00;
var checkBalance = true;
var isActive = false;

//The first question
if (checkBalance) {
    // the second question
    if(isActive) {
        handleIsActive(balance);
    } else {
        console.log("Your account is no longer active.");
    }
} else {
    console.log("Thank you. Have a nice day");
}

function handleIsActive(balance) {
    if(balance > 0) {
        console.log("Your balance is " + balance.tofix(2) +"$.");
    } else if(balance === 0) {
        console.log("Your Accunt is empty");  
    } else {
        console.log("Your balance is negetive, please contant bank");
    }
    return;
}

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

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