Map, Filter & Reduce & their Polyfills
const employees = [
{ name : 'John' , age : 32 } ,
{ name : 'Sarah' , age : 28 } ,
{ name : 'Michael' , age : 40 } ,
] ;
const employeesName = employees . map ( employee => employee . name ) ;
console . log ( employeesName ) ; // ["John", "Sarah", "Michael"]
if ( ! Array . prototype . myMap ) {
Array . prototype . myMap = function ( callback ) {
const result = [ ] ;
for ( let i = 0 ; i < this . length ; i ++ ) {
result . push ( callback ( this [ i ] , i , this ) ) ;
}
return result ;
} ;
}
const myEmployeesName = employees . myMap ( employee => employee . name ) ;
console . log ( myEmployeesName ) ; // ["John", "Sarah", "Michael"]
const products = [
{ name : 'iPhone' , price : 999 , inStock : true } ,
{ name : 'Samsung Galaxy' , price : 899 , inStock : false } ,
{ name : 'Google Pixel' , price : 799 , inStock : true } ,
] ;
const availableProducts = products . filter ( product => product . inStock ) ;
// [
// { name: 'iPhone', price: 999, inStock: true },
// { name: 'Google Pixel', price: 799, inStock: true },
// ]
if ( ! Array . prototype . myFilter ) {
Array . prototype . myFilter = ( callback ) => {
const result = [ ] ;
for ( let i = 0 ; i < this . length ; i ++ ) {
if ( callback ( this [ i ] , i , this ) ) {
result . push ( this [ i ] ) ;
}
}
return result ;
} ;
}
const myAvailableProducts = products . myFilter ( product => product . inStock ) ;
console . log ( availableProducts ) ;
// [
// { name: 'iPhone', price: 999, inStock: true },
// { name: 'Google Pixel', price: 799, inStock: true },
// ]
const orders = [
{ product : 'iPhone' , price : 999 , quantity : 2 } ,
{ product : 'Samsung Galaxy' , price : 899 , quantity : 1 } ,
{ product : 'Google Pixel' , price : 799 , quantity : 3 } ,
] ;
const totalAmount = orders . reduce ( function ( accumulator , order ) {
return accumulator + order . price * order . quantity ;
} , 0 ) ;
console . log ( totalAmount ) ; // 5294
if ( ! Array . prototype . myFilter ) {
Array . prototype . myReduce = ( callback , initialValue ) => {
let accumulator = initialValue === undefined ? this [ 0 ] : initialValue ;
for ( let i = initialValue === undefined ? 1 : 0 ; i < this . length ; i ++ ) {
accumulator = callback ( accumulator , this [ i ] , i , this ) ;
}
return accumulator ;
} ;
}
const myTotalAmount = orders . myReduce ( function ( accumulator , order ) {
return accumulator + order . price * order . quantity ;
} , 0 ) ;
console . log ( totalAmount ) ; // 5294
Question 1: Find the longest word length
const words = [ 'apple' , 'banana' , 'cherry' , 'dragonfruit' , 'elderberry' ] ;
const longestWordLength = words . reduce ( ( maxLength , word ) => {
const currentLength = word . length ;
return currentLength > maxLength ? currentLength : maxLength ;
} , 0 ) ;
console . log ( longestWordLength ) ; // Output: 11
Question 2: Find the longest word
const longestWord = words . reduce ( ( longestWord , word ) => {
return word . length > longestWord . length ? word : longestWord ;
} , "" ) ;
console . log ( longestWord ) ; // Output: 'dragonfruit'
Question 3: Calculate the factorial of the largest number in the array
const numbers = [ 5 , 2 , 8 , 4 , 3 ] ;
const largestFactorial = numbers . reduce ( ( largest , num ) => {
const currentFactorial = Array
. from ( { length : num } )
. map ( ( _ , i ) => i + 1 )
. reduce ( ( fact , val ) => fact * val , 1 ) ;
return currentFactorial > largest ? currentFactorial : largest ;
} , 1 ) ;
console . log ( largestFactorial ) ; // Output: 40320 (8!)
Question 4: Calculate the average score of students who scored above 90
const students = [
{ name : 'John' , score : 85 } ,
{ name : 'Sarah' , score : 92 } ,
{ name : 'Michael' , score : 88 } ,
{ name : 'Emma' , score : 95 } ,
{ name : 'Daniel' , score : 90 } ,
] ;
const above90StudentsAverage = students
. filter ( ( student ) => student . score > 90 )
. reduce ( ( acc , student , i , arr ) => acc + student . score / arr . length , 0 ) ;
console . log ( above90StudentsAverage ) ; // Output: 93.5 (average of 95 and 92)
Question 5: Filter out books published before the year 2000 and return their titles
const books = [
{ title : 'Book 1' , year : 1998 } ,
{ title : 'Book 2' , year : 2003 } ,
{ title : 'Book 3' , year : 1995 } ,
{ title : 'Book 4' , year : 2001 } ,
] ;
// Expected Output: ['Book 2', 'Book 4']
Question 6: Capitalize the first letter of each word in the array
const strings = [ 'hello world' , 'i am openai' , 'welcome to javascript' ] ;
// Expected Output: ['Hello World', 'I Am Openai', 'Welcome To Javascript']