Skip to content

Instantly share code, notes, and snippets.

@ultima51x
Created January 5, 2020 08:36
Show Gist options
  • Save ultima51x/efbbe09c5cdba5b1b09f8075e2ed0c2d to your computer and use it in GitHub Desktop.
Save ultima51x/efbbe09c5cdba5b1b09f8075e2ed0c2d to your computer and use it in GitHub Desktop.
hyfn-frontend-codechallenge-mysolution
<html>
<head>
<title>Cars</title>
</head>
<body>
<ul data-car-list="true">
</ul>
<script type="text/javascript" src="cars.js"></script>
</body>
</html>
<!--
Criteria
* Is it sorted right?
* Is there 10?
* Is the string right?
-->
class CarsData {
constructor() {
this.datasource = 'https://gist.github.com/scottburton11/66a921c458f9500a773a6b0ac65006df/raw/629bfd6a3125e3428bd85a53231bd8018c407a65/Javascript%2520Working%2520With%2520Data%2520Challenge%2520data';
}
// promise
carData() {
return fetch(this.datasource).then((resp) => resp.json())
}
finalList() {
return this.carData().then((json) => {
const carList = json.map(elem => new Car(elem))
const final = carList.
filter(elem => elem.coolFactor() >= 7).
sort((a, b) => b.total() - a.total()).
slice(0, 10)
return final
})
}
}
class Car {
constructor(obj) {
this.data = obj
}
fullName() {
return `${this.data['Year']} ${this.data['Make']} ${this.data['Model']}`
}
coolFactor() {
return parseInt(this.data['Cool Factor'])
}
total() {
return parseInt(this.data['Total Score'])
}
markup() {
const elem = document.createElement('li')
elem.appendChild(document.createTextNode(`${this.fullName()} - ${this.total()} - ${this.coolFactor()}`))
return elem
}
}
(function() {
const cars = new CarsData().finalList().then((list) => {
const listTag = document.querySelector('[data-car-list]')
list.forEach(car => listTag.appendChild(car.markup()))
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment