Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
Improve this question
How can I simplify this if statement in JavaScript?
if ((sessionStorage.getItem(EXTERNAL_SEARCH) === 'true') &&
((storageKey === KEY1) || (storageKey === KEY2))) {
sessionStorage.setItem(EXTERNAL_SEARCH, 'false');
return '';
}
Basically what I need is the following:
if(condition1 && (condition2 || condition3)){do something}
PS. Someone gave me two minus points for my question. I did my research and wrote the best condition I could but knew there is a way to optimize it and that is why I wrote it. If I could have optimized it myself, I wouldn't have posted it.
Your code is unreadable, you should store these expressions in variables and name them with something meaningful, e.g.:
let externalSearchEnabled = sessionStorage.getItem(EXTERNAL_SEARCH) === 'true'
let storageKeyIsValid = ((storageKey === KEY1) || (storageKey === KEY2))
if(externalSearchEnabled && storageKeyIsValid) {
// do something
}
The importance of doing this is to maintain the code more clean and readable, you should improve this answer to fit your project, name the variables with the most meaningful name.
Extra
I watched some lessons from Uncle Bob these days that blew my mind, for those who don't know who he is, he wrote the "Clean Code" book, search about him, he is a legend!
I will leave this link where he talks about clean code. I'm sure that will be a game-changer on your programming life
You can change this:
((storageKey === KEY1) || (storageKey === KEY2))
To this:
[KEY1, KEY2].includes(storageKey)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to assign || operator to variable.
var status;
if(this.state.selectedStatus === 'ALL'){
status = 'Working' || 'Left' #I need this how can we get this
}else{
status = this.state.selectedStatus
}
#It gives only 'working' output. But I need 'Working' || 'Left'
// For this
if(this.state.allEmployees[i].Status === status){
gEmployees.push({
value: this.state.allEmployees[i].Name,
id: this.state.allEmployees[i].EmployeeId
})
}
If you want to store an operator for use later, then it needs to be part of a function (and it is the function you store).
To solve this problem, however, just use an array:
status = ["Working", "Left"];
and make use of the includes method to find out if the Status matches one of the values in it.
if ( status.includes(this.state.allEmployees[i].Status) ) {
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I know there is probably a way of shortening this function with the new ES6 trying my best to work it out, could anyone shorten this so it still functions as it should? That way maybe I can see what I am doing wrong and why this isn't working....
Thank you!
userSchema.methods.isAdmin = function () {
let found = false
this.flags.forEach(
({type, flag}) => {
if (type == "UF" && flag == "ISADMIN") {
found = true
}
})
return found
}
You could take Array#some and return early.
userSchema.methods.isAdmin = function() {
return this.flags.some(({ type, flag }) => type === "UF" && flag === "ISADMIN");
};
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am using the Grasshopper app on my phone and I do not understand an example they gave me for if then statements.
They give you the solution because I answered incorrectly, but I do not understand why the solution given is correct.
var todayWeather = 'rainy';
var tommorrowWeather = 'cloudy';
if (todayWeather === 'rainy') {
print('Bring an umbrella');
}
if (todayWeather !== 'rainy') {
print('Maybe the sun will come out');
}
They say the correct answer is 'Bring an umbrella'. But why is this what this code will produce if it is run?
translating the code to english:
create a variable named todayWeather and set it to rainy
create a variable named tommorowWeather and set it to cloudy
if the variable todayWeather is rainy (true) then
print to the screen Bring an umbrella
(close if conditional)
if the variable todayWeather is not rainy (false, because it is set to rainy) then
print to the screen Maybe the sun will come out
(close code conditional)
Note the print statements only execute if the condition is met.
Also the variable values are never changed by this code after they are set.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Given some html:
<select id="this">
<option value="a"></option>
<option value="b"></option>
</select>
If your JS does different things depending on which option was selected, I'm wondering, how "strict" are you in the comparison? I.e., how paranoid are you? So there's a spectrum of options:
// OPTION A - not very paranoid
select_value = $("#this").val()
if (select_value == "a") {
...
} else {
...
}
// OPTION B - more paranoid, strict ===, but also has another else jus tin case
select_value = $("#this").val()
if (select_value === "a") {
...
} else if (select_value === "b") {
...
} else {
// some kind of exception handling, e.g., an error alert pops up, in case somehow user sneaks in another value...
}
Just asking because I realized I constantly default toward the most paranoid solution ever for everything, which is likely unhealthy for me and my code.
In the question's example there's no need for paranoia. == will sufice.
=== is used when you want to check equality based on value AND type of the var.
== allows for implicit conversion of the compared vars.
So '1' == 1 is true, '1' === 1 is false.
As you're just checking the select's value there's no need for ===
P.S.: Are you a Black Sabbath fan or something ? if yes leave the paranoias to Ozzy. He's good with them. :D
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Where should I store message types javascript function is returning? I can just return string messages, but it seems like a bad idea. Are there any best practices?
For example in this kind of function (of course real functions make more sense then this)
function isFooValid(foo){
if(foo.length < 4){
return "TOO_SHORT";
} else if(foo.length >100) {
return "TOO_LONG";
} else if(foo.charAt(1) == 'A'){
return "THERE_SHOULD_NOT_BE_SECOND_CHARACTER_A";
}
}
You can create a variable for that. For example you can have:
myMessages ={
"secondChar": "THERE_SHOULD_NOT_BE_SECOND_CHARACTER_A",
"tooShort": "TOO_SHORT"
}
Then you can retrieve them as myMessages.tooShort, for example.
Even, if you want to have something similar to the string resources in android, you can store all the strings in a json file. If you load it before you js script, you are going to have the myMessages var available.