Challenge: Create a JavaScript 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.
- Create the Class: Define a class named
Vehicle; - Add a Constructor: Inside the
Vehicleclass:- Define a constructor that takes three parameters:
make,model, andyear; - Assign these parameters to the class properties.
- Define a constructor that takes three parameters:
- Create and Test Instances:
- Create a
Vehicleinstance namedcar1with the values"Toyota","Camry", and2020; - Create another instance named
car2with"Ford","Mustang", and2018; - Log the properties for each car instance.
- Create a
1234567891011121314151617181920class _____ { 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
- Define a class named
Vehicle; - Add a constructor with three parameters:
make,model, andyear; - Inside the constructor, assign each parameter to a property using
this; - Create an instance of
Vehiclenamedcar1with values"Toyota","Camry", and2020; - Create another instance of
Vehiclenamedcar2with values"Ford","Mustang", and2018; - Use
console.log()to display the properties ofcar1andcar2.
1234567891011121314151617181920class 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
Everything was clear?
Thanks for your feedback!
SectionΒ 1. ChapterΒ 3
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 2.22
Challenge: Create a JavaScript Class
Swipe to show menu
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.
- Create the Class: Define a class named
Vehicle; - Add a Constructor: Inside the
Vehicleclass:- Define a constructor that takes three parameters:
make,model, andyear; - Assign these parameters to the class properties.
- Define a constructor that takes three parameters:
- Create and Test Instances:
- Create a
Vehicleinstance namedcar1with the values"Toyota","Camry", and2020; - Create another instance named
car2with"Ford","Mustang", and2018; - Log the properties for each car instance.
- Create a
1234567891011121314151617181920class _____ { 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
- Define a class named
Vehicle; - Add a constructor with three parameters:
make,model, andyear; - Inside the constructor, assign each parameter to a property using
this; - Create an instance of
Vehiclenamedcar1with values"Toyota","Camry", and2020; - Create another instance of
Vehiclenamedcar2with values"Ford","Mustang", and2018; - Use
console.log()to display the properties ofcar1andcar2.
1234567891011121314151617181920class 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
Everything was clear?
Thanks for your feedback!
SectionΒ 1. ChapterΒ 3