JS function call [duplicate] - javascript

This question already has answers here:
Variadic curried sum function
(19 answers)
Currying in javascript for function with n parameters
(5 answers)
Closed 5 years ago.
How do I write a function which can be called several times in a row with one parameter? Here is an example:
function sum(a) {
// what should here?
}
sum(1)(2) // should return 3

Return another function that when called sums the argument of the first and the second like this:
function sum(a) {
return b => a + b
}
console.log(sum(1)(2))

Related

How can I use the setTimeout() statement? [duplicate]

This question already has answers here:
Why is the method executed immediately when I use setTimeout?
(8 answers)
How can I pass a parameter to a setTimeout() callback?
(29 answers)
What is the difference between a function call and function reference?
(6 answers)
Closed 3 months ago.
I was Coding Today (new) and i was wondering how people made js and python so i tried making a function based programming language using js
function init(){
log(console,"lmao ok")
setTimeout(log(console,'#--- Done'), 5000 )
}
function log(arg1,arg2) {
// body...
if (arg1 == console){
console.log(arg2)
}
else {
return
}
}
function prompt(arg1,arg2,arg3){
console.debug
var arg1 = "lmao"
}
init()
log(console,'tosay')

repeat function with parameter by settimeout inside itselft [duplicate]

This question already has answers here:
Why can I not pass a function call (rather than a function reference or an anonymous function) to setTimeout()?
(5 answers)
How to access the correct `this` inside a callback
(13 answers)
Closed 2 years ago.
I need to iterate a function with its argument but got the recursion error
Error in mounted hook: "InternalError: too much recursion"
the function:
methods: {
showSlides: function(n) {
console.log(n);
setTimeout(this.showSlides(n), 3000);
//setTimeout(this.showSlides(n++), 3000);
}
},

how double parantheses ()() workes in JavaScript? [duplicate]

This question already has answers here:
Two sets of parentheses after function call
(4 answers)
Closed 4 years ago.
Here i have a question regarding Double parentheses, can anyone please explain me how does it works.
function counter() {
console.log("hI");
return function (){
console.log("inside hI");
}
}
counter()();
In your example counter returns a function, so having parentheses after the call to counter calls the function that counter returns

Javascript shorthand which calls functions [duplicate]

This question already has answers here:
Using ternary operator in JavaScript to invoke two functions
(2 answers)
Closed 5 years ago.
I want a shorthand that runs a function if true and runs a separate function if false. Something like:
(condition)? function1:function2;
condition? function1():function2();
function a() {
return "a";
}
function b() {
return "b";
}
(1 == 2) ? alert(a()) : alert(b());

Deconstructing arrays for the function arguments [duplicate]

This question already has answers here:
Passing an array as a function parameter in JavaScript
(12 answers)
Closed 6 years ago.
I have
function f(a,b,c) {}
and
const arr = [1,2,3]
What do I need to call
f(arr)
__________________________________________?
One this you can do is:
f.apply({}, arr);

Categories

Resources