DEV Community

PUSHAN VERMA
PUSHAN VERMA

Posted on

Must Do Interview Questions for Higher Order Functions

// πŸ“ŒQ-1 Get the number of products whose price is atleast 100

const products = [
{ name: "T-Shirt", price: 25 },
{ name: "Headphones", price: 125 },
{ name: "Keyboard", price: 75 },
{ name: "Monitor", price: 200 },
];

// let prouductabove100 =products.map(function(products){
// return products.price
// }).filter(function(price){
// return price>=100
// }).length

// πŸ‘‰ans->2 (correct one )

//πŸ“ŒApproach -2

let productabove100 =products.filter(function(item){
return item.price>=100
}).map(function(item){
return item.name
}).length

console.log(productabove100);

//πŸ‘‰ ans -> 2(also correct one 😊)
Enter fullscreen mode Exit fullscreen mode

//πŸ“Œ Q-2 //Get the movie Names from this Array of Objects
//method and only get the movie name which has rating higher than or equal to 8
// use Filter and map chaining to achaive the objective

var newReleases = [
{
"id": 1,
"title": "Die Hard",
"rating":9

},
{
       "id": 2,
       "title": "Bad Boys",
        "rating":7

},
{
       "id": 3,
       "title": "The Chamber",
       "rating": 10

},
Enter fullscreen mode Exit fullscreen mode

];

// soltion---->
let moviename =newReleases.filter(function(movie){
return movie.rating>=8
}).map(function(movie){
return movie.title
})

console.log(moviename);

// πŸ‘‰ans ->[ 'Die Hard', 'The Chamber' ]

//πŸ“Œ Q-3

//πŸ‘‰(a)You have to use map function and have to get all the students name in upperCase
//Map Question

//πŸ‘‰(b)Retrieve the details of students who scored more than 50 marks and have id greater than 120 from studentRecord
//use filter method to approach the problem
let studentRecords = [ {name: 'Abhishek', id: 123, marks : 98 },
{name: 'Udai', id: 101, marks : 90 },
{name: 'Himanshu', id: 200, marks : 96 },
{name: 'Mrinal', id: 115, marks : 75 } ]

// solution -(a)

let ans1 =studentRecords.map(function(student){
return student.name.toUpperCase();
})

console.log(ans1);

// πŸ‘‰ans ->[ 'ABHISHEK', 'UDAI', 'HIMANSHU', 'MRINAL' ]

let ans2=studentRecords.filter(function(stu){
if(stu.marks>50 && stu.id>120)
return stu
})

console.log(ans2);

// πŸ‘‰ans->[
// { name: 'Abhishek', id: 123, marks: 98 },
// { name: 'Himanshu', id: 200, marks: 96 }
// ]

// πŸ“ŒπŸ“ŒReduce questions

// From the transaction array filter out positive Elements and calculate the total amount
// Use filter and reduce method

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

let ans =transactions.filter(function(transactions){
return transactions>0
}).reduce(function(answer,value){
let sum =answer+value
return sum
},0)

console.log(ans);

// πŸ‘‰ans ->13800

Oldest comments (0)