lundi 5 avril 2021

Is there a way to know the value of a variable in an if statement without knowing what the variable is?

I am trying to find out the value of the second variable in the if statement without knowing what the variable is.

EX:

//a, b, and c are controlled by the user in input boxes.
let a = "Josh"
let b = "James"
let c = ""
if (a.length!== 0 && b.length !== 0|| a.length !== 0 && c.length !== 0){

Here I want to check if a equals the value that the length doesn't equal 0. In this case b.

Ex:

if (a == secondVariable){
alert("WOOH")
}

I know I can write

if (a.length !== 0 && b.length !== 0){
if (a == b){
alert("WOOH")
}
} else if (a.length !== 0 && c.length !== 0){
if (a == c){
alert("BOOH")
}
} else {
alert("")
}

But this is a lot of code and I have a lot more variables to check in my code. Is there a more efficient way to write this? Thank you

Aucun commentaire:

Enregistrer un commentaire