Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
47
Task/Search-a-list/JavaScript/search-a-list-3.js
Normal file
47
Task/Search-a-list/JavaScript/search-a-list-3.js
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
(function () {
|
||||
|
||||
function findIndex(fnPredicate, list) {
|
||||
for (var i = 0, lng = list.length; i < lng; i++) {
|
||||
if (fnPredicate(list[i])) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return Error("not found");
|
||||
};
|
||||
|
||||
// DEFINING A PARTICULAR TYPE OF SEARCH MATCH
|
||||
|
||||
function matchCaseInsensitive(s, t) {
|
||||
return s.toLowerCase() === t.toLowerCase();
|
||||
}
|
||||
|
||||
var lstHaystack = [
|
||||
'Zig', 'Zag', 'Wally', 'Ronald', 'Bush',
|
||||
'Krusty', 'Charlie', 'Bush', 'Bozo'
|
||||
],
|
||||
lstReversed = lstHaystack.slice(0).reverse(),
|
||||
iLast = lstHaystack.length - 1,
|
||||
lstNeedles = ['bush', 'washington'];
|
||||
|
||||
return {
|
||||
'first': lstNeedles.map(function (s) {
|
||||
return [s, findIndex(function (t) {
|
||||
return matchCaseInsensitive(s, t);
|
||||
},
|
||||
lstHaystack)];
|
||||
}),
|
||||
|
||||
'last': lstNeedles.map(function (s) {
|
||||
var varIndex = findIndex(function (t) {
|
||||
return matchCaseInsensitive(s, t);
|
||||
},
|
||||
lstReversed);
|
||||
|
||||
return [
|
||||
s,
|
||||
typeof varIndex === 'number' ?
|
||||
iLast - varIndex : varIndex
|
||||
];
|
||||
})
|
||||
}
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue