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 2 years ago.
Improve this question
I want to get only the value of "vp" from the query string only if it is between 0-4. If it is bigger (for example: 5, 12 etc.) I want to get undefined.
"?vp=324" // ==> undefined
"?vp=1&mn=345" // ==> 1
"?mn=345&vp=2" // ==> 2
"?sf=23&vp=12&fd=343" // ==> undefined
May or may not need to parseInt for this.
function smallVP(queryString) {
let params = new URLSearchParams(queryString);
let vp = parseInt(params.get("vp"), 10);
if (vp >= 0 && vp <= 4) {
return vp;
}
}
Related
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 have only one number like "100000"
I wan't to split this number same as [20000,20000,20000,20000,20000] in array
so if number is 138000 then it should be like [20000,20000,20000,20000,20000,20000,18000]
How to do this?
let num = 118000,
num_array = [];
while (num > 0) {
if (num >= 20000) {
num_array.push(20000);
num -= 20000;
} else {
num_array.push(num);
break;
}
}
console.log(num_array);
I think this is what you want, please post what you tried, so that it helps to understand where you failed.
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 5 years ago.
Improve this question
I'm not that good in javascript functions. Please help me understand below code.
var num = 5; // i have a variable 5
Add 10; //returns 15
Add 23; //returns 28
The question is, how to create a function that,
if add 10 it will return 15
if add 23 it will return 28
It seems you are looking for partial function in javascript which use the concept of closure.It is also known as currying-partials
var add = number(5);
function number(x) {
return function y(v) {
return x + v;
}
}
console.log(add(10));
console.log(add(23))
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 7 years ago.
Improve this question
I need to find an element that's text value is 0.
I do:
var d = $('.content').find('div');
if(d.text().length === 0){
//do something
}
Is there a way to put the above logic in the selector?
I've tried:
var d = $('.content').find('div:empty');
But no luck as the div may have other empty html tags in.
Use a filter
var elems = $('.content').find('div').filter(function() { return $(this).text() == 0 });
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
}
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 9 years ago.
Improve this question
returns the index where pattern starts in string (or -1 if not found).
The search is to be case sensitive if the 3rd parameter is true otherwise it is case insensitive.
Examples
index("abAB12","AB",true) returns 2 but index("abAB12","AB",false) returns 0
index("abAB12","BA",true) returns -1 and index("abAB12","BA",false) returns 1
Maybe something like:
var str = "abAB12";
var i = str.indexOf("AB");
if (i < str.length / 2) {
//stuff
} else {
//other stuff
}