Search code examples
javascriptargumentshigher-order-functions

JavaScript: Create a function defineFirstArg that accepts a function and an argument; Accept More Arguments


Create a function defineFirstArg that accepts a function and an argument. Also, the function being passed in will accept at least one argument. defineFirstArg will return a new function that invokes the passed-in function with the passed-in argument as the passed-in function's first argument. Additional arguments needed by the passed-in function will need to be passed into the returned function.

Below is my code:

const defineFirstArg = (inputFunc, arg) => {

  return function (argTwo) {
    return inputFunc(arg, argTwo)
  }
}

But it's failing the last test spec:

enter image description here

What am I doing wrong?


Solution

  • the third test condition says arguments, not an argument so maybe you will need to try spread operator instead

        const defineFirstArg = (inputFunc, arg) => {
    
          return function (...addtionalArgs) {
            return inputFunc(arg, ...addtionalArgs)
          }
        }
      f2 = defineFirstArg(console.log,"x")
      f2("y","z",'f')
      //x y z f
    

    to spread the parameters and execute the function passed on an unlimited number of parameters