FAQ: Higher-Order Functions - Functions as Parameters

I think perhaps they are wanting us to fallback to the documentation or search things we don’t fully understand ourselves. The way I understand it is google and the ability to search and find solutions to our questions is a very important tool in a coder’s arsenal. This forum is one such place to find answers as well as MDN. I have also found answers to questions on Stack andW3.
just to name a few. I have yet to have a question that hasn’t already been asked and answered at some point on some coding site. If coding is what we plan on doing we should definitely become comfortable with searching for answers, I’m sure the people that answer our questions here have questions of their own they have to search for answers to for every day.

3 Likes

I can only speak for myself, but that is a very true statement.

1 Like

Silly question but…

I’ve answered 3.Functions as parameters correctly (based on the assumption that the console on the right is not giving me error messages). But why is my solution not returning/displaying anything in the console? Here’s my code:-

const checkConsistentOutput = (func, val) => {
let firstCall = func(val);
let secondCall = func(val);
if (firstCall === secondCall){
return firstCall;
} else {
return ‘This function returned inconsistent results’;
}
};

checkConsistentOutput(addTwo, 3);

//end code

Many thanks!!

to show output, you need to log to the console (console.log())

1 Like

Many thanks for the reply @stem94! I understand calling console.log() – I was curious why calling a function I’d written in the code pane didn’t display in the console as it has done on other lessons.

: )

the learning environment (LE) of codecademy is complex, so sometimes the exercise validations/test cases do display output. But unless you can provide exercise + code where you experienced this, its difficult to say what exactly the issue is

but don’t rely on codecademy, if you want to log something, call the log method

After passing the submission test, albeit, so there are no wrinkles. Once we’ve passed we are free to play around a bit, so take up the opportunity to explore the lesson in greater depth while you’re on that page.

1 Like

Thanks @mtf, will do!

1 Like

I didn’t understand this lesson very well, after watching some youtube videos on the topic I was able to understand it a little better. However what I still don’t understand is this part in the exercise:

“This function should have two parameters: a function and a value. It should call the argument function with the value two times.”

When I read that I thought it meant I should call the function “value” using the argument “function” (i.e. val(func)) However it was the other way around. I don’t understand the wording of this exercise.

3 Likes

Hi there. I’m struggling to understand this exercise. I’m willing to risk asking naive questions to obtain guidance. :slight_smile:

First - Where is the addOneToOne function defined? The instructions refer to it, but I don’t see it anywhere.

Second -

checkConsistentOutput = (func, val) => {
let firstTry = func(val)

Where was func defined as a function? When func takes in val as an argument, what does func do with it?

Thanks!

It could be defined in the actual argument…

const foo = (f, n) => f(n)

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

Why is the marked ** line in the following code a syntax error?:

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

const checkConsistentOutput = (func,value) => {
  let a = func(value);
  let b = func(value);
  **a === b ? return a : return 'This function returned inconsistent results';**
};

May we see the traceback message, please. That will tell us some valuable information.

Not sure I follow the first two lines. May we see those functions, as well, please.

As for the ternary, consider,

return condition ? valueA : valueB;

First, here is the traceback:

/home/ccuser/workspace/higher-order-functions-functions-as-parameters/app.js:25
  a === b ? return a : return 'This function returned inconsistent results';
            ^^^^^^
SyntaxError: Unexpected token return
    at createScript (vm.js:53:10)
    at Object.runInThisContext (vm.js:95:10)
    at Module._compile (module.js:543:28)
    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)

And second, this is the full code for that exercise:

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
const isTimes1M = checkThatTwoPlusTwoEqualsFourAMillionTimes;
const time2p2 = timeFuncRuntime(isTimes1M);

const checkConsistentOutput = (func,value) => {
  let a = func(value);
  let b = func(value);
  a === b ? return a : return 'This function returned inconsistent results';
};
  
checkConsistentOutput(addTwo, 1);


1 Like

As one suspected, we cannot use return inside a ternary. Try re-writing the ternary in the style of my last post.

I know that it works without the ternary, but why does a ternary not accept that token? We did learn the usual way to make if statements, but since a ternary is shorter and more readable, I would have preferred here.
Or another way to phrase the question, what can I do with a ternary, and when do I have to use the verbose version?

Edit: Another question I had, why does this line work:

const checkConsistentOutput = (func,value) => {

but this one won’t?:

const checkConsistentOutput = (value,func) => {
  let a = func(value);
          ^

TypeError: func is not a function
    at checkConsistentOutput (/home/ccuser/workspace/higher-order-functions-functions-as-parameters/app.js:23:11)
    at Object.<anonymous> (/home/ccuser/workspace/higher-order-functions-functions-as-parameters/app.js:32:1)
    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)

Note: This is not the original traceback I first saw when I entered a value parameter first and then the function, but I can’t seem to recreate that. IIRC it was along the lines of unexpected token or something. It doesn’t matter so much though, I expect I just had another syntax error somewhere that changed the error. My question of why the parameter function has to be written before the value parameter is still the one I would like an answer for.

The arguments determine the order, not the parameters.

If we reverse the order of the parameters, we need to reverse the way we employ their values.

let a = value(func);

If we expect a function reference in the first positional argument, then be sure to pass it in first, followed by the value, which is expected to be the second positional argument.

Ternaries are expressions, not statements (unless assigned/referenced). That is why we cannot write return in the value on the right of the ? ternary operator. It would make each value a statement, not an expression.

When we consider how a return statement works,

    return // object/expression/value
1 Like

Oh so, it would work if I did something along the lines of:

let statement = a===b ? return a : return 'go home'

I guess?

Have you tried it?

 > func = (a, b) => {
       let statement = a===b ? return a : return 'go home'
   }
 X Uncaught SyntaxError: Unexpected token 'return'
 > 

Same problem as earlier.

 > func = (a, b) => {
       let value = a===b ? a : 'go home'
       return value;
   }
<- (a, b) => {
       let value = a===b ? a : 'go home'
       return value;
   }
 > func(2,2)
<- 2
 > func(2,3)
<- "go home"
 >

Oh, yeah I can see that. Thank you.

1 Like