GithubHelp home page GithubHelp logo

dasredcoat / web-module-challenge-scope-and-closure Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bloominstituteoftechnology/web-module-challenge-scope-and-closure

0.0 0.0 0.0 225 KB

JavaScript 97.58% HTML 2.42%

web-module-challenge-scope-and-closure's Introduction

Scope and Closure Challenge

The module challenge is the afternoon project or assignment that students work through independently. This expands on the guided project completed earlier with the instructor.

JavaScript Foundations

Scope and Closures

Objectives

  • Explain function scope
  • Describe what closure is, how closure is created in a program and why it is important to understand closures in JavaScript

Introduction

This challenge focuses on both scope and closures.

In this challenge you will be working to build a scoreboard (in the console) that takes randomly generated data and keeps track of a game's progress. If you're not familiar with the rules of baseball what you need to know is this: there are 9 innings and teams take turns "at-bat." Teams can only score while they are at bat. A team stops being at bat once they have gotten 3 outs by either striking out or through game play. You can read more about baseball rules here.

A scoreboard in a major league stadium looks something like this. In fact, the scoreboard at Fenway Park in Boston is actually quite famous.

Fenway Scoreboard

There are layers upon layers of nested functions within the game of baseball. Your challenge today will be to work through tasks associated with these layers, and ultimately to produce a scoreboard that logs in the console.

Instructions

Task 1 - Set Up Project and Tests

  1. Fork the repo
  2. Clone your forked version of the repo
  3. cd into your repo and create a branch with your first and last name
  4. open the terminal in your vs code and type npm install
  5. next type npm run test:watch in your terminal
  6. Complete your work making regular commits, once you have all your tests passing and you are ready to submit your work please see canvas for instructions on how to submit

Task 2a - MVP code

Find the file index.js and complete the tasks.

Task 2b - Written questions

Edit the ReadMe file with your answers.

  1. In your own words, define closure (1-2 sentences).
  2. Study the following code, then answer the questions below.
function personalDice(name){
  return function(){
      // generate random number between 1 and 6
    const newRoll = Math.floor(Math.random() * 6);
    console.log(`${name} rolled a ${newRoll}`)
  }
}

const dansRoll = personalDice("Dan");

const zoesRoll = personalDice("Zoe");


dansRoll();
dansRoll();

a. Where is closure used in this code? How can you tell? b. Compare and contrast calling dansRoll the first and second time. What is always the same? What could change? c. What is the lexical scope of newRoll?

Task 3 - Stretch Goals

After you have completed the requirements, create a new file called stretch.js and practice more with closures. There are no tests for these problems.

See if you can complete one or more of the following challenges:

  1. Write a function that would allow you to do this using a closure. (This is another interview question we've seen before - when you're ready for answers, view an explanation here).
var addSix = createBase(6);
addSix(10); // returns 16
addSix(21); // returns 27
  1. Research the differences between functional programming and object oriented programming. Then, describe the pros and cons of functional programming vs object-oriented programming. This is a common interview question and great practice!

Resources

๐Ÿ“š Scope and Closures Guide

๐Ÿง  "I never Understood Closures" Blog

Submission Format

Please see Canvas for cohort specific submission instructions

web-module-challenge-scope-and-closure's People

Contributors

brityhemming avatar davekidd avatar zoelud 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.