ENGLISH:
Scope:
- Gandolfo's quick verbal definition: "The area where a variable has access to some value."
- The MDN: https://developer.mozilla.org/en-US/docs/Glossary/Scope
- There are *global* variables and *local* variables.
- A global variable is available throughout the entire codebase.
It can be declared with the regular keywords ('let', 'const', 'var'),
or with no keyword (but this is not a good idea),
or by attaching it to the 'window' object, e.g., 'window.globalVar'.
- local variables are inside of a function or code block.
- The keyword 'let' (unlike 'var') creates block scope.
- A 'block' is basically anything inside of two curly braces.
- If a variable is declared and defined inside of curly braces,
but is declared with 'var' instead of 'let', it does NOT create block scope.
Higher-Order Functions and Callback Functions:
- These are the core concepts of 'functional programming'.
- A higher-order function can...
...take a function as an input (argument)
For example:
element.addEventListener('change', () => {
console.log('Our evidence is updated')
})
...return a function as the output
For example:
const newClue = (name) => {
const length = name.length
return (weapon) => {
let clue = length + weapon.length
return !!(clue % 1)
}
}
- A callback function is just a function we pass to a function.
For example:
const ifElse = (condition, isTrue, isFalse) => {
return condition ? isTrue() : isFalse()
}
const logTrue = () => { console.log(true) } // true // FYI this function logs 'true' but returns 'undefined'
const logFalse = () => { console.log(false) }
console.log(ifElse(true, logTrue, logFalse)) // undefined
- Simple example of passing arguments:
- In ES5:
var increment = function(n) { return n + 1 }
var square = function(n) { return n * n }
var doMathSoIDontHaveTo = function(n, func) { return func(n) }
console.log(doMathSoIDontHaveTo(5, square)) // 25
console.log(doMathSoIDontHaveTo(4, increment)) // 5
- In ES6:
const increment = n => n + 1
const square = n => n * n
const doMathSoIDontHaveTo = (n, func) => func(n)
console.log(doMathSoIDontHaveTo(5, square)) // 25
console.log(doMathSoIDontHaveTo(4, increment)) // 5
// But remember it is probably cleaner / more readable to include the curly braces and 'return' keyword,
// like this '{ return n + 1 }', instead of just 'n + 1', for example.
CODE:// JS// Scope exerciseletACTUAL=nullletfn=function(){letinnerCounterif(innerCounter){// i.e., innerCounter === true, i.e., innerCounter has a truthy value, i.e., innerCounter has been definedinnerCounter=innerCounter+1ACTUAL=innerCounter}else{innerCounter=10innerCounter=innerCounter+1ACTUAL=innerCounter}}console.log(ACTUAL)// nullfn()console.log(ACTUAL)// 11fn()console.log(ACTUAL)// 11// The point is that the line just above does NOT log 12 and that the reason is:// *** Every time a function is called, a brand new function scope is created. ***//-------------------------------------------------------------------// JS// Scope exerciseletACTUAL=nullletouterCounter=10letfn=function(){outerCounter=outerCounter+1ACTUAL=outerCounter}console.log(ACTUAL)// nullfn()console.log(ACTUAL)// 11fn()console.log(ACTUAL)// 12// The point is that the line just above does NOT log 11 again and that the reason is:// *** Between multiple calls of a function,// that function retains access to variables in an outer scope,// and the modification of those variables sustains between calls to the function. ***// *** If you are confused about how exactly this works,// remember that once the value of 'outerCounter' in the function is updated,// 'outerCounter' in the outer scope is updated. ***//-------------------------------------------------------------------// JS// Passing two functions ('fn1' and 'fn2') to a function ('ifElse')// and calling one of the two functions ('fn1' in the below example)// with an argument (the string 'Hey')constifElse=(condition,isTrue,isFalse,greeting)=>{returncondition?isTrue(greeting):isFalse(greeting)}constfn1=(x)=>{console.log(`${x}, it IS true!`)}// Hey, it IS true!constfn2=(x)=>{console.log(`${x}, it is NOT true!`)}console.log(ifElse(true,fn1,fn2,'Hey'))// undefined//-------------------------------------------------------------------// JS// The 'reduce' Methodconstarr=[1,2,3,4,5]constsum=arr.reduce((acc,curVal,curIndex,array)=>{// fCC paraphrase: The accumulator (represented by the 'acc' parameter in the above line)// gets assigned the return value of the callback function from the previous iteration. I.e., 'acc'// could also be 'previousValue', because it holds what the previous iteration returned.console.log(curIndex)// to see result of above line, view in consoleconsole.log(array)// to see result of above line, view in consolereturnacc+curVal},0)// fCC paraphrase: 'reduce' has a second parameter (the '0' in the above line)// which takes an initial value for the accumulator. I.e., the second parameter of 'reduce' is the// initial value (often set to '0'), which just determines what the accumulator used in the first// iteration is. If this second parameter is not used, then the first iteration is skipped and the// second iteration gets passed the first element of the array as the accumulator.console.log(sum)// 15//-------------------------------------------------------------------// JS// Using the 'map' methodconstarr=[1,2,3,4,5]constnewArr=arr.map(num=>{returnnum+2},0)console.log(newArr)// [3, 4, 5, 6, 7]// Using the 'reduce' method to do the same thingconstarr=[1,2,3,4,5]constnewArr=arr.reduce((acc,curVal)=>{acc.push(curVal+2)returnacc},[])console.log(newArr)// [3, 4, 5, 6, 7]// Using the 'filter' methodconstarr=[1,2,3,4,5]constnewArr=arr.filter(num=>{returnnum>2})console.log(newArr)// [3, 4, 5]// Using the 'reduce' method to do the same thingconstarr=[1,2,3,4,5]constnewArr=arr.reduce((acc,curVal)=>{if(curVal>2){acc.push(curVal)}returnacc},[])console.log(newArr)// [3, 4, 5]//-------------------------------------------------------------------// JS// Example from fCC article: https://www.freecodecamp.org/news/reduce-f47a7da511a9/consteuros=[29.76,41.85,46.5]constaverage=euros.reduce((total,amount,index,array)=>{total+=amountif(index===array.length-1){returntotal/array.length}else{returntotal}})// note that not having the second parameter (often '0') here or having it here makes no difference in this exampleconsole.log(average)// 39.37//-------------------------------------------------------------------// JS// Example from fCC article: https://www.freecodecamp.org/news/reduce-f47a7da511a9/// "Creating a Tally with the Reduce Method In JavaScript// Use it when: You have a collection of items and you want to know how many of each// item are in the collection."// less concise versionconstfruitBasket=['banana','cherry','orange','apple','cherry','orange','apple','banana','cherry','orange','fig']constcount=fruitBasket.reduce((tally,fruit)=>{if(!tally[fruit]){// i.e., tally[fruit] === undefinedtally[fruit]=1}else{tally[fruit]++}returntally},{})console.log(count)// {banana: 2, cherry: 3, orange: 3, apple: 2, fig: 1}// more concise versionconstfruitBasket=['banana','cherry','orange','apple','cherry','orange','apple','banana','cherry','orange','fig']constcount=fruitBasket.reduce((tally,fruit)=>{tally[fruit]=(tally[fruit]||0)+1returntally},{})console.log(count)// {banana: 2, cherry: 3, orange: 3, apple: 2, fig: 1}//-------------------------------------------------------------------// JS// Example from fCCconstusers=[{name:'John',age:34},{name:'Amy',age:20},{name:'camperCat',age:10}]constusersObj=users.reduce((obj,user)=>{obj[user.name]=user.agereturnobj},{})console.log(usersObj)// {John: 34, Amy: 20, camperCat: 10}//-------------------------------------------------------------------// JS// fCC challenge: Use 'reduce' to find the average IMDB rating of the movies directed by Christopher Nolan.constwatchList=[{"Title":"Inception","Year":"2010","Rated":"PG-13","Released":"16 Jul 2010","Runtime":"148 min","Genre":"Action, Adventure, Crime","Director":"Christopher Nolan","Writer":"Christopher Nolan","Actors":"Leonardo DiCaprio, Joseph Gordon-Levitt, Elliot Page, Tom Hardy","Plot":"A thief, who steals corporate secrets through use of dream-sharing technology, is given the inverse task of planting an idea into the mind of a CEO.","Language":"English, Japanese, French","Country":"USA, UK","Awards":"Won 4 Oscars. Another 143 wins & 198 nominations.","Poster":"http://ia.media-imdb.com/images/M/MV5BMjAxMzY3NjcxNF5BMl5BanBnXkFtZTcwNTI5OTM0Mw@@._V1_SX300.jpg","Metascore":"74","imdbRating":"8.8","imdbVotes":"1,446,708","imdbID":"tt1375666","Type":"movie","Response":"True"},{"Title":"Interstellar","Year":"2014","Rated":"PG-13","Released":"07 Nov 2014","Runtime":"169 min","Genre":"Adventure, Drama, Sci-Fi","Director":"Christopher Nolan","Writer":"Jonathan Nolan, Christopher Nolan","Actors":"Ellen Burstyn, Matthew McConaughey, Mackenzie Foy, John Lithgow","Plot":"A team of explorers travel through a wormhole in space in an attempt to ensure humanity's survival.","Language":"English","Country":"USA, UK","Awards":"Won 1 Oscar. Another 39 wins & 132 nominations.","Poster":"http://ia.media-imdb.com/images/M/MV5BMjIxNTU4MzY4MF5BMl5BanBnXkFtZTgwMzM4ODI3MjE@._V1_SX300.jpg","Metascore":"74","imdbRating":"8.6","imdbVotes":"910,366","imdbID":"tt0816692","Type":"movie","Response":"True"},{"Title":"The Dark Knight","Year":"2008","Rated":"PG-13","Released":"18 Jul 2008","Runtime":"152 min","Genre":"Action, Adventure, Crime","Director":"Christopher Nolan","Writer":"Jonathan Nolan (screenplay), Christopher Nolan (screenplay), Christopher Nolan (story), David S. Goyer (story), Bob Kane (characters)","Actors":"Christian Bale, Heath Ledger, Aaron Eckhart, Michael Caine","Plot":"When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, the caped crusader must come to terms with one of the greatest psychological tests of his ability to fight injustice.","Language":"English, Mandarin","Country":"USA, UK","Awards":"Won 2 Oscars. Another 146 wins & 142 nominations.","Poster":"http://ia.media-imdb.com/images/M/MV5BMTMxNTMwODM0NF5BMl5BanBnXkFtZTcwODAyMTk2Mw@@._V1_SX300.jpg","Metascore":"82","imdbRating":"9.0","imdbVotes":"1,652,832","imdbID":"tt0468569","Type":"movie","Response":"True"},{"Title":"Batman Begins","Year":"2005","Rated":"PG-13","Released":"15 Jun 2005","Runtime":"140 min","Genre":"Action, Adventure","Director":"Christopher Nolan","Writer":"Bob Kane (characters), David S. Goyer (story), Christopher Nolan (screenplay), David S. Goyer (screenplay)","Actors":"Christian Bale, Michael Caine, Liam Neeson, Katie Holmes","Plot":"After training with his mentor, Batman begins his fight to free crime-ridden Gotham City from the corruption that Scarecrow and the League of Shadows have cast upon it.","Language":"English, Urdu, Mandarin","Country":"USA, UK","Awards":"Nominated for 1 Oscar. Another 15 wins & 66 nominations.","Poster":"http://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3._V1_SX300.jpg","Metascore":"70","imdbRating":"8.3","imdbVotes":"972,584","imdbID":"tt0372784","Type":"movie","Response":"True"},{"Title":"Avatar","Year":"2009","Rated":"PG-13","Released":"18 Dec 2009","Runtime":"162 min","Genre":"Action, Adventure, Fantasy","Director":"James Cameron","Writer":"James Cameron","Actors":"Sam Worthington, Zoe Saldana, Sigourney Weaver, Stephen Lang","Plot":"A paraplegic marine dispatched to the moon Pandora on a unique mission becomes torn between following his orders and protecting the world he feels is his home.","Language":"English, Spanish","Country":"USA, UK","Awards":"Won 3 Oscars. Another 80 wins & 121 nominations.","Poster":"http://ia.media-imdb.com/images/M/MV5BMTYwOTEwNjAzMl5BMl5BanBnXkFtZTcwODc5MTUwMw@@._V1_SX300.jpg","Metascore":"83","imdbRating":"7.9","imdbVotes":"876,575","imdbID":"tt0499549","Type":"movie","Response":"True"}]functiongetRating(theWatchList){constavgRating=theWatchList.filter(movieObj=>{returnmovieObj.Director==='Christopher Nolan'}).map(movieObj=>{return+movieObj.imdbRating}).reduce((ratingsSum,rating)=>{returnratingsSum+=rating},0)/theWatchList.filter(movieObj=>{returnmovieObj.Director==='Christopher Nolan'}).lengthreturnavgRating}console.log(getRating(watchList))// 8.675//-------------------------------------------------------------------// JS// fCC challenge: The function should return a new array containing the squares of only the// positive integers (decimal numbers are not integers) when an array of real numbers is// passed to it. An example of an array of real numbers is [-3, 4.8, 5, 3, -3.2].constsquareList=arr=>{constnewArr=arr.filter(num=>{returnnum>0&&Number.isInteger(num)}).map(num=>{returnnum*num})returnnewArr}console.log(squareList([-3,4.8,5,3,-3.2]))// [25, 9]// the above could also be shortened to thisconstsquareList=arr=>{returnarr.filter(num=>{returnnum>0&&Number.isInteger(num)}).map(num=>{returnnum*num})}console.log(squareList([-3,4.8,5,3,-3.2]))// [25, 9]// doing with 'reduce' instead of both 'filter' and 'map'constsquareList=arr=>{returnarr.reduce((sqrIntegers,num)=>{returnNumber.isInteger(num)&&num>0?sqrIntegers.concat(num*num):sqrIntegers},[])}console.log(squareList([-3,4.8,5,3,-3.2]))// [25, 9]//-------------------------------------------------------------------// JS// To fully understand the code below, really walk through it.// CREATING the 'reduce' functionconstreduce=function(list,callback,initialValue){letaccumulator=initialValuefor(leti=0;i<list.length;i++){// The reason for the below 'if' condition and statement is in case there is no initialValue// passed as an argument to the 'reduce' function call.if(i===0&&accumulator===undefined){// Remember that, in the above line, 'i === 0' means the INDEX is 0, NOT that the VALUE at i is 0.accumulator=list[0]}else{accumulator=callback(accumulator,list[i])// Note that in the callback function call just above we're leaving out 'current index' and// 'array' parameters just because we're only doing the basic functionality here.}}returnaccumulator}// USING the 'reduce' function created aboveconsole.log(reduce([1,2,3],(acc,curVal)=>{returnacc+=curVal},0))// 6
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)