-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path5.js
58 lines (49 loc) · 1.63 KB
/
5.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'use strict';
// Create a constructor called CarStore, that takes an array of cars as a parameter:
// It should have an "addCar" method that adds a car object to it's list
// It should have a "getSumPrice" method that returns the sum of all cars price
// It should have a "getOldestCarType" method that returns the oldest car's type
// It should have a "deleteCarByType" method that removes the cars from the inner list
// that have the given type
function CarStore(cars) {
this.cars = cars;
this.addCar = function(type, price, year) {
cars.push({type: type, price: price, year: year})
}
this.getSumPrice = function() {
var result = 0;
cars.forEach(function(car) {
result += car.price
});
return result;
};
this.getOldestCarType = function() {
var oldest = cars[0];
for (var i = 0; i < cars.length; i++) {
if (oldest.year > cars[i].year) {
oldest = cars[i];
}
}
return oldest.type;
};
this.deleteCarByType = function(car) {
for (var i = 0; i < cars.length; i++) {
if (cars[i].type === car) {
cars.splice(i, 1);
}
}
}
};
var cars = [
{type: 'Dodge', price: 20000, year: 2012},
{type: 'Tesla', price: 30000, year: 2015},
{type: 'Nissan', price: 12000, year: 2010},
{type: 'Trabant', price: 2000, year: 1980},
{type: 'Ferrari', price: 40000, year: 2001}
];
var carStore = new CarStore(cars);
carStore.addCar('Smart', 18000, 2011);
console.log(carStore.getSumPrice()); // 122000
console.log(carStore.getOldestCarType()); // 'Trabant'
carStore.deleteCarByType('Ferrari');
console.log(carStore.getSumPrice()); // 82000