-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b92fc61
commit 699e3c6
Showing
10 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// HOF -> Higher Order Function | ||
|
||
function x() { | ||
let x = 10; | ||
console.log("Value of x", x); | ||
} | ||
|
||
function hof(f) { | ||
console.log("Value of f", f); | ||
f() | ||
} | ||
|
||
hof(x) // hof | ||
// x is a callback function | ||
|
||
// HOF | ||
// A function that takes another function as its argument. | ||
// for example: hof() | ||
|
||
// callback function | ||
// A function which is passed as an argument | ||
// for example: x() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// We need to filter out those names who can vote | ||
|
||
let people = [ | ||
{ | ||
name: "Huzaifa", | ||
age: 52, | ||
}, | ||
{ | ||
name: "Parth", | ||
age: 25, | ||
}, | ||
{ | ||
name: "Sumit", | ||
age: 15, | ||
}, | ||
{ | ||
name: "Rinka", | ||
age: 13, | ||
}, | ||
{ | ||
name: "Tushar", | ||
age: 18, | ||
}, | ||
]; | ||
|
||
// let votingNames = people.filter(({age}, index, array) => { | ||
// return age >= 18; | ||
// }); | ||
|
||
let votingNames = people.filter(({ age }) => age >= 18); | ||
|
||
console.log(votingNames); | ||
|
||
|
||
// Homework: | ||
// sort |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// HOF | ||
// Callback | ||
|
||
|
||
// Program 1: | ||
|
||
function jack(x) { | ||
|
||
} | ||
|
||
function sparrow(x) { | ||
|
||
} | ||
|
||
sparrow(jack) | ||
|
||
// callback -> jack | ||
// hof -> sparrow | ||
|
||
|
||
// Program 2 | ||
|
||
function add() { | ||
|
||
} | ||
|
||
function subtract() { | ||
|
||
} | ||
|
||
function mutiple() { | ||
|
||
} | ||
|
||
function divide() { | ||
|
||
} | ||
|
||
divide(mutiple) // hof -> divide callback -> multiple | ||
add(subtract) // callback -> subtract hof -> add | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
array methods + hof: | ||
forEach | ||
map | ||
filter | ||
reduce | ||
*/ | ||
|
||
let arr = [1, 2, 3]; | ||
|
||
arr.forEach() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// forEach | ||
|
||
let arr = [ | ||
"Mango", | ||
"Apple", | ||
"Orange", | ||
"Watermelon", | ||
"Chiku", | ||
"Banana", | ||
"Lichi", | ||
]; | ||
|
||
// Tell me the index number of 'Watermelon' in my array | ||
|
||
arr.forEach(function (element, index, array) { | ||
if (element === "Lichi") { | ||
console.log(index); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
let arr = [1, 2, 3, 4, 5]; | ||
|
||
let sum = 0; | ||
let product = 1; | ||
arr.forEach(function (element, index, array) { | ||
sum = sum + element; | ||
product = product * element; | ||
}); | ||
|
||
console.log("Sum of array", sum); | ||
console.log("Product of array", product); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
let sumOfArr = 0; | ||
|
||
function sum(element, index, array) { | ||
sumOfArr += element; | ||
} | ||
|
||
let arr = [1, 2, 3, 4, 5]; | ||
|
||
let x = arr.forEach(sum); | ||
|
||
// console.log(sumOfArr); | ||
console.log(x); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Tell me the difference between forEach and map? | ||
|
||
// 1. map returns an array whereas forEach does not | ||
// 2. If you just want to iterate over your array, then go ahead with forEach | ||
// 3. If you want to create a new array, then go ahead with map | ||
// some examples | ||
|
||
// HOF | ||
// map | ||
|
||
let arr = [1, 2, 3, 4, 5]; | ||
// element -> square | ||
// updated -> [1, 4, 9] | ||
|
||
// [ 1, 4, 9, 16, 25] | ||
// element = 3 | ||
// index = 2 | ||
let x = arr.map(function (element, index, array) { | ||
return element ** 2; // 3**2 -> 9 | ||
}) | ||
|
||
console.log("Value of x", x); | ||
|
||
// for (let i = 0; i < arr.length; i++) { | ||
// arr[i] = arr[i] ** 2; | ||
// } | ||
|
||
// console.log(arr); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// filter | ||
// In filter, return inside the function expects a Boolean value | ||
|
||
function isPrime(num) { | ||
let factor = 0; | ||
for (let i = 1; i <= num; i++) { | ||
if (num % i === 0) { | ||
factor++; | ||
} | ||
} | ||
return factor === 2; | ||
} | ||
|
||
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
|
||
// element = 4 | ||
// index = 3 | ||
// x = [2, 4] | ||
let x = arr.filter(function (element) { | ||
return element > 5; | ||
}); | ||
|
||
console.log(x); | ||
|
||
// let newArr = []; | ||
// for (let i = 0; i < arr.length; i++) { | ||
// if (arr[i] % 2 === 1) { | ||
// newArr.push(arr[i]); | ||
// } | ||
// } | ||
|
||
// console.log(newArr); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// reduce | ||
// accumulator -> default -> 0 | ||
|
||
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
|
||
// index = 9 | ||
// element = 10 | ||
// accumulator = 55 | ||
let x = arr.reduce(function (accumulator, element, index, array) { | ||
return `${accumulator} ${element}`; | ||
}, ""); | ||
|
||
console.log(x); |