AnxiousMaster
AnxiousMaster

Reputation: 81

How to use the if else conditional loop?

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.

Upvotes: 0

Views: 43

Answers (2)

Arash Ghazi
Arash Ghazi

Reputation: 1001

  1. you can use this code
    let paw = {};
    if (nameu.trim() !== ''){
       paw.Title=nameu;
    }
    if (empidu.trim() !== ''){
       paw.EmpID = empidu;
    }
    paw = JSON.stringify(paw);

Upvotes: 0

A_Al3bad
A_Al3bad

Reputation: 114

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

Upvotes: 1

Related Questions