Skip to content

Instantly share code, notes, and snippets.

@kironroy
Last active July 26, 2023 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kironroy/cb1ada06aeab52fc7b06c219c1b6aa65 to your computer and use it in GitHub Desktop.
Save kironroy/cb1ada06aeab52fc7b06c219c1b6aa65 to your computer and use it in GitHub Desktop.
Classes challenges
'use strict';
// Your tasks:
// 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
function challengesTitle(str) {
console.log(`\n--- ${str} --- \n\n`);
}
challengesTitle('Challenge 1');
const Car = function (make, speed) {
this.make = make;
this.speed = speed;
};
Car.prototype.accelerate = function () {
console.log(`${this.make} is accelerating at ${this.speed + 10} m/hr`);
};
Car.prototype.brake = function () {
console.log(`${this.make} is de-accelerating at ${this.speed - 5} m/hr`);
};
const mazda = new Car('Mazda', 120);
const ford = new Car('Ford', 95);
mazda.accelerate();
mazda.brake();
ford.accelerate();
ford.brake();
challengesTitle('Challenge 2');
// 1. Re-create Challenge #1, but this time using an ES6 class (call it 'CarCl')
// 2. Add a getter called 'speedUS' which returns the current speed in mi/h (divide
// by 1.6)
// 3. Add a setter called 'speedUS' which sets the current speed in mi/h (but
// converts it to km/h before storing the value, by multiplying the input by 1.6)
// 4. Create a new car and experiment with the 'accelerate' and 'brake'
// methods, and with the getter and setter.
// Test data:
// § Data car 1: 'Ford' going at 120 km/h
class CarCl {
constructor(make, speed) {
this.make = make;
this.speed = speed;
}
accelerate() {
this.speed += 10;
console.log(`${this.make} is accelerating at ${this.speed} km/hr`);
}
brake() {
this.speed -= 5;
console.log(`${this.make} is de-accelerating at ${this.speed} km/hr`);
}
get speedUS() {
return this.speed / 1.6;
}
set speedUS(speed) {
this.speed = speed * 1.6;
}
}
const datsun = new CarCl('Datsun', 120);
console.log(datsun.speedUS);
datsun.accelerate();
datsun.accelerate();
datsun.brake();
datsun.speedUS = 50;
console.log(datsun);
challengesTitle('Challenge 3');
const ElectricCar = function (make, speed, charge) {
Car.call(this, make, speed);
this.charge = charge;
};
// link to prototypes
ElectricCar.prototype = Object.create(Car.prototype);
ElectricCar.prototype.chargeBattery = function (chargeTo) {
this.charge = chargeTo;
};
ElectricCar.prototype.accelerate = function () {
this.speed += 20;
this.charge--;
console.log(
`${this.make} is going at ${this.speed} km/h, with a charge of ${this.charge}`
);
};
const tesla = new ElectricCar('Tesla', 120, 23);
tesla.chargeBattery(90);
console.log(tesla);
tesla.brake();
tesla.accelerate();
// 1. Use a constructor function to implement an Electric Car (called 'EV') as a child
// "class" of 'Car'. Besides a make and current speed, the 'EV' also has the
// current battery charge in % ('charge' property)
// 2. Implement a 'chargeBattery' method which takes an argument
// 'chargeTo' and sets the battery charge to 'chargeTo'
// 3. Implement an 'accelerate' method that will increase the car's speed by 20,
// and decrease the charge by 1%. Then log a message like this: 'Tesla going at 140
// km/h, with a charge of 22%'
// 4. Create an electric car object and experiment with calling 'accelerate',
// 'brake' and 'chargeBattery' (charge to 90%). Notice what happens when
// you 'accelerate'! Hint: Review the definiton of polymorphism 😉
// Test data:
// § Data car 1: 'Tesla' going at 120 km/h, with a charge of 23%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment