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.
- Explain function scope
- Describe what closure is, how closure is created in a program and why it is important to understand closures in JavaScript
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.
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.
Using VSCode and Command Line:
- Fork the repo
- Go into canvas and connect your reop to codegrade
- Clone your forked version of the repo
- DO NOT CREATE A BRANCH. You will be pushing your changes to the main/master today
- cd into your repo
- open the terminal in your vs code and type
npm install
- next type
npm run test
in your terminal - Complete your work making regular commits to main/ master your codegrade score will update each time you make a push.
Find the file index.js
and complete all tasks.
Open a second terminal inside of your project by clicking on the split terminal icon
Inside of your second terminal type npm start
You will be running your tests in one terminal and debugging in the other. As you work on your code you should make use of console.log
to check your progress and debug.
Edit the ReadMe
file with your answers to the questions below.
- In your own words, define closure (1-2 sentences).
- 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
?
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:
- 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
- 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!
🧠 "I never Understood Closures" Blog
Please submit your project via codegrade by following these instructions See part 2, submitting an assignment with codegrade