Giter Site home page Giter Site logo

callbacks-closures's Introduction

callbacks-closures

Callbacks and Closures

Homework:

  • In twice.js, export a function that accepts a function as an argument, and returns a function that performs the given function twice. For example, if the function exported from twice.js is named doTwice, then passing the function num => num + 2 into doTwice should return a function that will return a number that is 4 larger than the given argument.
  • In counter.js, export a function that will accept a string as an argument. The function should return an object with the following contents: 1) As keys: Every string ever given to this instance of the function as an argument since the time it was exported, and 2) as values: The number of times the corresponding key has been given to the function as an argument. So invoking this function with Jerry should return { Jerry: 1 }. Running the same instance of the function with Elaine should return { Jerry: 1, Elaine: 1 }. Running the same instance of the function with Jerry again should return { Jerry: 2, Elaine 1 }.
  • In saveHistory.js, export a function that will accept two arguments in this order: 1) a string that will serve as a password, and 2) a function. The exported function should return a new function. When the new function is invoked with any argument other than the password, it will seemingly behave just like the function that was passed into the exported function as an argument. When the password is passed into the new function as an argument, the new function should return an array of all arguments ever passed into it, except for when the password was given. So if the exported function is saved into a variable called makeHistorySaver, and then we invoke makeHistorySaver with this line: const multByThree = makeHistorySaver('Bosko', num => num * 3);, then the following invocations in the following order will yield these corresponding outputs: 1) multByThree(4) will yield 12, 2) after that, multByThree(6) will yield 18, 3) after that, multByThree('Bosko') will yield [4, 6], 4) after that, multByThree(3) will yield 9, 5) after that, multByThree('Bosko') will yield [4, 6, 3].

callbacks-closures's People

Contributors

soleiluwedu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.