Challenge: Implement Getters and Setters in a Class
Task
You are working within a Car class that has a private #mileage property. Your task is to define getters and setters for this property to control how itβs accessed and modified.
- Define Getter for Mileage: Define a getter for the
mileageproperty to retrieve the current mileage; - Define Setter for Mileage with Validation: Define a setter for the
mileageproperty that accepts a new mileage value. This setter should:- Only allow updating if the new mileage is greater than the current mileage (mileage can only increase);
- If the new mileage is invalid, log
"Invalid mileage update"and do not change the current mileage.
123456789101112131415161718192021222324252627class Car { #mileage = 1000; // Initial private mileage // Getter for mileage _____ mileage() { return this.#_____; } // Setter for mileage with validation _____ mileage(newMileage) { _____ (_____ > this.#_____) { this.#_____ = newMileage; } _____ { console.log(_____); } } } // Testing const car1 = new Car(); console.log(car1.mileage); // Expected: 1000 car1.mileage = 1500; // Update mileage to 1500 console.log(car1.mileage); // Expected: 1500 car1.mileage = 900; // Attempt invalid mileage update, expected: Invalid mileage update
- Define a
getaccessor formileageto retrieve the current mileage; - Define a
setaccessor formileagethat takesnewMileageas a parameter; - In the setter, check if
newMileageis greater than the current mileage (this.#mileage); - If
newMileageis valid, update#mileagetonewMileage; - If
newMileageis not greater than the current mileage, log"Invalid mileage update"and do not change the mileage.
123456789101112131415161718192021222324252627class Car { #mileage = 1000; // Initial private mileage // Getter for mileage get mileage() { return this.#mileage; } // Setter for mileage with validation set mileage(newMileage) { if (newMileage > this.#mileage) { this.#mileage = newMileage; } else { console.log('Invalid mileage update'); } } } // Testing const car1 = new Car(); console.log(car1.mileage); // Output: 1000 car1.mileage = 1500; // Update mileage to 1500 console.log(car1.mileage); // Output: 1500 car1.mileage = 900; // Attempt invalid mileage update, expected: Invalid mileage update
Everything was clear?
Thanks for your feedback!
SectionΒ 1. ChapterΒ 10
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: Implement Getters and Setters in a Class
Swipe to show menu
Task
You are working within a Car class that has a private #mileage property. Your task is to define getters and setters for this property to control how itβs accessed and modified.
- Define Getter for Mileage: Define a getter for the
mileageproperty to retrieve the current mileage; - Define Setter for Mileage with Validation: Define a setter for the
mileageproperty that accepts a new mileage value. This setter should:- Only allow updating if the new mileage is greater than the current mileage (mileage can only increase);
- If the new mileage is invalid, log
"Invalid mileage update"and do not change the current mileage.
123456789101112131415161718192021222324252627class Car { #mileage = 1000; // Initial private mileage // Getter for mileage _____ mileage() { return this.#_____; } // Setter for mileage with validation _____ mileage(newMileage) { _____ (_____ > this.#_____) { this.#_____ = newMileage; } _____ { console.log(_____); } } } // Testing const car1 = new Car(); console.log(car1.mileage); // Expected: 1000 car1.mileage = 1500; // Update mileage to 1500 console.log(car1.mileage); // Expected: 1500 car1.mileage = 900; // Attempt invalid mileage update, expected: Invalid mileage update
- Define a
getaccessor formileageto retrieve the current mileage; - Define a
setaccessor formileagethat takesnewMileageas a parameter; - In the setter, check if
newMileageis greater than the current mileage (this.#mileage); - If
newMileageis valid, update#mileagetonewMileage; - If
newMileageis not greater than the current mileage, log"Invalid mileage update"and do not change the mileage.
123456789101112131415161718192021222324252627class Car { #mileage = 1000; // Initial private mileage // Getter for mileage get mileage() { return this.#mileage; } // Setter for mileage with validation set mileage(newMileage) { if (newMileage > this.#mileage) { this.#mileage = newMileage; } else { console.log('Invalid mileage update'); } } } // Testing const car1 = new Car(); console.log(car1.mileage); // Output: 1000 car1.mileage = 1500; // Update mileage to 1500 console.log(car1.mileage); // Output: 1500 car1.mileage = 900; // Attempt invalid mileage update, expected: Invalid mileage update
Everything was clear?
Thanks for your feedback!
SectionΒ 1. ChapterΒ 10