Higher Order Functions 
Tags: javascript, programming, webdev
Published: 2022-02-02 07:46:04

Learning about MAP,FILTER 
**_Map_**

// higher order functions 


//Map Functions->

//📌Map is itself a function 
// Map takes a callback function as argument 
//Map will call the callback function as many times as the elements in the array
//map will process  every value and will apply the instructions that inside the callback function
//map returns a new array and doesnt make changes in original array 
//it does not chagnge the original array 

//📌Q-1
let arr =[2,3,4,5,6,7,8,9];

let squarr =arr.map(function square(v){
    return v*v;
})

//👉ans -->[
//    4,  9, 16, 25,
//    36, 49, 64, 81 
//  ]

console.log(squarr);
console.log(arr);

//📌 Q-2

// You have to use map function and you will take out firstName and lastName separately

let name1=['Pushan Verma','Chetna Singh','Aditi Srivastava' ,'Chandresh Tomar'];

let modifiedarr=name1.map(function(n)
{
    let splitting=n.split(" ");
    return splitting
})

console.log(modifiedarr);

// 👉ans -->[
//     [ 'Pushan', 'Verma' ],
//     [ 'Chetna', 'Singh' ],
//     [ 'Aditi', 'Srivastava' ],
//     [ 'Chandresh', 'Tomar' ]
//   ]

// let modifierarr1 =name1.map(function(n)
// {
//     let splitting1=n.split(" ");
//     let first =
// })






//Q-3
// you have transaction array and you have to implement map function to convert all this ruppees into dollars
//  dollar to rs 
const transactions = [1000, 3000, 4000, 2000, -898, 3800, -4500];
const inrtToUsd = 74;

const modifiedarr2 =transactions.map(function(n)
{
    const  value =(n/inrtToUsd).toFixed(2);
    return value;
})

console.log(modifiedarr2);


// 👉ans -->

//👍when tofixed is 0
// [
//     '14',  '41',
//     '54',  '27',
//     '-12', '51',
//     '-61'
//   ]
  //👍when tofixed is 2
//   [
//     '13.51',  '40.54',
//     '54.05',  '27.03',
//     '-12.14', '51.35',
//     '-60.81'
//   ]
  
**_FILTER_**


//📌Filter 
//filter returns a new array containing array elements that matches a specified condition 

//it doesnot change the original array
//for loop ki condition ki jesi hai yeh 
//jo value true hongi wahi array mei jakar gir jayengi , aur jinki condition satisfy nahi hongi wo discard ho jauengi 



// 📌Q-1
let arr=[2,3,4,5,6,7,8,9];

let ans =arr.filter(function(n)
{
    if(n%2==0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
)

console.log(ans);

// 👉ans ->[ 2, 4, 6, 8 ]



//📌Q-2

//Filter out the profit in transactions i.e the +ve ones

const transactions = [1000, 3000, 4000, 2000, -898, 3800, -4500];


let profit=transactions.filter(function(n){

    if(n>0)
    {
        return true;
    }
    else
    {
        return false;
    }
})

console.log(profit);

//👉 ans --> [ 1000, 3000, 4000, 2000, 3800 ]

**_FILTERMAP CHAIN_**

//👉 Q-those who are female , we have to return their age 
let arr = [
    {name: "A", age: 14, gender: "M"}, 
    {name: "B", age: 34, gender: "M"}, 
    {name: "C", age: 24, gender: "F"}, 
    {name: "D", age: 44, gender: "F"}, 
    {name: "E", age: 44, gender: "M"}, 
    {name: "I", age: 28, gender: "F"}, 
    {name: "G", age: 36, gender: "M"}, 
    {name: "H", age: 47, gender: "F"}
]


let ladiesage =arr.filter(function(n)
{
    if(n.gender=='F')
    {
        return true;
    }
    else
    {
        return false;
    }
})

console.log(ladiesage);

// 👉ans -->[
//     { name: 'C', age: 24, gender: 'F' },
//     { name: 'D', age: 44, gender: 'F' },
//     { name: 'I', age: 28, gender: 'F' },
//     { name: 'H', age: 47, gender: 'F' }
//   ]

let finalans =ladiesage.map(function(n){
    return n.age; 
})

console.log(finalans);

// 👉ans -->[ 24, 44, 28, 47 ]




//this was the naive approach to the problem 

//now we will study this 



//📌📌FILTER MAP CHAIN

let ladiesage1 =arr.filter(function(n)
{
    if(n.gender=='F')
    {
        return true;
    }
    else
    {
        return false;
    }
}).map(function(n){
    return n.age
})


console.log(ladiesage1);

// 👉ans --> [ 24, 44, 28, 47 ]

Small Functions like Some,FIND AND EVERY

**_Some_**

//📌Some function


//some elements should follow the condition . ie if any of the one follow the condition then it will return true

const transactions = [1000, 3000, 4000, 2000, -898, 3800, -4500];


//displaying element that is +ve
let ans =transactions.some(function(n)
{
    return n>0
})

console.log(ans);

// 👉ans -->true


const transactions1 = [-1000, -3000, -4000, -2000, -898, -3800, -4500];

//displaying element that is +ve
let ans1 =transactions1.some(function(n)
{
    return n>0
})

console.log(ans1);

//👉 ans ->false

**_EVERY_**

//📌every function

//every element should follow the condition 

//if all the elements follow the condition then only it will return true
const transactions = [1000, 3000, -4000, 2000, -898, 3800, 4500];


//displaying element that is +ve
let ans =transactions.every(function(n)
{
    return n>0
})

console.log(ans);

//👉 ans -->false


const transactions1 = [-1000, -3000, -4000, -2000, -898, -3800, -4500];

//displaying element that is +ve
let ans1 =transactions1.every(function(n)
{
    return n<0
})

console.log(ans1);

// 👉ans ->true

**_FIND_**
//📌find

// it just works same as filter , but it doesnot give array , it finds the first value according to the condition and return 



const transactions = [1000, 3000, 4000, 2000, -898, 3800, -4500];

let ans =transactions.find(function(n)
{
    return n<0;
})

console.log(ans);

// 👉ans -->-898