Next.js Discord

Discord Forum

Help With higher order functions

Unanswered
Goldstripe sardinella posted this in #help-forum
Open in Discord
Goldstripe sardinellaOP
function addByX(x) {
  return (input) => input + x
}

const addByTwo = addByX(2);

function once(func) {
  let counter = 0
  let value = 0
  
  return () => {
    if (counter === 0) {
      value = func()
      return value
    } else {
      return value
    }
  }
}

const onceFunc = once(addByTwo);
console.log(onceFunc(4));  // => should log 6
console.log(onceFunc(10));  // => should log 6
console.log(onceFunc(9001));  // => should log 6

Hello everyone my name is hafeez and I have a question not specific to NEXT.js but about javascript in general. I am learning about higher order functions and I am stuck. Basically the premise is that i want to code a higher order function that only runs the callback function once and if the higher order function is called again with a different input into the callback function then it should just return the output value of the first callback function that was called. Anyways from what I have done so far i understand the problem is on the line value = func() as when i hardcode value = func(4) the higher order function works as intended. Can anyone help me figure out the label that is used to identify the callback function's input? I am pretty sure there is a legitimate term for this that if i googled i would be able to get the answer but i have been stuck with googling 'passing parameters into functions inside a higher order function' to no avail

5 Replies

@Goldstripe sardinella function addByX(x) { return (input) => input + x } const addByTwo = addByX(2); function once(func) { let counter = 0 let value = 0 return () => { if (counter === 0) { value = func() return value } else { return value } } } const onceFunc = once(addByTwo); console.log(onceFunc(4)); // => should log 6 console.log(onceFunc(10)); // => should log 6 console.log(onceFunc(9001)); // => should log 6 Hello everyone my name is hafeez and I have a question not specific to NEXT.js but about javascript in general. I am learning about higher order functions and I am stuck. Basically the premise is that i want to code a higher order function that only runs the callback function once and if the higher order function is called again with a different input into the callback function then it should just return the output value of the first callback function that was called. Anyways from what I have done so far i understand the problem is on the line `value = func()` as when i hardcode `value = func(4)` the higher order function works as intended. Can anyone help me figure out the label that is used to identify the callback function's input? I am pretty sure there is a legitimate term for this that if i googled i would be able to get the answer but i have been stuck with googling 'passing parameters into functions inside a higher order function' to no avail
you are never setting counter to something else so the first condition (counter === 0) is always running, which evaluates the function on every call
Goldstripe sardinellaOP
function addByX(x) {
  return (input) => input + x
}

const addByTwo = addByX(2);

function once(func) {
  let counter = 0
  let value = 0
  
  return () => {
    if (counter === 0) {
      value = func()
      counter ++
      return value
    } else {
      return value
    }
  }
}

const onceFunc = once(addByTwo);
console.log(onceFunc(4));  // => should log 6
console.log(onceFunc(10));  // => should log 6
console.log(onceFunc(9001));  // => should log 6

Hey thanks for pointing out the bug. However even with the updated code shown above, the output i receive is still NaN
Goldstripe sardinellaOP
The main problem i believe is the underlined portion. When i hardcode the input into the higher order function, i get the expected output. However i need the input to be dynamic. I feel like the function func() inside the once() function needs an argument as well. I saw some solutions online that make use of the self keyword but that only works in classes not in functions? Not sure if I am even googling the correct things
Goldstripe sardinellaOP
Alternative log to showcase that there is no logic fail within the code.

p.s. Is there any way to retrieve the numbers 4, 10 or 9001 within the once() function any idea where it gets stored in memory or what label is attributed to it?