Giter Site home page Giter Site logo

chocolate-babka-closures-web-0715-public's Introduction

Chocolate Babka

You're running late to a dinner party, you need to bring something. Why not a chocolate babka? Sure, cinnamon has it's good qualities but you want chocolate. The only problem is the bakery's ticket machine is broken. You're smart, enterprising engineer, so you make a deal with the owner that if you get it working you can have your Babka free of charge.

Yeah, yeah this photo isn't from the right episode.

This is your moment to shine, no Pepsi and Ring Dings for you. You're an adult you're gonna get a chocolate babka and a nice bottle of wine to impress your friends. You just need to understand JavaScript closures first.

Closures

Closures are like small snapshots of the scope of a function at a certain place in time. This is achieved by returning an anoymous function from within a function. Variables defined within that outer function will be within scope for the inner function that is returned. We can then call the returned inner function, which will access the variable stoed in its closure.

ex.

function idMaker(phrase) {
  var greeting = phrase;
  
  // this inner function has access to the outer function's variables and argument(s)
  return function(firstName, lastName){
    return greeting + firstName + ' ' + lastName + '.';
  };
}

var labelMaker = idMaker('Hello, my name is ');

labelMaker('Tristan', 'Siegel');
// -> 'Hello, my name is Tristan Siegel'

labelMaker('Steven', 'Nunez');
// -> 'Hello, my name is Steven Nunez'

The code above works because we saved the returned function to a variable, making it a function expression. Because as the code was run the inner function had access to the greeting variable it now has saved access to it. Even though we can not directly access greeting we can see its value gets returned when we call the labelMaker function.

Requirements

Your counter will need to start at a number based on what is passed in. It must then return a function that, when called, will increment number that was passed in.

Also, the baker has realized that she will need a seperate counter for the babkas since they are so popular. This doesn't mean you need to make a new function for the babka counter. However, to make sure customers know which number is being served, you will need to make a second function that accepts the returned closure as an argument as well as a string to announce which line is being served.

This will mean you need to make two seperate functions that create closures by returning inner functions. Follow the specs for more information.

##Before You Start Install jasmine:

$ gem sources -a http://flatiron:[email protected]
$ gem install learn-co

Run Jasmine test suite:

$ learn  # runs only in the terminal
$ learn -b # runs also in the browser

Resources

chocolate-babka-closures-web-0715-public's People

Contributors

deniznida avatar ehawk823 avatar fislabstest avatar fs-lms-test-bot avatar sarogers avatar tsiege avatar

Watchers

 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.