In the article How to Build a Responsive Form with Filter Functionality Using HTML, CSS, and JavaScript I’m having trouble converting the ternary operator back to a standard if/else statement. It would help me understand the ternary operator if I could reverse engineer it so to speak.
share the code you are talking about, unfortunately the crystal ball is in maintenance
Here is the code…
where is the ternary operator?
What is the problem you are having?
rows[i].style.display =
language.toUpperCase().indexOf(value) > -1 ? "" : "none";
This is the condition:
language.toUpperCase().indexOf(value) > -1
These are the two values the ternary can evaluate to:
"" : "none"
This is the assignment of one of the two values:
rows[i].style.display =
Ternary:
const redOrGreen = Math.floor(Math.random() * 4);
document.documentElement.style.backgroundColor =
redOrGreen > 1 ? "red" : "green";
if/else
const redOrGreen = Math.floor(Math.random() * 4);
if (redOrGreen > 1) {
document.documentElement.style.backgroundColor = "red";
} else {
document.documentElement.style.backgroundColor = "green";
}
In the javascript. Please look at the javascript.
Instead of the ternary operator I would like to see the entire code with if/else statement. I can’t figure how to write the code with if/else. It would help me understand writing the code with the ternary operator
do you mean this?
rows[i].style.display =
language.toUpperCase().indexOf(value) > -1 ? "" : "none";
what have you written when trying to convert this to if/else?
never mind I figured it out.
if (language.toUpperCase().indexOf(value) > -1) { rows[i].style.display = "";} else { rows[i].style.display = "none";}