HighOrder Problem
Mon Oct 03 2022 17:43:08 GMT+0000 (Coordinated Universal Time)
Saved by @j_jivan #javascript
// write your own find and findIndex function
const arrNums = [20, 10, 2, 30, 60, 100, 1, 4, 5];
// find method
const temp = 100;
const findNum = (num) => {
return num === temp;
};
// find method return the number if availabe else return undefine
if (arrNums.find(findNum)) {
console.log("found");
} else {
console.log("Not Found");
}
// findIndexOf method
// find index method return the index of this no if present else return -1
arrNums.findIndex(findNum);
// (Hint: using for loop and if else)
// ======================================================== //
const practiceData = [
[14, 21, 23, 64],
[33, 46, 51, 65],
];
const newArr = practiceData.map((each) => {
return each.map((each) => {
return each + 1;
});
});
console.log(newArr);
/**
* requirement one
* increment each number by one and output the resultant array using a higher order method and for loop
* HINT: use nesting
* solution = [[15,22,24,65], [34,47,52,66]]
*
*
* requirement two
* from the above array, create an array of array of even numbers using a higher order method and for loop
* solution = [[14,64], [46]]
*/
const evenArray = practiceData.map((each) => {
return each.filter((each) => {
return each % 2 === 0;
});
});
console.log(evenArray);
// ======================================================== //
const array = [23, 45, 467, 789, 34, 989, 56];
// problem => return an array such that numbers at odd idex are multiplied by 2
// and numbers at even index are multiplied by 10
const oddEven = array.map((each, index) => {
return index % 2 ? each * 2 : each * 10;
});
console.log(oddEven);
// ======================================================== //
const carBrands = ["honda", "toyota", "maruti", "tata"];
const carModel = ["city", "innova", "alto", "nano"];
const carObject = {};
for (let i = 0; i < carBrands.length; ++i) {
carObject[carBrands[i]] = carModel[i];
}
console.log(carObject);
// using a higher order method, create a carObject variable such that
/**
*
* {
* honda:city,
* toyota:innova,
* maruti:alto,
* tata:nano
* }
*/
// ======================================================== //
/**
* Flattening
Use the reduce method
to “flatten” an array of arrays into a single array that has all the elements of the original arrays.
// result = [1, 2, 3, 4, 5, 6]
*
*
*/
const arrays = [[1, 2, 3], [4, 5], [6]];
const newSingleArray = arrays.reduce((old, newA) => {
return old.concat(newA);
});
console.log(newSingleArray);
// ======================================================== //
/**
* const countries = ['Finland', 'Denmark', 'Sweden', 'Norway', 'Iceland']
* return an array with all words converted into uppercase using higher order function
* const countries = ['FINLAND', 'DENMARK', 'SWEDEN', 'NORWAY', 'ICELAND']
*
*
*
*/
const countries = ["Finland", "Denmark", "Sweden", "Norway", "Iceland"];
const countriesCapt = countries.map((country) => {
return country.toUpperCase();
});
console.log(countriesCapt);
// ======================================================== //



Comments