Javascript statements all to be evaluated - javascript

Hi I want each if statements to be evaluated (java script). This is not happening, when one or two are true the remaining statements are not evaluated. They are all independent questions. What should I be using instead of if and else? Thanks.
//Side-Vent-Even
if (SideVent == "Side-Vent-Even" && Canvas=="Base-Shirt") {
// do this
}
//Side-Vent-Uneven
else if (SideVent2 == "Side-Vent-Uneven" && Canvas=="Base-Shirt") {
// do this
}
//Golf-Tee-Right
else if (GolfTee == "Golf-Tee-Right" && Canvas=="Base-Shirt") {
// do this
}
//Golf-Tee-Left
else if (GolfTee2 == "Golf-Tee-Left" && Canvas=="Base-Shirt") {
// do this
}

Turn them in to regular ifs, which will cause them all to be run independently of each other
if(){
}
....
if(){
}

If else are used when you wish to perform condition from either of them but not both or all, in your case you want all of them to be performed, hence use if condition statement for all, like
//Side-Vent-Even
if (SideVent == "Side-Vent-Even" && Canvas=="Base-Shirt") {
// do this
}
//Side-Vent-Uneven
if (SideVent2 == "Side-Vent-Uneven" && Canvas=="Base-Shirt") {
// do this
}

Related

Issue with nested if statement (within function) in Javascript

first time here and I've run into an issue... idk if it's my syntax or my logic is completely wrong...
var hasExperience;
var amountOfExperience;
function employed(hasExperience,amountOfExperience) {
if(hasExperience === true){
if(amountOfExperience > 5)
return true;
} else {
return false;
}
}
It doesn't seem to want to return false if the first two if statements aren't met... anyone can help?
Thanks!
If you need both of the if statements to evaluate to true, you should write it like this:
if(hasExperience === true && amountOfExperience > 5){
return true;
} else {
return false;
}
In your example, if(hasExperience === true) then you run the code inside the if block, else run the code inside the else block. It's important to understand that this is completely independent of what's inside the if block.
if(hasExperience === true){
// code inside if block
} else {
// code inside else block
}
The code inside the if block happens to be another if statement that will return true if(amountOfExperience > 5), and does nothing otherwise. Again, this is independent of the other if statement.
if(amountOfExperience > 5)
return true;
Using &&, means that both statements have to evaluate to true in order to execute the code inside of the if block. You can read more about this here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators
Also, as some others have stated, you can just write your function like this:
function employed(hasExperience,amountOfExperience) {
return hasExperience === true && amountOfExperience > 5;
}
Since your evaluating (hasExperience === true && amountOfExperience > 5) as a boolean and returning that boolean, you can avoid the if statement all together.
Try experimenting with this a little bit more to understand what's going on.
Update based on comment:
You could also accomplish this using the nested if, but this makes the code messy and difficult to read.
if (hasExperience === true) {
// only hasExperience is true
if (amountOfExperience > 5) {
// hasExperience is true and amountOfExperience is greater than 5
return true;
} else {
// hasExperience is false and amountOfExperience is less than 5
return false;
}
} else {
// hasExperience is false and we have no idea what amountOfExperience is
return false;
}

Writing multiple logical operators more concisely

