Skip to content

Commit a929efc

Browse files
committed
inclass files from js2week2 improvement
1 parent 07ba4f5 commit a929efc

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

javascript2/week2/inclass/ex2.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@
88
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
99
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
1010
</head>
11-
<body class="bg-light">
11+
<body>
1212
<div class="container">
1313
<h1 class="text-center">
1414
Listings
1515
</h1>
16+
<div class="card">
17+
<div class="card-body">
18+
<h5 class="card-title">Filter options</h5>
19+
<p class="card-text" id="filter">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
20+
</div>
21+
</div>
22+
23+
<div class="card" id="filter"></div>
1624
<div class="table-responsive">
1725
<table class="table table-hover">
1826
<thead class="table-info">

javascript2/week2/inclass/ex2.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,25 @@ function generateListings(numberOfListings) {
3434
return listings;
3535
}
3636
const tableBodyTag = document.getElementById("tableBody")
37-
const listings = generateListings(37);
37+
const listings = generateListings(1000);
3838

3939
// Filter listings
4040
const filter = {
4141
type: 'farm',
42+
facilities: ['Parkering', 'Husdyr'],
4243
minPrice: 1500,
4344
maxPrice: 8000,
45+
hasGarden: false,
46+
minSize: 50,
47+
maxSize: 700
4448
};
4549

50+
// add filter do DOM with id filter
51+
const filterTag = document.getElementById("filter")
52+
// JSON stringify with line breaks and spaces
53+
filterTag.innerHTML = JSON.stringify(filter, null, 2)
54+
55+
4656
// const filterListings = (listings, filter) => {
4757
// const filteredItems = listings
4858
// .filter(listing => listing.type.toLowerCase() === filter.type)
@@ -53,13 +63,24 @@ const filter = {
5363
const filterListings = (listings, filter) => {
5464
const allowedKeys = ["type", "facilities", "minPrice", "maxPrice", "hasGarden", "minSize", "maxSize"]
5565
const filteredKeys = Object.keys(filter).filter(element => allowedKeys.includes(element))
56-
// console.log(filteredKeys)
66+
console.log(filteredKeys)
5767

5868
const filterFunctions = {
59-
type: listing => listing.type.toLowerCase() === filter.type,
69+
type: listing => listing.type.toLowerCase() === filter.type.toLowerCase(),
70+
// next line is a bit more complicated, but it works
71+
// it checks if the filter.facilities array includes any of the listing.facilities
72+
// for filter.facilities.every see:
73+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
74+
facilities: listing => filter.facilities.every(facility => {
75+
const listingFacilities = listing.facilities.map(facility => facility.toLowerCase())
76+
const filterFacility = facility.toLowerCase()
77+
return listingFacilities.includes(filterFacility)
78+
}),
6079
minPrice: listing => listing.price >= filter.minPrice,
6180
maxPrice: listing => listing.price <= filter.maxPrice,
62-
// add key and filtering function for each allowedKey from allowedKeys
81+
hasGarden: listing => listing.hasGarden === filter.hasGarden,
82+
minSize: listing => listing.size >= filter.minSize,
83+
maxSize: listing => listing.size <= filter.maxSize,
6384
}
6485

6586
filteredKeys.forEach(key => {

0 commit comments

Comments
 (0)