Reputation: 407
I have a two functions:
first - takes 1 argument
secon - takes 2 arguments
Then I have third function that receives a function and a value as argument.
I am unable to figure out how to make the third function know that it is receiving one argument or two argument depending on which function is passed in 2nd argument???
**In the below example, when third is called it should return 2 on both calls at the end of code.
let val = 12;
let val2 = 14
let toC = 14;
// checks if val === 12 | takes 1 argument
const first = num => num === 12 ?2 :0;
// checks if val === toC | takes 2 arguments
const second = (num, check) => num === check ?2 :0;
// third function that receives a function and a val as arguments
const third = (func, num) => {
let temp = func(num);
return temp;
}
// this one works
console.log(third(first, val));
// this doesn't
console.log(third(second, (val2, toC)));
Upvotes: 1
Views: 23
Reputation: 6837
It looks like the third
function is there to just invoke the function provided as an argument and return the value. In this case, you can use the rest parameters.
let val = 12;
let val2 = 14
let toC = 14;
// checks if val === 12 | takes 1 argument
const first = num => num === 12 ? 2 :0;
// checks if val === toC | takes 2 arguments
const second = (num, check) => num === check ?2 :0;
const third = (func, ...args) => {
let temp = func(...args);
return temp;
}
console.log(third(first, val));
console.log(third(second, val2, toC));
Upvotes: 1