Skip to content

Instantly share code, notes, and snippets.

@dmitru
Created April 20, 2017 15:48
Show Gist options
  • Save dmitru/6b811f790df2f4cce3717750578cd77d to your computer and use it in GitHub Desktop.
Save dmitru/6b811f790df2f4cce3717750578cd77d to your computer and use it in GitHub Desktop.
keksobooking-module7-task1 - filters example
'use strict';
/* global utils */
(function () {
var filteredList = [];
var filterContainer = document.querySelector('.tokyo__filters-container');
var filterHousing = filterContainer.querySelector('#housing_type');
var filterPrice = filterContainer.querySelector('#housing_price');
var filterRoomNumber = filterContainer.querySelector('#housing_room-number');
var filterGuestsNumber = filterContainer.querySelector('#housing_guests-number');
var filterFeatures = filterContainer.querySelector('#housing_features');
var filterList = [
// Takes ONE object, returns true / false
function priceFilter(data) {
if (filterPrice.value === 'middle') {
return data.offer.price > 10000 && data.offer.price < 50000;
} else if (filterPrice.value === 'low') {
return data.offer.price < 10000;
} else {
return data.offer.price > 50000;
}
},
function roomNumberFilter(data) {
var filterRoomNumberValue = filterRoomNumber.value;
if (filterPriceValue === '1') {
return data.offer.rooms === 1;
} else if (filterPriceValue === '2') {
return data.offer.rooms === 2;
} else if (filterPriceValue === '3') {
return data.offer.rooms === 3;
} else {
return true;
}
},
// ...
// And so on...
// ...
];
function filterAllFields(data) {
for (var i = 0; i < filterList.length; ++i) {
var filter = filterList[i];
if (!filter(data)) {
return false;
}
}
return true;
}
window.filter = function (offerList) {
return offerList.filter(filterAllFields);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment