Challenge: ES6 Inheritance

This challenge will test your skills in implementing inheritance and constructors in ES6 version of JavaScript.

Problem statement

In this challenge, there are two classes, Vehicle and Car. You need to implement inheritance between them so that Car is the child class and Vehicle is the parent. You have the following tasks:

  • Define a constructor for Vehicle that initializes a fuel (given in ml) property.

  • Define the turnOn and turnOff functions in Vehicle. They should display Turned on and Turned off messages.

  • Next, implement inheritance between the Vehicle and Car class. Define the Car class constructor that initializes the parameter isparked. This is a boolean variable to check whether the car is parked or not.

  • Redefine the function turnOn in Car class. If the fuel is less than 500 ml, it should display Refill Fuel or it should start the car.

  • Redefine the turnOff function in Car class. The car can only turn off if it has been parked; otherwise, it should display Car not parked.


Calling turnOn and turnOff functions


The message displaying whether the car is turned on, off, needs to refill fuel, or is not parked

Sample input

var car1 = new Car(1000,true)
var car2 = new Car(400,true)
var car3 = new Car(1500,false)

Sample output

"Turned on"
"Turned off"
"Refill Fuel"
"Turned off"
"Turned on"
"Car not parked"

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.