Giter Site home page Giter Site logo

javascript-first-class-functions-lab-v-000's Introduction

Functions Returning Functions Lab

Objectives

  1. Practice writing functions that return other functions
  2. Practice passing a function to another function
  3. Practice calling a function returned by another function

Introduction

Functions are a very important part of JavaScript, and you will use them pretty much all of the time. Without functions, we wouldn't get anything done! There is a lot more to functions than meets the eye, which we will discover together throughout this unit. In this lab, we'll take a look at how we can use first-class functions to pass around functions, or return them.

Exercise 1 — The final countdown

Europe

Dudududuuuuuu. Dudududu.

The band, Europe, has been struggling to keep in rhythm with each other. When the keyboard tunes stop, the vocals should kick in after two seconds — for dramatic effect, naturally! Unfortunately, the lead singer has been missing the mark lately. Let's help him out!

We'll start things off by creating a countdown() function. This function takes one argument, callback, which is a function. Using window.setTimeout(), we will wait two seconds before calling the callback function that was passed into the countdown() function. This exercise demonstrates the use of callbacks for async operations. If we didn't use a callback, our program would continue right away instead of waiting for the callback to be called.

Exercise 2 — Playing Scrabble

Scrabble

Let's say we're trying to recreate Scrabble in JavaScript. Some board tiles can give us double or triple letter score. Let's create a helper function that takes a number (let's call it the multiplierValue). This function then returns a function that multiplies a given value by the multiplierValue. In your code, do the following:

  1. Create a createMultiplier() function. Make sure it returns the right thing!
  2. Create a doubler variable that uses the createMultiplier() function to create a function that doubles any given number.
  3. Create a tripler variable that does the same thing as the doubler, but it triples the value instead.

Exercise 3 — It's part(y)(ial) time

Instead of a function returning another function (like we did in the previous exercise), we can also write a function that takes two values right away: the multiplierValue and the value. However, we can't create the doubler and tripler functions by just calling this new function with two arguments! The trick is to partially apply the function. We can do this using .bind(). Quick hint: you can forget about .bind()'s first argument (the this context) for now — you can use null as its value.

To pass all tests, do the following:

  1. Create a multiplier() function that takes two arguments and multiplies them together.
  2. Create a doublerWithBind variable that partially applies the multiplier() function by passing in 2 as its first argument.
  3. Create a triplerWithBind variable that partially applies the multiplier() function by passing in 3 as its first argument.

Resources

View First Class Functions Lab on Learn.co and start learning to code for free.

javascript-first-class-functions-lab-v-000's People

Contributors

pletcher avatar thomastuts avatar crwhitesides avatar jessrudder avatar

Watchers

James Cloos 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.