Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically construct a condition for use in an if() statement

Tags:

javascript

Let's assume for a moment that I have something like this:

if (document.getElementById('div1').innerHTML &&     document.getElementById('div2').innerHTML &&     document.getElementById('div3').innerHTML &&     document.getElementById('div4').innerHTML &&     document.getElementById('div5').innerHTML &&     ...     document.getElementById('div100').innerHTML) 

Obviously typing out and maintaining a big conditional statement like this is problematic.

I would like some solution like:

var conditional = ""; for(var i = 1; i <= 100; i++){   conditional += "document.getElementById('div" + i +"').innerHTML";   if(i < 100) {     conditional += " && ";   } } if(interpretStringAsJSExpression(conditional)){     console.log("all my divs have content"); } 

Is something like this possible in JavaScript?

Great answers have been submitted, and I am sure that I and others will benefit from them. However, purely from a place of curiosity, is it possible to store and run JavaScript expressions or commands in strings?

Like I have proposed in my example: interpretStringAsJSExpression(conditional)

like image 578
WillD Avatar asked Dec 09 '18 03:12

WillD


People also ask

How do you write an if statement with condition?

When you combine each one of them with an IF statement, they read like this: AND – =IF(AND(Something is True, Something else is True), Value if True, Value if False) OR – =IF(OR(Something is True, Something else is True), Value if True, Value if False) NOT – =IF(NOT(Something is True), Value if True, Value if False)

Can we use in in if statement in Python?

When we use in with if statements, our code can tell if a string contains a substring, whether a list has a specific value, or if a dictionary has a certain key. Our if statements can also make decisions based on the opposite. We do that with not in .

How do you write if conditions in JavaScript?

In JavaScript we have the following conditional statements: Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.

How do you write a conditional code?

Conditional Code FormatThe statement begins with if followed by a condition in parentheses. After the condition, the code block is run. Code blocks are contained within curly braces { } . The else means "if the previous condition isn't met, do this instead," or more simply, "Otherwise."


2 Answers

You can do the tests in a loop.

var allOK = true; for (var i = 1; i <= 100; i++) {     if (!document.getElementById("div"+i).innerHTML) {         allOK = false;         break;     } } if (allOK) {     console.log("all my divs have content"); } 

You could also give all your DIVs a common class, then use a built-in iterator.

var allDivs = document.getElementsByClassName("divClass"); if (Array.from(allDivs).every(div => div.innerHTML)) {     console.log("all my divs have content"); } 
like image 99
Barmar Avatar answered Sep 29 '22 18:09

Barmar


As the other answers said, you can solve your conditions problem more easily.

But, to answer your new question

purely from a place of curiosity, is it possible to store and run JavaScript expressions or commands in strings?

Yes, you can write JavaScript code to a string and execute it later with eval. Which you should not do if you are concerned with security or performance.

like image 25
jorbuedo Avatar answered Sep 29 '22 20:09

jorbuedo