0

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?
Marco Altieri
  • 3,726
  • 2
  • 33
  • 47

5 Answers5

0

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) {
huypham
  • 184
  • 3
  • 8
0

= is assignment, == is used to check, change = to ==, === is used to check equality and type.

   if (checkBalance = true) {
Akhil Aravind
  • 5,741
  • 16
  • 35
0

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.

NocNit
  • 280
  • 1
  • 7
0

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");
    }
  }
}
huypham
  • 184
  • 3
  • 8
-1

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;
}
Andrew
  • 7,201
  • 5
  • 25
  • 34
  • I'm not sure if this code is correct. You use `handleIsActive()` when the account is both active and when it's not. Also, you retained `if(balance = 0)` which is an assignment inside an if statement. – NocNit May 27 '18 at 09:14
  • @NocNit Whoops, definitely made those mistakes. Thank you for poitning them out – Andrew May 27 '18 at 19:04