-1

I'm trying to solve this algorithm. I think I'm very close.

I need the seven function to pass the a variable to the timesFive function. OR is there a way I can pass the * 5 operation from timesFive to seven in order to get the desired result?

  • I'm not allowed to have variables outside of these two function scopes.
  • I can add another argument to timesFive as long as b remains the first argument.

    function seven(action) {
        var a = 7;
    
        return action;
    }
    
    
    function timesFive(b) {
        console.log(a);  // How can I access "a"?
    
        return a * 5;
    }
    
    
    console.log(seven(timesFive()));    // Desired result: 35
    
  • Are you *sure* you're required to have the functions in that particular order, `seven(timesFive())`? I'd have expected them to be the other way around – CertainPerformance Nov 04 '18 at 03:12
  • 1
    I have to know what this is for? Who is asking you to write code like this? – Wainage Nov 04 '18 at 03:16
  • 1
    It's likely you have misunderstood what you were asked to accomplish. I assume the goal is to teach you closures, scopes and higher-order function. Either your teacher is providing you with a very bad example, either you did not fully understand the task at hand. – Swann Nov 04 '18 at 03:19
  • @soueuls This might not be a great example of closures, but I don't think that's what it's meant to be. It's a fine example of composing functions. – Mark Nov 04 '18 at 03:29
  • @MarkMeyer It's not, your proved it with your answer. You either had to remove `b` from the function's signature or just plain ignore it. That's why I assume either the problem could do a better job at teaching higher-order function, either OP did not fully understand the problem. – Swann Nov 04 '18 at 03:39
  • @soueuls I agree the OP probably doesn't understand the problem; that's why they are asking the question (which is fine: higher order functions are hard at first). My point was that the goal is probably not to teach closures, but rather composing higher order functions. – Mark Nov 04 '18 at 03:45
  • @MarkMeyer I am not saying OP should understand higher order functions at first glance, it takes time. I am saying OP probably did not fully understand the question that was asked (or maybe the question is poorly written). In your solution, you remove `b` entirely while OP specifically says it should be kept as first argument. It's obviously possible to just ignore it. I just encourage OP to re-read the problem's statement carefully. – Swann Nov 04 '18 at 03:52
  • @soueuls I reread the question and agree the part about `as long as b remains the first argument.` is a little strange. – Mark Nov 04 '18 at 04:03

1 Answers1

1

timeFive should return a function and seven should expect a function as a parameter. Then seven can simply call the function that timesFive() returns (so basically returning the * 5 operation as you mentioned in your question):

function seven(action) {
    var a = 7;
    return action(a);
}


function timesFive() {
    return (a) => a * 5;
}

console.log(seven(timesFive()));    // Desired result: 35

BTW You'll often see these kind of exercises written in a way that mimics the functional notation from math, like:

const seven = (action) => action(7);
const timesFive = () => (a) => a * 5;

console.log(seven(timesFive()));    // Desired result: 35
Mark
  • 90,562
  • 7
  • 108
  • 148