简体   繁体   中英

Javascript - More than one condition

I just started to learn JS and I want to ask about a task that I could not complete.

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) .

  1. Why this code doesn't work?
  2. Is it possible to write if statement inside another if statement?
  3. Is there a better solution?

In javascript you should use === for condition equal your code:

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. 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. The first is changing

if (checkBalance = true)

Into this:

if (checkBalance)

Edit 1: Shorter if statements

You can omit the = true part because checkBalance is already a boolean value. This means that it is already true or false , which are values an if statement accepts. This brings me onto my second edit, which is the most important one.

Edit 2: Checking for equality

In your code, you use = true inside your if statements . 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; . 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. 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. No need to do if and else 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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