diff --git a/README.md b/README.md index 6d7f61f41..a60a71357 100644 --- a/README.md +++ b/README.md @@ -30,8 +30,10 @@ There are layers upon layers of nested functions within the game of baseball. Yo 1. Fork repo and add TL as collaborator on Github 2. Clone _your_ fork (not Lambda's repo by mistake!) 3. `cd` into your newly cloned repository -4. Create a new branch by typing `git checkout -b ` -5. Work on your branch, push commits and create PR as usual +1. Create a new branch by typing `git checkout -b ` +4. Install dependencies by typing `npm install` +5. Run tests by typing `npm run test:watch` +6. Work on your branch, push commits and create PR as usual ### Task 2a - MVP code @@ -42,6 +44,8 @@ Find the file `index.js` and complete the tasks until your tests are passing. Edit the `ReadMe` file with your answers. 1. In your own words, define closure (1-2 sentences). + closure is what allows a nested function to reach out and access the variables in the outer function. + 2. Study the following code, then answer the questions below. ```js @@ -63,8 +67,14 @@ dansRoll(); ``` a. Where is closure used in this code? How can you tell? +it's used in personal dice because it has a function inside a function + b. Compare and contrast calling `dansRoll` the first and second time. What is always the same? What could change? +the function being run is always the same. the number can change because it is is being run a new time each time it's invoked. + c. What is the lexical scope of `newRoll`? +personalDice + ### Task 3 - Stretch Goals @@ -75,8 +85,8 @@ See if you can complete one or more of the following challenges: 1. Predict the output of the code below and explain why this is the output using what you learned today. When you're ready for answers, view an explanation [here](https://www.coderbyte.com/algorithm/3-common-javascript-closure-questions ```js -(function(){ - var a = b = 3; +(function(){ + var a = b = 3; })(); console.log("a defined? " + (typeof a !== 'undefined')); console.log("b defined? " + (typeof b !== 'undefined')); @@ -92,7 +102,31 @@ addSix(21); // returns 27 3. 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! +## Testing FAQ + +instructions screenshot + +**What are Tests?** + +- Lambda School staff members use tests to algorithmically check for required elements in a code file. For example, a test for the question "`console.log()` "hello [name]" using a variable `name` to hold the name" (not a question you will ever be asked, but I digress), might pass **if** your code includes a variable called `name` **and if** that variable was created with `const` **and** **if** `console.log` is called. Additional parameters could require that this code doesn't exceed 2 lines. +- Your tests work similarly, with more complex conditionals for what is considered passing and what is considered failing. These tests and associated error messages are meant to help you by providing informative error messages that are specific to the requirements of your project. +- Read more about testing frameworks [here](https://blog.bitsrc.io/top-javascript-testing-frameworks-in-demand-for-2019-90c76e7777e9) including how testing can be used at scale. Lambda School uses Mocha to test in JavaScript. + +**How do I run tests in terminal?** + +- Run tests by typing `npm run test:watch`. +- Testing in Terminal can be done in the terminal utility or VS code + +**How do I run tests in my browser?** + +- Open `index.html` +- You should see the following at first with failed tests and error messages + +failing tests + +- Once your code is working you will see tests passing and can click on tests to view the code which made the test pass. +passing tests ## Resources diff --git a/index.html b/index.html index fdb306537..05d7babf9 100644 --- a/index.html +++ b/index.html @@ -6,6 +6,6 @@ Document - + - + \ No newline at end of file diff --git a/index.js b/index.js index c2cac5161..99075c83f 100644 --- a/index.js +++ b/index.js @@ -27,23 +27,27 @@ function processFirstItem(stringList, callback) { * Study the code for counter1 and counter2. Answer the questions below. * * 1. What is the difference between counter1 and counter2? + * counter1 uses a closure to store the value; counter2 uses a global variable * * 2. Which of the two uses a closure? How can you tell? + * counter1 uses a closure because there's a nested function * * 3. In what scenario would the counter1 code be preferable? In what scenario would counter2 be better? + * counter1 is good if you have one function that can create multiple values + * counter 2 is good if you have multiple functions that need to access one global variable * */ // counter1 code function counterMaker() { - let count = 0; + let count = 0; // count is protected inside function return function counter() { count++; } } -const counter1 = counterMaker(); - +const counter1 = counterMaker(); //can set up multiple counters w/ one function + // counter2 code let count = 0; @@ -56,15 +60,19 @@ function counter2() { Write a function called `inning` that generates a random number of points that a team scored in an inning. This should be a whole number between 0 and 2. */ -function inning(/*Code Here*/){ +function inning(){ + return Math.round(Math.random() * 2); +} - /*Code Here*/ +console.log(inning()); +console.log(inning()); +console.log(inning()); +console.log(inning()); -} /* Task 3: finalScore() -Write a higher order function called `finalScore` that accepts the callback function `inning` (from above) and a number of innings and and returns the final score of the game in the form of an object. +Write a higher order function called `finalScore` that accepts the callback function `inning` (from above) and a number of innings and returns the final score of the game in the form of an object. For example, @@ -76,11 +84,18 @@ finalScore(inning, 9) might return: */ -function finalScore(/*code Here*/){ - - /*Code Here*/ - +function finalScore(cb, num){ + let score = { + home: 0, + away: 0, + }; + for (let i = 0; i < num; i++) { + score.home += cb(); + score.away += cb(); + } + return score; } +console.log(finalScore(inning, 9)); /* Task 4: @@ -90,7 +105,7 @@ Create a function called `scoreboard` that accepts the following parameters: (2) Callback function `inning` (2) A number of innings -and returns the score at each pont in the game, like so: +and returns the score at each point in the game, like so: 1st inning: awayTeam - homeTeam 2nd inning: awayTeam - homeTeam @@ -104,8 +119,33 @@ and returns the score at each pont in the game, like so: Final Score: awayTeam - homeTeam */ -function scoreboard(/* CODE HERE */) { - /* CODE HERE */ +function getInningScore(home, away, num) { + let ordinal = ""; + if (num === 1) { + ordinal = "1st"; + } + else if (num === 2) { + ordinal = "2nd"; + } + else if (num === 3) { + ordinal = "3rd"; + } + else ordinal = `${num}th`; + + return `${ordinal} inning: ${away} - ${home}` } +function scoreboard(cb1, cb2, num) { + let finalHome = 0; + let finalAway = 0; + for (let i = 0; i < num; i++) { + let home = cb1(); + let away = cb1(); + finalHome += home; + finalAway += away; + console.log(getInningScore(home, away, i+1)); + } + console.log(`Final score: ${finalAway} - ${finalHome}`) +} +scoreboard(inning, getInningScore, 9); diff --git a/stretch.js b/stretch.js new file mode 100644 index 000000000..981726e4b --- /dev/null +++ b/stretch.js @@ -0,0 +1,55 @@ +//?????????????????????? iife, but why can i console log a variable inside the function and why not both? +// (function () { +// var a = b = 3; +// })(); + +// console.log("a defined? " + (typeof a !== 'undefined')); +// console.log("b defined? " + (typeof b !== 'undefined')) + +//can't read var a = whatever because you can't reach inside a function +//why can we read b = 3? is a creating a global lexical scope that houses b? +// a = b = 3 is really +// b = 3 +// a = b +// so a = b is creating a backpack in the global scope to house b=3? + + +// for (var i = 0; i < 3; i++) { +// setTimeout(function() { alert(i); }, 1000 + i); //alert(i) only has access to the i after the loop is over -- backpack is not a fluid variable that updates throughout a loop +// } + +// for (var i = 0; i < 3; i++) { +// setTimeout(function(i_local) { //i_local parameter sets i up to be passed in as an argument +// return function() { alert(i_local); } // alert function gets changing local scope +// }(i), 1000 + i);//this (i) is where i is being passed into the iife +// } + + +function createBase(base) { + return function(num) { + return num + base; + } +}; + +var addSix = createBase(6);//not same as var addSix = createBase!! you need to return a function to get addSix to work. +console.log(addSix(10)); // returns 16 +console.log(addSix(21)); // returns 27 + +// functional +// separates data and methods +// doesn’t change data -- uses methods to create new data +// declarative - lots of abstraction - bigger functions calling smaller functions to do detailed imperative steps (ex: .map, .filter, .reduce) +// supports parallel programming +// functions > data + + +// OOP +// objects hold both data and methods +// updates data +// imperative - step by step directions (ex: writing out for loops that do the same as .map) +// doesn’t support parallel programming +// functions can have side effects +// data > functions + +// functional for "data about people", OOP for "simulating people" +// "salary change" -> functional, "employee requests time off" -> OOP