Skip to main content

Command Palette

Search for a command to run...

[JavaScript] Object-Oriented Programming (OOP) with JavaScript : Coding CHallenge #1

Published
•View as Markdown
[JavaScript] Object-Oriented Programming (OOP) with JavaScript : Coding CHallenge #1
  1. Use a constructor function to implement a 'Car'. A car has a 'make' and a 'speed' property. The 'speed' property is the current speed of the car in km/h.

  2. Implement an 'accelerate' method that will increase the car's speed by 10, and log the new speed to the console.

  3. Implement a 'brake' method that will decrease the car's speed by 5, and log the new speed to the console.

  4. Create 2 'Car' objects and experiment with calling 'accelerate' and 'brake' multiple times on each of them.

Test data:

  • Data car 1: 'BMW' going at 120 km/h
  • Data car 2: 'Mercedes' going at 95 km/h
const Car = function (make, speed) {
  this.make = make;
  this.speed = speed;
};

const bmw = new Car('BMW', 120);
const mercedes = new Car('Mercedes', 95);

Car.prototype.acclerate = function () {
  this.speed += 10;
  console.log(`${this.make} is going to at ${this.speed} km/h`);
};

Car.prototype.brake = function () {
  this.speed -= 5;
  console.log(`${this.make} is going to at ${this.speed} km/h`);
};

bmw.acclerate();
bmw.acclerate();
bmw.brake();
bmw.acclerate();

mercedes.acclerate();
mercedes.acclerate();
mercedes.brake();
mercedes.acclerate();

coding challenge 1.PNG

JavaScript

Part 1 of 50

More from this blog

Jayden's Blog

166 posts