Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge: Building a Class | Classes
Advanced JavaScript Mastery
course content

Course Content

Advanced JavaScript Mastery

Advanced JavaScript Mastery

1. Classes
2. DOM Manipulation
3. Events and Event Handling
4. Asynchronous JavaScript and APIs

book
Challenge: Building a Class

Task

You're creating a system to manage a car rental fleet. Each vehicle has specific details: the make, model, and year. Build a Vehicle class to represent these details for each car.

  1. Create the Class: Define a class named Vehicle;
  2. Add a Constructor: Inside the Vehicle class:
    • Define a constructor that takes three parameters: make, model, and year;
    • Assign these parameters to the class properties.
  3. Create and Test Instances:
    • Create a Vehicle instance named car1 with the values "Toyota", "Camry", and 2020;
    • Create another instance named car2 with "Ford", "Mustang", and 2018;
    • Log the properties for each car instance.
1234567891011121314151617181920
class _____ { constructor(_____, _____, _____) { this._____ = _____; this._____ = _____; this._____ = _____; } } // Create instances const car1 = new _____(_____, _____, _____); const car2 = new _____(_____, _____, _____); // Output the details console.log(car1._____); // Expected: Toyota console.log(car1._____); // Expected: Camry console.log(car1._____); // Expected: 2020 console.log(car2._____); // Expected: Ford console.log(car2._____); // Expected: Mustang console.log(car2._____); // Expected: 2018
copy
  • Define a class named Vehicle;
  • Add a constructor with three parameters: make, model, and year;
  • Inside the constructor, assign each parameter to a property using this;
  • Create an instance of Vehicle named car1 with values "Toyota", "Camry", and 2020;
  • Create another instance of Vehicle named car2 with values "Ford", "Mustang", and 2018;
  • Use console.log() to display the properties of car1 and car2.
1234567891011121314151617181920
class Vehicle { constructor(make, model, year) { this.make = make; this.model = model; this.year = year; } } // Create instances const car1 = new Vehicle('Toyota', 'Camry', 2020); const car2 = new Vehicle('Ford', 'Mustang', 2018); // Output the details console.log(car1.make); // Output: Toyota console.log(car1.model); // Output: Camry console.log(car1.year); // Output: 2020 console.log(car2.make); // Output: Ford console.log(car2.model); // Output: Mustang console.log(car2.year); // Output: 2018
copy

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 3
We're sorry to hear that something went wrong. What happened?
some-alt