Skip to content
This repository was archived by the owner on Oct 1, 2025. It is now read-only.

Commit a4e4daa

Browse files
committed
feat: Coding Challenge 1 of OOP Section Done
1 parent e1d2f6b commit a4e4daa

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

challenges/section14-challenges.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict';
2+
3+
// Coding Challenge 1
4+
5+
/*
6+
Your tasks:
7+
1. Use a constructor function to implement a 'Car'.A car has a 'make' and a;
8+
'speed' property.The 'speed' property is the current speed of the car in
9+
km / h;
10+
2. Implement an 'accelerate' method that will increase the car's speed by 10,
11+
and log the new speed to the console;
12+
3. Implement a 'brake' method that will decrease the car's speed by 5, and log
13+
the new speed to the console;
14+
4. Create 2 'Car' objects and experiment with calling 'accelerate' and;
15+
'brake' multiple times on each of them
16+
Test data:
17+
§ Data car 1: 'BMW' going at 120 km / h
18+
§ Data car 2: 'Mercedes' going at 95 km / h;
19+
*/
20+
21+
const Car = function (make, speed)
22+
{
23+
this.make = make;
24+
this.speed = speed;
25+
};
26+
27+
Car.prototype.logSpeed = function ()
28+
{
29+
console.log(`${ this.make } going at ${ this.speed } km/h`);
30+
};
31+
32+
Car.prototype.accelerate = function ()
33+
{
34+
this.speed += 10;
35+
this.logSpeed();
36+
};
37+
38+
Car.prototype.brake = function ()
39+
{
40+
this.speed -= 5;
41+
this.logSpeed();
42+
};
43+
44+
const car1 = new Car('BMW', 120);
45+
const car2 = new Car('Mercedes', 95);
46+
47+
for (let i = 0; i < 5; i++)
48+
car1.accelerate();
49+
50+
for (let j = 0; j < 3; j++)
51+
car2.accelerate();
52+
53+
for (let k = 0; k < 4; k++)
54+
car1.brake();

0 commit comments

Comments
 (0)