Skip to content

Commit 979c810

Browse files
authored
Update Notes.md
1 parent 751ff59 commit 979c810

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

Ref/Notes.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2506,3 +2506,86 @@ When using `translate()`, the element still occupies its original space (sort of
25062506
* https://quizlet.com/28293152/front-end-interview-questions-css-flash-cards/
25072507
* http://peterdoes.it/2015/12/03/a-personal-exercise-front-end-job-interview-questions-and-my-answers-all/
25082508

2509+
# High Order Functiions:
2510+
2511+
A higher-order function is a function that can take another function as an argument, or that returns a function as a result.
2512+
A Higher-Order function is a function that receives a function as an argument or returns the function as output.
2513+
2514+
let total = 0, count = 1;
2515+
while (count <= 10) {
2516+
total += count;
2517+
count += 1;
2518+
}
2519+
console.log(total);
2520+
2521+
console.log(sum(range(1, 10)));
2522+
it is using below high order functions like .filter(), .map() and .reduce()
2523+
2524+
Let’s say we have an array of numbers and we want to create a new array which contains double of each value of the first array.
2525+
2526+
Without Higher-order function
2527+
2528+
const arr1 = [1, 2, 3];
2529+
const arr2 = [];
2530+
for(let i = 0; i < arr1.length; i++) {
2531+
arr2.push(arr1[i] * 2);
2532+
}
2533+
// prints [ 2, 4, 6 ]
2534+
console.log(arr2);
2535+
With Higher-order function map
2536+
2537+
const arr1 = [1, 2, 3];
2538+
const arr2 = arr1.map(function(item) {
2539+
return item * 2;
2540+
});
2541+
console.log(arr2);
2542+
2543+
The filter() method creates a new array with all elements that pass the test provided by the callback function.
2544+
2545+
const persons = [
2546+
{ name: 'Peter', age: 16 },
2547+
{ name: 'Mark', age: 18 },
2548+
{ name: 'John', age: 27 },
2549+
{ name: 'Jane', age: 14 },
2550+
{ name: 'Tony', age: 24},
2551+
];
2552+
const fullAge = [];
2553+
for(let i = 0; i < persons.length; i++) {
2554+
if(persons[i].age >= 18) {
2555+
fullAge.push(persons[i]);
2556+
}
2557+
}
2558+
console.log(fullAge);
2559+
2560+
With Higher-order function filter
2561+
2562+
const persons = [
2563+
{ name: 'Peter', age: 16 },
2564+
{ name: 'Mark', age: 18 },
2565+
{ name: 'John', age: 27 },
2566+
{ name: 'Jane', age: 14 },
2567+
{ name: 'Tony', age: 24},
2568+
];
2569+
const fullAge = persons.filter(person => person.age >= 18);
2570+
console.log(fullAge);
2571+
2572+
The reduce method executes the callback function on each member of the calling array which results in a single output value.
2573+
2574+
With Higher-order function reduce
2575+
2576+
const arr = [5, 7, 1, 8, 4];
2577+
const sum = arr.reduce(function(accumulator, currentValue) {
2578+
return accumulator + currentValue;
2579+
});
2580+
// prints 25
2581+
console.log(sum);
2582+
2583+
Without Higher-order function
2584+
2585+
const arr = [5, 7, 1, 8, 4];
2586+
let sum = 0;
2587+
for(let i = 0; i < arr.length; i++) {
2588+
sum = sum + arr[i];
2589+
}
2590+
// prints 25
2591+
console.log(sum);

0 commit comments

Comments
 (0)