Multiple bool var check inside condition - javascript

Is this ok way to check if all 4 variables have same value instead of checking each individually?
if (bodovi >= min_bodovi && (Q15,Q16,Q17,Q18) == true)

You can simplify that to:
if (bodovi >= min_bodovi && Q15 && Q16 && Q17 && Q18)
Given that these are boolean variables, their value being TRUE is sufficient for javascript to evaluate each as TRUE. If one of them id false, e.g. Q15 = false, then it will fail

Will validate only the last variable, here if Q18 is true then the condition will just pass even if Q15,Q16,Q17 are false or any one of them false.
(Q15,Q16,Q17,Q18) == true
You can use (Q15 && Q16 && Q17 && Q18)

Related

I am trying to get 3 conditions in one if . But it's not working

I need to match three conditions so that the page can show the appropriate result for that filters. I tried adding the && between three of the conditions but it doesn't work.
I am not revealing the real code here, but I've shown it without the original var names.
if(localStorage.getItem("eg1") == 30000 && eg2 == 1 && eg3 == use){};
eg2 and eg3 are just variables, they are not localstorage.
It should be:
if((localStorage.getItem("eg1") == 30000) && (eg2 == 1) &&
(eg3== use)){};
Here is a bit cleaner way to do it:
const a = ["eg1", "eg2", "eg3"].map(e => localStorage.getItem(e));
if (a[0] == 30000 && a[1] == 1 && a[2] == use) {
//
}
And a less dynamic but shorter way to do it:
if ([30000, 1, use].map((e, i) => e == localStorage.getItem("eg" + i++)).includes(false)) {
}
Try to decompose the problem in order to analyze it better:
var e1 = localStorage.getItem("eg1") === 30000;
var e2 = eg2 === 1;
var e3 = eg3 === use;
I've used triple equal since i suppose you don't want type coercion.
Since you can't show the original code, if you don't put some log of your results it's quite difficult to imagine what variables can contain with appropriate level of certainty.
Sure is that looking to your conditions independently should help you to identify where your guesses fall to fix what appears as a simple branch condition.
localStorage.getItem returns the value as string.
Try using if(localStorage.getItem("eg1") == "30000" && eg2 == 1 && eg3 == use){};

if statement with more than one condition

I need to check three conditions,
sheet_exists = 1
recalc = 1
qty_total and new_qty_total are not equal
The if statement works well if only the first 2 arguments are used:
if(sheet_exists === 1 && recalc === 'yes'){
//do something
}
But when I try to add he 3rd argument it fails, the actions in the if statement are ignored. I've tried:
if((sheet_exists === 1) && (recalc === 'yes') && (qty_total !== new_qty_total)){
//do something
}
And:
if(sheet_exists === 1 && recalc === 'yes' && (qty_total !== new_qty_total)){
//do something
}
And:
if(sheet_exists === 1 && recalc === 'yes' && qty_total !== new_qty_total){
//do something
}
Where am I going wrong?
Considering you are happy with the behavior of the first two conditions, and not with the last one, the problem must be in the last one.
Pay attention, qty_total !== new_qty_total will return TRUE only when the value or type of qty_total and new_qty_total are different.
If one is an integer 100 and the other is a string '100' then the condition evaluates as TRUE because they differ on the data type. But if they are both integers it will return FALSE, because neither the value nor the type are different.
To make sure the comparison works ok, make sure both variables are the same data type.

Execute If statement, only if element is existed

if($("#Prefix").val().trim()=="" && $("#Infix").val().trim()==""){
return false;
}
In the above code, when the element id Prefix or Infix does not exist, it's throwing undefined error
TypeError: $(...).val(...) is undefined
I know, this can be avoided by checking its length $("#Prefix").lenght>0 and $("#Infix").lenght>0.
My question here is, how can we do both checks inside single if statement itself.
try below code . check this link explain element length condition
if(($("#Prefix").length && $.trim($("#Prefix").val()) == "") && ($("#Infix").length && $.trim($("#Infix").val())=="")){
return false;
}
if (($("#Infix").lenght>0 && $("#Prefix").lenght>0) && ($("#Prefix").val().trim()=="" && $("#Infix").val().trim()=="")){
//your code here
}
Yes you can.
if statement with && operator stops checking farther when first 0 is returned. [0 && anything == 0].
So just check for the .length first.
if ( ($("#Infix").lenght && $("#Prefix").lenght) && (another conditions) ) {
...
}

