PUSHAN VERMA

Posted on

Higher Order Functions

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"}
]

{
if(n.gender=='F')
{
return true;
}
else
{
return false;
}
})

// ๐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' }
// ]

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

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

// ๐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

DEV Community

11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but itโs surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!