if statement with more than one condition - javascript

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.

Related

issues with localStorage and saving values

I have a program where I have these strings being added to an array. However, there are many strings that are triggered by a certain condition, which can be met multiple times, however I only want it to be added on the first occurrence. So I have implemented a system where the event of adding the string to the array is triggered by the original condition, and a boolean expression. Here is an example of one of those conditions:
if (count >= 10 && displayMulti == true) {
consoleData.shift()
consoleData.push("Multi available")
displayMulti = false
window.localStorage.setItem("display_multi", String(displayMulti))
updateConsole()
}
When the string is added to the array, the boolean, displayMulti, is set to false so that it will not trigger again. However, upon refreshing the page, it will still trigger. I'm not sure why because I feel like I have saving the values to localstorage correctly. Code is below:
if (window.localStorage.getItem("display_multi") != null ) {
displayMulti = Boolean(window.localStorage.getItem("display_multi"))
} else {
console.log("here")
var displayMulti = true
}
There "here" console log statement is not triggered. So I have no idea why this would keep triggering because I don't see how the boolean is true. I've tested at like so many different points I genuinely have no idea what's wrong. I also don't think those values are affected anywhere else in my code. Any help is appreciated.
Here is a solution that properly parses your string as a boolean. Instead of Boolean(), a conditional (window.localStorage.getItem("display_multi") === 'true')(window.localStorage.getItem("display_multi") === 'true') is used.
if (window.localStorage.getItem("display_multi") != null ) {
displayMulti = (window.localStorage.getItem("display_multi") === 'true')
} else {
console.log("here")
var displayMulti = true
}
if (count >= 10 && displayMulti == true) {
consoleData.shift()
consoleData.push("Multi available")
displayMulti = false
window.localStorage.setItem("display_multi", String(displayMulti))
updateConsole()
}

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) ) {
...
}

What condition is false in a multiple conditions if statement

I'm writing a multiple if statement in Javascript. I've 3 (or more) conditions, and I wanna doSomething() only if all these 3 conditions are true. If only one of these 3 are false, I wanna doSomethingElse(). I think my code it's right, but my problem is on another level.
What if I wanna know for which condition my statement is false?
E.g.: condition1=true, condition2=true, condition3=false.
if (condition1 && condition2 && condition3) {
doSomething();
} else {
doSomethingElse();
};
I've thought that I can put another if statement in the else part.
if (condition1 && condition2 && condition3) {
doSomething();
} else {
if (condition1 == false) {
doWhatIDoWhenC1isFalse();
};
};
Is this the right way? Is there any other way to do this? Maybe faster way!
Thank for your help, and be nice to me, it's my second day on Javascript :)
Since the conditions are mutually exclusive, you can just use an else if without nesting.
if (condition1 && condition2 && condition3) {
doSomething();
} else if (!condition1) {
doWhatIDoWhenC1isFalse();
}
// add more else-if conditions as needed
If only one of your conditions can be false at a time (or if you don't care when two of them are false) then you can just have three else-if clauses and check each condition individually. If you do need to treat the cases where two conditions are false separately, you'll need an else-if for each combination. Pay close attention to the order you list them in if that's the case. The cases where you check if two conditions are both false should come before the cases where you only check one condition.
if (condition1 && condition2 && condition3) {
doSomething();
}else if (!condition1){
doWhatIDoWhenC1isFalse();
}else if (!condition2){
doWhatIDoWhenC2isFalse();
}else{
doWhatIDoWhenC3isFalse();
}
You have to do something along the lines of this. No way to cleanly get which expression that failed.
You may go this way if you want if any one of the condition is false:
if ((!condition1 && condition2 && condition3)||
(condition1 && !condition2 && condition3)||
(condition1 && condition2 && !condition3))
{
doSomethingElse();
} else {
doSomething();
};
It can be:
var x = []; /* an array that will take expressions number that result true */
if(condition1) x.push(1);
if(condition2) x.push(2);
if(condition3) x.push(3);
if( x.length == 2 ){ /* if two conditions got true */
doThingForTwoTrue();
}
if( x.length == 3 ){ /* if three conditions got true */
doThingForThreeTrue();
}
if( x.indexOf(1) !== -1 ){ /* if condition1 got true */
doThingOne();
}

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