checking values before submitting form

Before I submit a form I want to check the values in the input.
Here I'm checking if a value is NOt equal to .5 or 1. or not a empty string.
form.onsubmit = function(e) {
var ftimes = document.getElementsByClassName("add_timebox");
var fflag = 0;
for(i=0;i< ftimes.length;i++) {
var value1 = ftimes[i].value;
console.log(value1);
if ( value1 !==.5 ||value1 !== 1 || (!IsStringEmpty(value1)) ){
fflag = 1;
console.log('inside');
}
}
if(fflag==1) {
alert('enter again' );
return false;
}
I have made many changes to the IF statement to try to get it correct.
But it is still going in the loop even when I know if shouldn't.
For example when i submit the form and i have one input value equal .22
then it should only give me 1 'inside' but in keeps repeating:
inside
.22
(empty string)
inside
....
You do not show how you are implementing your IsStringEmpty method, but if you are using something like this, then any number is also a non-empty string, so your if statement will always run.
function IsStringEmpty(str) {
return (!str || 0 === str.length);
}
So you need to change your ORs with ANDs, or it will never check the number conditions.
You can check if the value is not an empty string and is different from 0.5 and 1. Then your condition should be like this.
if (!IsStringEmpty(value1) && value1 !== 0.5 && value1 !== 1)
But, you are getting the value from a form, so it will be a string. Therefore, you are comparing strings and you need this.
if (!IsStringEmpty(value1) && value1 !== ".5" && value1 !== "1")
Although you will probably want to compare floats, in which case you need this.
if (!IsStringEmpty(value1) && parseFloat(value1) !== .5 && parseFloat(value1) !== 1))
So basically, when you enter 1, .5 or and empty string in all of the form fields, you skip the inside block. But if you have any other value in any of the fields, then the flag will be set to 1. If that is not what you meant, please update your question to be more specific.
Please check Plunker here.
Hope this helps.
you have to add a break; statment in your if condition once the if condition is satisfied.
if ( value1 !==.5 ||value1 !== 1 || (!IsStringEmpty(value1)) ){
fflag = 1;
console.log('inside');
break;
}

jquery check variable values

In jQuery I have the following 4 variables.
var add
var city
var state
var zip
I need to check to see that any one of the above have a value.
If none have a value that is OK. If all of them have a value that is OK.
Just need to check that at least one of them do not have a value.
Not sure what is the most efficient way of doing this.
var check = [ add, city, state, zip ].every( function ( v ) { return !!v } )
Just for the sake of showing off.
Explaination: the every method loops through all the array and returns false if one of the conditions returns false and stops immediately the loop. If all the loops return true, true is returned.
PS: v is for "variable".
var check = (function(a, b, c, d) {
return !!a && !!b && !!c && !!d;
}(add, city, state, zip));
console.log(check);
another method... lets learn some new techniques today!
this will actually check to see if the value is not false. anything else is ok (strings, numerics, TRUE).
Simply
if (yourVar)
{
// if yourVar has value then true other wise false.
}
Hope thats what you required..
to check i a variable has a value assign it to it you can do:
var myVar
....
if (typeof myVar === 'undefined'){
// here goes your code if the variable doesn't have a value
}
if(!add || !city || !state || !zip) {
console.log('exists var with no value');
}
if( add.length == 0 || zip.length == 0 || city.length == 0 || state.length == 0) {
alert("at least one of the variables has no value");
}; else if (add.length == 0 & zip.length == 0 & city.length == 0 & state.length == 0) {
alert("all of the variables are empty");
}; else { alert("okay"); }

Categories

Resources