Reputation: 81
I need some help in this conditional code.
In order to store the required data to the variable paw
, I have three conditions.
nameu
is not blank or empty and empidu
is blank or empty, it should store the Title : nameu
only.nameu
is blank or empty and empidu
is not blank or empty, it should store the EmpID : empidu
only.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
Reputation: 1001
let paw = {};
if (nameu.trim() !== ''){
paw.Title=nameu;
}
if (empidu.trim() !== ''){
paw.EmpID = empidu;
}
paw = JSON.stringify(paw);
Upvotes: 0
Reputation: 114
You should use logical AND operator (&&) to evaluate multiple expressions instead of bitwise AND (&)
Upvotes: 1