I'm trying to write multiple logical operators in a more concise way. In my case, I want the function to only run when all four inputs are only numbers. The only way I can think of doing this is to write them all in one if statement with &&, use multiple ifs (as below), or use switch. But I was wondering if there is a more concise way.
function fn() {
const input = display.getInput();
if (input.p !== "" && !isNaN(input.p)) {
if (input.d !== "" && !isNaN(input.d)) {
if (input.s !== "" && !isNaN(input.s)) {
if (input.y !== "" && !isNaN(input.y)) {
if (input.y <= 100) {
/* run code */
}
}
}
}
}
}
To answer exactly what you area asking, you could do it like this:
if (input.p !== "" && !isNaN(input.p) && input.d !== "" && !isNaN(input.d) ...
But in fact you should write it better. First implement a validation function:
function isValid(property) {
return property !== "" && !isNaN(property);
}
So the if would be like:
if (isValid(input.p) && isValid(input.d) && ...
And finally, you might want to put everything into a new function:
function isEverythingValid(input) {
for (let property of ["p", "d", "s", "y"]) {
if (!isValid(input[property])) {
return false;
}
}
return input.y <= 100;
}

Check for undefined in one if condition with or

if(!parent[0] || parent[0].style.display !== 'none') {
console.log('1');
}
I am trying to do this in one line, but it is failing in Firefox giving Type Error that parent[0] is undefined. But here I am trying to check and just do the if loop if it is. So my only solution to break it in two if's like this:
if(!parent[0]) {
console.log('1');
return;
}
if(parent[0].style.display !== 'none') {
console.log('1');
}
How can I make it in one line? Why the first code fails and says that parent[0] is undefined, how can I prevent that and just do the console log which is inside.
This should do the work:
if(parent[0] && parent[0].style.display !== 'none') {
console.log('1');
}
Using someVariable && condition means that someVariable is defined and condition is met.
In my opinion the most readable code is:
function doSomething() {
// if condition is not met we quit the function
if(parent[0] === undefined) {
return false;
}
// parent[0] is defined we proceed
// Perhaps other conditions that should quit the function
if(parent[0].style.display === 'none') {
return false;
}
// parent[0] is defined we proceed and all checks are done
// All your logic here
}
Change the logic to &&.
if(parent[0] && parent[0].style.display !== 'none') {
console.log('1');
}
so I suggest you try:
"style" in parent[0] ? parent[0].style.display == "none" ? console.log(1) : 0 : 0;
Beware that if the display property is not set by javascript action, it will not reflect the state of the requested property. e.g.: you'll get an empty string instead.

JavaScript If string is equal to anything else

How do I check if a string is not equal to any of the other options? I know I can just do a simple else statement, but I'm not sure if it'll work since the code in my ifs aren't combined, yet they are just if() and else if() for its own, but then no else. How could I do a else statement/check if its anything else without using a switch/case statement?
Here's an example.
if(object1 === "string") {
function1();
}
if(object1 === "string2") {
function2();
if(object2 === "string" && variable === 10) {
function1();
}
if(object1 || object2 === "") {
alert("That's not a valid option!");
}
WARNING
I can't insert the code here as standalone or snippet, due to it exceeding the maximum character limit. I am only able to add a JSFIDDLE. Please excuse the inconvienence.
JSFIDDLE
The two main ways you can do it without if/else if/else being combined are a simple fall through
function foo(opt) {
if (opt === 'this') { return true; }
if (opt === 'that') { return 'something else'; }
/* Code here represents the case that opt is not this or that */
}
Another more complex way of doing it would be to have some kind of array of options and then checking them all but it's a bad pattern and you really should consider using if/else if/else as a combined tree (not sure you've really explained why you can't do this.
function foo(opt) {
var checked = ['this', 'that'];
if (opt === 'this') { return true; }
if (opt === 'that') { return 'something else'; }
if (checked.indexOf(opt) === -1) { return 'fall through case'; }
}
A switch is also possible but that's just a variation of a combined if/else if/else so if you can't use the latter I don't see how the former would help
Again the above is a bad pattern but if for some reason that's what you have to then there's an implementation.
Another solution would be to raise flags when going through one of the if statement, so that at the end, you can check those flags and if none are true, then you can do whatever you want.
var flag = false;
if(object1 === "string") {
function1();
flag = true;
}
if(object1 === "string2") {
function2();
flag = true;
if(object2 === "string" && variable === 10) {
function1();
flag = true;
}
if(object1 || object2 === "") {
alert("That's not a valid option!");
flag = true;
}
if(!flag) {
alert('no other options were validated');
}

Javascript conditional return statement (Shorthand if-else statement)

While writing shorthand if-else in javascript,getting syntax error. Here is my code:
data && data.cod == '404' && return;
Although works fine when I use normal if-else like below:
if(data && data.cod == '404') {return};
var temp = data && data.main && data.main.temp;
//Code here...
I know, it works fine if I use ternary operator like return (data && data.cod == '404')?'true':'false'; but I'm looking "return" on conditional basis otherwise continue further.
What you're trying to do is a violation of syntax rules.
The return keyword can only be used at the beginning of a return statement
In data && data.cod == '404' && <something>, the only thing you can place in <something> is an expression, not a statement. You can't put return there.
To return conditionally, use a proper if statement:
if(data && data.cod == '404') {
return;
}
I would recommend against using shortcuts like you're trying to do as a "clever" way to execute code with side effects. The purpose of the conditional operator and boolean operators is to produce a value:
Good:
var value = condition ? valueWhenTrue : valueWhenFalse;
Bad:
condition ? doSomething() : doSomethingElse();
You shouldn't be doing this, even if the language allows you to do so. That's not what the conditional operator is intended for, and it's confusing for people trying to make sense of your code.
Use a proper if statement for that. That's what it's for:
if (condition) {
doSomething();
} else {
doSomethingElse();
}
You can put it on one line if you really want to:
if (condition) { doSomething(); } else { doSomethingElse(); }
Well then just write the return in the if
var result = (data && data.cod == '404')
if (result) {
return result;
} else {
//otherwise
}

Categories

Resources