How should I write the JavaScript code such that the output will be "first", "second" and "third"

I’m trying to figure out a JavaScript code scope problem and I have simplified this to

const first = () => {
    console.log("first");
}

const second = (first) => {
    first();
    console.log("second");
}

const third = (second) => {
    second();
    console.log("third");
}

Based on three functions above, I am supposed to write code using only the three functions above to get the output like in console below:
first
second
third

I tried:
#1
second(first) – which returns first, second

#2
third(second(first) – which throws an error second is not a function.

Any ideas?

>Solution :

You can do this

third(second.bind(null, first))

Not that I would ever write that in the real world.
In Typescript land, bind doesn’t work that well.

third( ()=> second(first) )

Could also work

Leave a Reply