Shortening Javascript [closed] - javascript

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

Related

How to assign Logical Or operator in varaible [closed]

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

How to Turn this If Statement into a Switch [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 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";
}
}
}

Where to store return messages for javascript? [closed]

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.

Simple Javascript validation function [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 8 years ago.
Improve this question
I do not have any JavaScript experience and would like some assistance in creating a function as I am not sure on how to do it.
I would like to create a Function that validates that x between 1 and 17 it can also be equal to 1 and 17
If the value is not valid a simple messagebox/alert could be used to notify the user
I know this is a really stupid question but thank you in advance
A function like that would look something like:
function validateNumber(n) {
var ok = n >= 1 && n <= 17;
if (!ok) alert('The value is not valid. Values from 1 to 17 are allowed.');
return ok;
}
You can call it, and you get the status back if you want to use it for something:
if (validateNumber(x)) {
// the number was valid
} else {
// the number wasn't valid
}

Change the selector of a function in jquery [closed]

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 8 years ago.
Improve this question
I need some help. See if you can help me out.
I have a function whose form is:
$this.find(settings.selector).each(function(index) {
Anyway, depending on a variable can change the selector as follows:
$this.each(function(index) {
Can you give me some idea how to do it? I hope I have explained roughly.
Thank you.
You can use a temp variable like
var $els;
if (some_condition) {
$els = $this.find(settings.selector)
} else {
$els = $this;
}
$els.each(function (index) {});

Categories