How to resolve the algorithm Search a list of records step by step in the JavaScript programming language
How to resolve the algorithm Search a list of records step by step in the JavaScript programming language
Table of Contents
Problem Statement
Many programming languages provide convenient ways to look for a known value in a simple list of strings or numbers. But what if the elements of the list are themselves compound records/objects/data-structures, and the search condition is more complex than a simple equality test? Write a function/method/etc. that can find the first element in a given list matching a given condition. It should be as generic and reusable as possible. (Of course if your programming language already provides such a feature, you can use that instead of recreating it.) Then to demonstrate its functionality, create the data structure specified under #Data set, and perform on it the searches specified under #Test cases. The data structure to be used contains the names and populations (in millions) of the 10 largest metropolitan areas in Africa, and looks as follows when represented in JSON: However, you shouldn't parse it from JSON, but rather represent it natively in your programming language.
If any of that is impossible or unreasonable in your programming language, then feel free to deviate, as long as you explain your reasons in a comment above your solution. If your programming language supports higher-order programming, then the most elegant way to implement the requested functionality in a generic and reusable way, might be to write a function (maybe called "find_index" or similar), that takes two arguments: If this is not the approach which would be most natural or idiomatic in your language, explain why, and show what is.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Search a list of records step by step in the JavaScript programming language
The first code block:
The code block defines two functions: find
and findIndex
.
The find
function takes a predicate function (a function that returns a boolean) and a list as input, and returns the first element of the list that satisfies the predicate.
The findIndex
function takes a predicate function and a list as input, and returns the index of the first element of the list that satisfies the predicate.
The code then defines a list of objects, each of which represents a city and its population. Finally, the code returns an object with three properties:
darEsSalaamIndex
: The index of the city "Dar Es Salaam" in the list.firstBelow5M
: The name of the first city in the list with a population less than 5 million.firstApop
: The population of the first city in the list whose name starts with the letter "A".
The second code block:
The second code block is a lot more concise than the first one, thanks to the introduction of the Array.prototype.find
and Array.prototype.findIndex
methods in ES6.
The find
method takes a predicate function as input, and returns the first element of the array that satisfies the predicate.
The findIndex
method takes a predicate function as input, and returns the index of the first element of the array that satisfies the predicate.
The code defines a list of objects, each of which represents a city and its population. Finally, the code returns an object with three properties:
darEsSalaamIndex
: The index of the city "Dar Es Salaam" in the list.firstBelow5M
: The name of the first city in the list with a population less than 5 million.firstApop
: The population of the first city in the list whose name starts with the letter "A".
Note:
The use strict
directive at the beginning of each code block is used to enable strict mode, which helps to catch errors and enforce good coding practices.
Source code in the javascript programming language
(function () {
'use strict';
// find :: (a -> Bool) -> [a] -> Maybe a
function find(f, xs) {
for (var i = 0, lng = xs.length; i < lng; i++) {
if (f(xs[i])) return xs[i];
}
return undefined;
}
// findIndex :: (a -> Bool) -> [a] -> Maybe Int
function findIndex(f, xs) {
for (var i = 0, lng = xs.length; i < lng; i++) {
if (f(xs[i])) return i;
}
return undefined;
}
var lst = [
{ "name": "Lagos", "population": 21.0 },
{ "name": "Cairo", "population": 15.2 },
{ "name": "Kinshasa-Brazzaville", "population": 11.3 },
{ "name": "Greater Johannesburg", "population": 7.55 },
{ "name": "Mogadishu", "population": 5.85 },
{ "name": "Khartoum-Omdurman", "population": 4.98 },
{ "name": "Dar Es Salaam", "population": 4.7 },
{ "name": "Alexandria", "population": 4.58 },
{ "name": "Abidjan", "population": 4.4 },
{ "name": "Casablanca", "population": 3.98 }
];
return {
darEsSalaamIndex: findIndex(function (x) {
return x.name === 'Dar Es Salaam';
}, lst),
firstBelow5M: find(function (x) {
return x.population < 5;
}, lst)
.name,
firstApop: find(function (x) {
return x.name.charAt(0) === 'A';
}, lst)
.population
};
})();
(() => {
'use strict';
let lst = [
{ "name": "Lagos", "population": 21.0 },
{ "name": "Cairo", "population": 15.2 },
{ "name": "Kinshasa-Brazzaville", "population": 11.3 },
{ "name": "Greater Johannesburg", "population": 7.55 },
{ "name": "Mogadishu", "population": 5.85 },
{ "name": "Khartoum-Omdurman", "population": 4.98 },
{ "name": "Dar Es Salaam", "population": 4.7 },
{ "name": "Alexandria", "population": 4.58 },
{ "name": "Abidjan", "population": 4.4 },
{ "name": "Casablanca", "population": 3.98 }
];
return {
darEsSalaamIndex: lst.findIndex(x => x.name === 'Dar Es Salaam'),
firstBelow5M: lst.find(x => x.population < 5)
.name,
firstApop: lst.find(x => x.name[0] === 'A')
.population
};
})();
You may also check:How to resolve the algorithm Meissel–Mertens constant step by step in the RPL programming language
You may also check:How to resolve the algorithm Pythagorean quadruples step by step in the Rust programming language
You may also check:How to resolve the algorithm Address of a variable step by step in the QB64 programming language
You may also check:How to resolve the algorithm Sexy primes step by step in the Java programming language
You may also check:How to resolve the algorithm Idiomatically determine all the lowercase and uppercase letters step by step in the Ruby programming language