FAQ: Higher-Order Functions - Functions as Parameters

you could use the ternary operator, but then don’t use if.

then the condition of the ternary operator should be to check if the results are the same/equal. And then return or the first result or that string informing the user of inconsistentcy.

I tried declaring func as a function.

parameters/app.js:24
    let firstTry = func(val);
                          ^

TypeError: func is not a function
const checkThatTwoPlusTwoEqualsFourAMillionTimes = () => {
  for(let i = 1; i <= 1000000; i++) {
    if ( (2 + 2) != 4) {
      console.log('Something has gone very wrong :( ');
    }
  }
};

const addTwo = num => num + 2;

const timeFuncRuntime = funcParameter => {
  let t1 = Date.now();
//  funcParameter();
  let t2 = Date.now();
  return t2 - t1;
};

// Write your code below
let time2p2 = timeFuncRuntime(checkThatTwoPlusTwoEqualsFourAMillionTimes());

const func = {};

const checkConsistentOutput = (func, val) => {
    let firstTry = func(val);
    let secondTry = func(val);
    if (firstTry === secondTry) {
        return firstTry;
    } else {
        return 'This function returned inconsistent results';
    }
};
checkConsistentOutput(addTwo());

when calling checkConsistentOutput function, you should pass addTwo by reference, and then within the checkConsistentOutput function invoke func().

now:

checkConsistentOutput(addTwo());

you invoke addTwo first. That shouldn’t be the case

How do I get func to be recognized as a function?

parameters/app.js:24
    let firstTry = func()(val);
                   ^

TypeError: func is not a function
const checkConsistentOutput = (func, val) => {
    let firstTry = func(val);
    let secondTry = func(val);
    if (firstTry === secondTry) {
        return firstTry;
    } else {
        return 'This function returned inconsistent results';
    }
};

Make sure your call to this function has a function reference in the first positional argument.

checkConsistentOutput(addTwo, 5)

The above assumes a function called addTwo exists.

I have func defined as a function, but it says that it is not.

parameters/app.js:27
    let firstTry = func(val);
                   ^

TypeError: func is not a function
let func = () => {
   console.log('This is a function');
};

const checkConsistentOutput = (func, val) => {
    let firstTry = func(val);
    let secondTry = func(val);
    if (firstTry === secondTry) {
        return firstTry;
    } else {
        return 'This function returned inconsistent results';
    }
};

Where is your call? You do know that it’s not a good idea to name the function the same as the parameter. That name is a placeholder, not the actual name of the function.

const addTwo = x => x + 2;

checkConsistentOutput(addTwo, 5)

Follow instructions and play with the code when you have completed the lesson successfully, and not until then. Trying to get ahead will only put you further behind.

I tried defining my function with the parameters ‘function’ & ‘value’ like so:

const checkConsistentOutput = (funct,value) => {}

But I get this syntax error:

const checkConsistentOutput = (function,value) => {
SyntaxError: Unexpected token ,

When I changed it to ‘funct’, it stopped erroring
Is there a limit of characters you can have in a parameter?

maybe, but that would then have to be a very very long variable name.

the problem is that function is a reserved keyword.

1 Like

This code worked, but I don’t see how it is different than I what I had for func.

const checkConsistentOutput = (func, val) => {
    let firstTry = func(val);
    let secondTry = func(val);
    if (firstTry === secondTry) {
        return firstTry
    } else {
        return 'This function returned inconsistent results'
    }
};

checkConsistentOutput(addTwo, 10);

I don’t understand well higher order function. How do we use that function and where do we use it? Which advantage does it have?

1 Like

higher order functions invoke another function:

const higher = (func) => {
   func();
}

const callback = () => {
   console.log('test');
}

higher(callback)

higher is the higher order function, which has a parameter (func), for which we provide an argument when we invoke the higher order function:

higher(callback) // pass callback function by reference

you could write a higher order function which calculates the execution time of other functions. There are certainly usage cases.

1 Like

Thanks sir. Now, I understand well.

Sorry to jump onto the pile on but this question need to be changed, and played out differently…
Considering peeps have been complaining about it since early 2019 to now without the format being changed is slightly boggling…

my 5c

3 Likes

What difference does it make to use anonymous functions?

const timeFuncRuntime = funcParameter => {
   let t1 = Date.now();
   funcParameter();
   let t2 = Date.now();
   return t2 - t1;
}

const addOneToOne = () => 1 + 1;

timeFuncRuntime(addOneToOne);
function addOneToOne() { return 1 + 1; }

function timeFuncRuntime(func) {
   let t1 = Date.now();
   func();
   let t2 = Date.now();
   return t2 - t1;
}

timeFuncRuntime(addOneToOne);

The only real difference is that declared functions are hoisted. The weakness is that a declared function can be deleted or overwritten. When we write it as a const function expression it cannot be deleted.

1 Like

Oh gosh, this has not been helpful. So much is missing from this lesson. Please fix it or oust it!!!
Please!

1 Like

And what is an “ANONYMOUS FUNCTION”? This is so frustrating.

An anonymous function is one without a name, such as that in a function expression.

const func = function (arg) {

}

The expression on the right is anonymous, but we have assigned it to a variable which makes it reusable by invoking that name.

console.log(foo(x => x * 2, 21)) 
                ^^^^^^^^^^

The first positional argument in that call to foo() is an anonymous function that will be invoked by foo() on the second positional argument, 21.

Anonymous functions are always written as expressions, which means we can return them as well as pass them into other functions, as we have done above.

It is a tiny bit mind stretching at first but we soon catch on since we use this in everyday coding scenarios.

A very common use of HOF is as callbacks in iterator methods and event handlers. We refer to them as callback because they can be repeatedly called BY the iterator or invoked by the listener, much as our anonymous function above is called by foo(), internally.

1 Like

Fixing it is not something we volunteers on the forum side can do. Believe you me there is a long wish list we’ve been whining about for years. Measures are finally underway to expedite a lot of these nagging little issues. Per chance this one might be among the next wave of refits.