freecodecamp: Functional Programming, Learn About Functional Programming
note code to remember about this course
Learn About Functional Programming
"const tea4TeamFCC = getTea(40); // :("
Understand Functional Programming Terminology
"const tea4GreenTeamFCC = getTea(prepareGreenTea,27); // :(
const tea4BlackTeamFCC = getTea(prepareBlackTea,13); // :("
Understand the Hazards of Using Imperative Code
""
Avoid Mutations and Side Effects Using Functional Programming
" return fixedValue+1;"
Pass Arguments to Avoid External Dependence in a Function
"function incrementer (v) {
return v+1
// Add your code above this line
}
"
Refactor Global Variables Out of Functions
"// Add your code below this line
function add (a,bookName) {
let t=a.slice(0);
t.push(bookName);
return t;
// Add your code above this line
}
/* This function should remove a book from the list and return the list */
// New parameters should come before the bookName one
// Add your code below this line
function remove (a,bookName) {
let t=a.slice(0);
let n=t.indexOf(bookName);
if (n >= 0) {
t.splice(n, 1);
return t;
// Add your code above this line
}
}"
Use the map Method to Extract Data from an Array
"
// Add your code below this line
var rating = [];
rating= watchList.map((v)=>{
let obj = {'title':'','rating':0};
obj.title=v.Title;
obj.rating= v.imdbRating;
return obj;
});
// Add your code above this line"
Implement map on a Prototype
" var newArray = [];
// Add your code below this line
for(let i=0;i<this.length;i++){
newArray.push(callback(this[i]));
}
// Add your code above this line
return newArray;"
Use the filter Method to Extract Data from an Array
"// Add your code below this line
function isAdd(){
watchList.hasOwnProperty('Title') && watchList.hasOwnProperty('imdbRating');
return obj
}
var filteredList=watchList.filter((v)=>v.hasOwnProperty('Title') && v.imdbRating>=8.0).map(l=>{
let obj={'title':'','rating':0};
obj.title=l.Title;
obj.rating=l.imdbRating;
return obj;
});
// Add your code above this line"
Implement the filter Method on a Prototype
" // Add your code below this line
this.forEach(function(item){
if(callback(item))
newArray.push(item);
});
// Add your code above this line"
Return Part of an Array Using the slice Method
"return anim.slice(beginSlice,endSlice);"
Remove Elements from an Array Using slice Instead of splice
"// Add your code below this line
if(cities.length>=3)
return cities.slice(0,3);
// Add your code above this line"
Combine Two Arrays Using the concat Method
" // Add your code below this line
return original.concat(attach);
// Add your code above this line"
Add Elements to the End of an Array Using concat Instead of push
"return original.concat(newItem);"
Use the reduce Method to Analyze Data
"var averageRating=watchList.filter(v=>v.Director==='Christopher Nolan').map(v=>{return parseFloat(v.imdbRating)}).reduce((a,c,n,arr)=>a+ c/arr.length,0);"
Sort an Array Alphabetically using the sort Method
" // Add your code below this line
return arr.sort((a,b)=>a>b);
// Add your code above this line"
Return a Sorted Array Without Changing the Original Array
" // Add your code below this line
let tt= arr.slice(0);
let t=[];
return t.concat(tt.sort((a,b)=>a>b));
// Add your code above this line"
Split a String into an Array Using the split Method
"let reg=/\W/;
return str.split(reg);"
Combine an Array into a String Using the join Method
" // Add your code below this line
let reg=/\W/;
let t=str.split(reg);
return t.join(' ');
// Add your code above this line"
Apply Functional Programming to Convert Strings to URL Slugs
"return title.trim().toLowerCase().split(/\W+/).join("-"); "
Use the every Method to Check that Every Element in an Array Meets a Criteria
"return arr.every(function(v){return v>0;});"
Use the some Method to Check that Any Elements in an Array Meet a Criteria
"return arr.some(function(v){return v>0;});"
Introduction to Currying and Partial Application
" // Add your code below this line
return function(y){
return function(z){
return y+z+x;
}
}"
Nhận xét
Đăng nhận xét