FAQ: Higher-Order Functions - Functions as Parameters

when trying to call timeFuncRuntime with a non-defined function:

timeFuncRuntime(nonExistingFun)

JS should throw an error.

Remember, a higher order function like timeFuncRuntime still has the same rules for parameters (funcParameter) and argument (The value passed to the parameter) as regular functions.

Then the answer to this previous question: [quote=“mindyjo, post:78, topic:371851”]
does this assume that somewhere in the code (not shown in the example) there is a function written with the name funcParameter
[/quote]
would be “yes” not “no” - if it were “no”, then JS would throw an error. Correct? to put it another way, if funcParameter were not defined somewhere in the code, it would throw an error. The sample exercise was confusing to me because it did not show funcParameter as a defined function.

Back to basicis, when we have a function:

const x = y => {
   console.log(y);
}

this code won’t throw an error. Even though y isn’t defined anywhere. Because the parameter (y) acts as a placeholder until its gets a value from the argument you provide at function call:

x(3);

same is true for the funcParameter.

got it. tricky. but I think it’s sinking in. thanks. One more question, if you don’t mind. The rest of the exercise example code is:

const addOneToOne = () => 1 + 1;

timeFuncRuntime(addOneToOne);
So if my slow brain is now comprehending: the funcParameter was named as the “parameter” of timeFuncRuntime, and then when called, it ran the addOneToOne function because the addOneToOne function was the argument when timeFuncRuntime was run.
right?

Steadily we are getting there :slight_smile: Yea, that is right. Of course, timeFuncRuntime can be used to calculate the duration of any function, but in this case, we calculate the run time of addOneToOne because that is the argument we pass.

1 Like

2 posts were split to a new topic: This higher order function does not make sense

const timeFuncRuntime = funcParameter => {
let t1 = Date.now();
funcParameter();// ok if we can use parameter as a function call then function call by itself which leads to recurrsion.??? please can any one resolve this doubt
let t2 = Date.now();
return t2 - t1;
}

const addOneToOne = () => 1 + 1;

timeFuncRuntime(addOneToOne);

If the function would call itself:

const example = () => {
   example();
}

that would be recursion.

in your code you invoke addOneToOne (a different function), that is totally fine :slight_smile:

I agree with you, I also think that pedagogically just falling back on “well look it up” seems to fall short of actually teaching subject matter. For me, I have been looking at functions and loops as the “meat and potatoes” of “Learn JavaScript”. I’ll admit to being weak at it and even adding study time to polish it up and be better, but the way they approach teaching some of it leaves a lot to be desired.

Hi, i would like to report an issue.
well as soon as my problem and try to code the answer to the question/task 1.
i get the following error:
/home/ccuser/workspace/higher-order-functions-functions-as-parameters/app.js:13
funcParameter();
^

TypeError: funcParameter is not a function
at timeFuncRuntime (/home/ccuser/workspace/higher-order-functions-functions-as-parameters/app.js:13:3)
at Object. (/home/ccuser/workspace/higher-order-functions-functions-as-parameters/app.js:20:13)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
Well as i can see the problem is in the default code provided to us.
Can you please check this out? and let me know if i am the only one with this issue.
if not, please change the problem or change the default code which doesnt give error.
Thanks.

Could you please copy paste the default code to the forum? thank you :slight_smile:

hi, i went away with the problem and am now 4-5 questions ahead and as i am new here i dont know how to go back. so really sorry i cant paste it here.

On part two of this question, I do not get any syntax errors or tips at the bottom of the screen, but this code does not pass the test so I can move on.

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 checkConsistentOutput = (func,value) => {
  let a = func(value);
  let b = func(value);
 // return condition ? valueA : valueB; 'This function returned inconsistent results';
 if (condition ? valueA : valueB) { 
   return 'This function returned inconsistent results';
   };
};

here:

if (condition ? valueA : valueB)

condition variable isn’t defined anywhere?

also, why would you use a ternary operator here?

The ternary was recommended in a previous post. How do I write the condition such that it meets the conditions: " If the callback function produces the same result twice, it should return the result of the function call, otherwise, it should return the string 'This function returned inconsistent results'"?

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.