How to Turn this If Statement into a Switch [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I would like to turn this if statement into a Switch but Im not sure how to go about doing it. Thanks for your help.
if ((VS_WRNG !== "") && ((VS_WRNG == "DSC") || (VS_WRNG == "DSQ")) && (VS_BTYP === "")) {
setValue('BOB_TYPE',"LIN");
VS_BTYP = "LIN";
}

If you must, this would be one possible formulation:
function x() {
switch (VS_WRNG) {
case "DSC":
case "DSQ":
if (VS_BTYP === "") {
setValue('BOB_TYPE', "LIN");
VS_BTYP = "LIN";
}
}
}

Related

Simplify if statement in JavaScript [closed]

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)

Shortening Javascript [closed]

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");
};

Code always showing same output (Problem with If statements in JavaScript) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I tried making a tool that calculates battle out comes but this part of the code always displays: "DRAW!"
function battle()
{
var rawpower = document.getElementById('rawpower').value;
var rawpoweropp = document.getElementById('rawpoweropp').value;
if(rawpower > rawpoweropp){
alert("You won!");
} else if(rawpower < rawpoweropp){
alert("You lose!");
} else{
alert("Draw!");
}
}
The element with id="rawpower" is a paragraph tag <p>. These elements do not have values. So document.getElementById('rawpower').value returns undefined, and same for the other line. undefined is not less than undefined, nor is it greater than undefined, so you're going into the third case.

If and else statement in Javascript is not working [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
want to run this javascript when there the button is clicked, if errors on the form check if statement and stop, if no errors execute the else statement......
$(document).ready(function () {
$('#continueToAttachments').on('click', function() { //on click button
if ($("#cevent").valid() = false) { // if errors stop the user from moving forward
return;
} else { // expecting this to run if there are no errors in the form
$('#Event-Information').removeClass('w--tab-active');
$('#Attachments').addClass('w--tab-active');
$('#Event-Information-Tab').removeClass('w--current');
$('#Attachments-Tab').addClass('w--current');
}
});
});
To check if a value is equal another you should use == or === to check if the variables are the same type too. = is for attribution.
if ($("#cevent").valid() == false)

Checking if checkbox is checked, display both booleans [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to determine whether the checkbox is checked or not, but when I push the checkbox, the javascript displays:
false
true
And when I push it again it changes to
true
false
And then it continues as I push further...
How can I only display one of those?
Here is my javascript code:
function selected() {
const bg = document.getElementById("myCheck").checked;
console.log(bg);
if (bg == 'true') {
document.getElementById("changeBG").style.backgroundColor = 'red';
} else {
document.getElementById("changeBG").style.backgroundColor = 'green';
}
}
This is incorrect:
if (bg == 'true') {
The checked property gives you a boolean, not a string. true is a string.
Just use:
if (bg) {

Categories