Jump to content

[question] storing condition in variable


newrehmi

Recommended Posts

var conditionz = i == 5;

Without the quotes, that actually works. The next version is a little easier to read but not more correct.

var conditionz = (i == 5);

Either way, conditionz now holds true or false. But if you are hoping that the value of conditionz will automatically be updated as the value of i changes, no, that won't happen.

Link to comment
Share on other sites

I was trying to guess what newrehmi's goal is. If it is simply to test a condition, there is no reason this code won't do the same thing:

if (i == 5){something happen;}

Because the substitution is childishly obvious, I'm assuming that is NOT what newrehmi means. So I'm making guesses at what newrehmi really does mean. Here is what won't work:

var i = 1000;var conditionz = (i == 5); // conditionz now equals falsei = 5;if (conditionz){   alert("Hello");   // conditionz STILL equals false   // so this code block will not execute}

Link to comment
Share on other sites

thanks so much for the quick answer btw,deirdre's dad's answer looks like what I am looking for. So, i'll just reveal more example to explain what am trying to achieve.

function functionName(){var valuez = document.getElementById('idName').valuevar conditionz = (i == valuez)if (conditionz){alert('the code is correct')}}

actually I am just unsure and afraid if the conditionz could not be read because it has been changed into "string".Thanks so much for the answer, it really helps.

Link to comment
Share on other sites

The result of a comparison is always a boolean true or false, never a string.
okay, then how if I rather just want to store the condition, not the value of either true or false. Can it be done? This is the example of the code usage:
function functionName(){var valuez = document.getElementById('idName').value;var conditionz = (i == valuez);for(i=0;i<=10;i++){if (conditionz){alert('the code is correct');continue;}document.write(i);}}

thanks so much

Link to comment
Share on other sites

WHY do you want to store "the condition"? Will it change? Why not if (i == valuez) . . . ?
If I succeed, I want to try to store more conditions in one IF. Let me add more example:
var valuez1 = document.getElementById('valuename1').valuevar valuez2 = document.getElementById('valuename2').valuevar valuez3 = document.getElementById('valuename3').valuevar conditionz = (i == valuez1 || i == valuez2 || i == valuez3)for (i=0;i<=10;i++){if (conditionz){continue;}document.write(i)}

actually, i will too try to make some looping script for valuez later, if I succeed...thanks so much for answering..edit:additional question, is there's any way to simplify this condition?

If (i == valuez1 || i == valuez2 || i == valuez3)

Link to comment
Share on other sites

I don't understand what benefit you think you get by putting the condition in a variable versus just putting it directly in the if statement. It goes in the if statement, just leave it there. It's easier to look at an if statement and see the condition there then it is to hunt down whatever variable the if statement is using.If you're searching for multiple values, you can store all of the values you're looking for in an array and then use the array.indexOf method to see if a certain value is in the array.

var values = [  document.getElementById('valuename1').value,  document.getElementById('valuename2').value,  document.getElementById('valuename3').value];for (i=0;i<=10;i++){  if (values.indexOf(i) != -1){	continue;  }  document.write(i)}

Link to comment
Share on other sites

If you want to reuse some logic in multiple places, you can encapsulate it in a function:

var is_equal = function(a, b) {	return a == b;}var i = 1;if (test_equality(i, 5)) do_something();if (test_equality(i, 6) || test_equality(i+1, 6)) do_more();

Of course, though, for something as simple as an equality test this is not very useful.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...