Skip to content

Commit

Permalink
Add: Interview concept (HOF)
Browse files Browse the repository at this point in the history
  • Loading branch information
huzaifa621 committed May 11, 2024
1 parent b92fc61 commit 699e3c6
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 0 deletions.
22 changes: 22 additions & 0 deletions JS/HOF [11-05-2024]/program1.js
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()
36 changes: 36 additions & 0 deletions JS/HOF [11-05-2024]/program10.js
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
41 changes: 41 additions & 0 deletions JS/HOF [11-05-2024]/program2.js
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

12 changes: 12 additions & 0 deletions JS/HOF [11-05-2024]/program3.js
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()
19 changes: 19 additions & 0 deletions JS/HOF [11-05-2024]/program4.js
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);
}
});
11 changes: 11 additions & 0 deletions JS/HOF [11-05-2024]/program5.js
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);
12 changes: 12 additions & 0 deletions JS/HOF [11-05-2024]/program6.js
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);
28 changes: 28 additions & 0 deletions JS/HOF [11-05-2024]/program7.js
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);
32 changes: 32 additions & 0 deletions JS/HOF [11-05-2024]/program8.js
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);
13 changes: 13 additions & 0 deletions JS/HOF [11-05-2024]/program9.js
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);

0 comments on commit 699e3c6

Please sign in to comment.