Home > Software design >  How to use the if else conditional loop?
How to use the if else conditional loop?

Time:02-22

I need some help in this conditional code. In order to store the required data to the variable paw, I have three conditions.

  1. If nameu is not blank or empty and empidu is blank or empty, it should store the Title : nameu only.
  2. Else if nameu is blank or empty and empidu is not blank or empty, it should store the EmpID : empidu only.
  3. Else store both.

I have written the code myself, but it is not working. Here is the code :

var paw = "";
    if(nameu !== " ") {
       paw = JSON.stringify({
        Title: nameu
      });
    }
    else if(nameu === "" & empidu !== " ") {
       paw = JSON.stringify({
        EmpID: empidu
      });
    }
    else {
     paw = JSON.stringify({
      Title: nameu,
      EmpID: empidu,
    });
  }

Note : nameu (String) and empidu (number) are user input values

I think I am either writing wrong statements or logic. Please help me.

CodePudding user response:

You should use logical AND operator (&&) to evaluate multiple expressions instead of bitwise AND (&)

  • Related