Sort Function Debugging, Adding to Array.prototype, Default Array.prototype Method Behavior, Constructor Functions, and ESLint

Array.prototype.filterReduce = function(filter) {
this.arr = this.reduce( (acc, element) => {
if (filter(element)) {
acc.push(element);
}
return acc;
}, []);
return this.arr;
};
Array.prototype.mapReduce = function(map) {
this.arr = this.reduce( (acc, element) => {
acc.push(map(element));
return acc;
}, []);
return this.arr;
};
let list = ([
{ item: 'new', price: 3},
{ item: 'prototype', price: 4},
{ item: 'using', price: 5},
{ item: 'classic', price: 3 },
{ item: 'function', price: 6},
{ item: 'notation', price: 3},
]);
Array.prototype.inYourBudget = function(budget) {
return this
.filterReduce(element => element.price <= budget)
.mapReduce(element => element.item);
};
console.log(list.inYourBudget(3));
Array.prototype.sort = function() {
let items = this;
let length = this.length;
for (let i = length - 1; i >= 0; i--) {
//Number of passes
for (let j = length - i; j > 0; j--) {
//Compare the adjacent positions
if (items[j] < items[j - 1]) {
//Swap the numbers
debugger;
let tmp = items[j];
items[j] = items[j - 1];
items[j - 1] = tmp;
}
}
}
return items;
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store