-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChallenge1.js
More file actions
50 lines (37 loc) · 1.04 KB
/
Challenge1.js
File metadata and controls
50 lines (37 loc) · 1.04 KB
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
function Car(make, speed) {
this.make = make;
this.speed = speed;
}
Car.prototype.accelerate = function () {
return (this.speed += 10);
};
Car.prototype.brake = function () {
return (this.speed -= 5);
};
// const bmw = new Car('BMW', 120);
// const mercedes = new Car('BMW', 95);
// console.log(bmw.accelerate());
// console.log(mercedes.accelerate());
// console.log(bmw.brake());
// console.log(mercedes.brake());
const ElectricCar = function (make, speed, battery) {
Car.call(this, make, speed);
this.battery = battery;
};
ElectricCar.prototype = Object.create(Car.prototype);
ElectricCar.prototype.chargedBattery = function (chargeTo) {
this.battery = chargeTo;
};
ElectricCar.prototype.accelerate = function () {
this.speed += 20;
this.battery--;
return `${this.make} going at this.speed km/h, with a charge of ${this.battery}`;
};
const tesla = new ElectricCar('Tesla', 120, 90);
console.log(tesla);
tesla.chargedBattery(50);
console.log(tesla);
tesla.brake();
console.log(tesla);
tesla.accelerate();
console.log(tesla);