Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading