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))
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 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;
}
}
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 6 years ago.
Improve this question
I am a javascript beginner, and I do not quite understand this code in the picture, can somebody explain a little ?
Thanks!!
The easiest way to explain the code is to fill in the values during each call.
The function plusGenerator is a function that takes an offset and then returns another function that returns the result of adding offset to another number. When you call plusGenerator(2), the function that is returned looks like:
var addTwo = function(x) { return x + 2; };
You then call addTwo(15) which returns 17.
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 would like to write function that accepts 3 parameters and then returns area of the block. I've working on this for few days and I don't have answers. I need some help.
// We declare the function
var areaOfBlock = function(a,b,c) {
// we tell it to return to us what it gets
// when it multiplies a by b and then that answer
// by c
// could also be written like:
// var area = a * b *c;
// return area;
return a * b * c;
}
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 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'm taking online class to learn JS and I'm stuck. The quiz is asking me to:
Returns the cube of the sum of an input and 2.
So I coded this:
var returnCubed = function(num){ return (num + 2) * 3;};
But it doesn't pass. What else can I do to take in a parameter, add two to it, then cube it? Or am I not understanding the directions?
Try
var returnCubed = function(num){
return (num+2) * (num+2) * (num+2);
};
*3 will always multiply with 3.
Hope this can solve your problem.