Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moumen-khadija-js-oop-class-15 #173

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
79 changes: 79 additions & 0 deletions class-15-js-OOP/moumen-khadija/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Add a static function compareSpeed that compares two cars and returns the object that is the faster car.
// Change the speed setter so that it throws an error if the caller tries to make it greater than 300km/h.
// Write a getter speedInKmSec that returns the speed in km/sec. It should use the speed in km/h.


class Car {
// Part 1. Constructor (REVIEW)
constructor(color, fuelType) {
this.color = color;
this.fuelType = fuelType;

}

// Method.
paint(newColor) {
return this.color = newColor;
}

// Part 2. Static methods.
hasTheSameProperties(car2){
return this.color === car2.color &&
this.fuelType === car2.fuelType
}

static hasTheSamePropertiesStatic(car1, car2) {
return car1.color === car2.color &&
car1.fuelType === car2.fuelType
}

// static method
static compareSpeed(car1,car2){
if( car1.speedKmH > car2.speedKmH){
return car1

}else {
return car2
}
}


// Part 3. Getter, setter.
get speedKmH() {
return this.speed
}

set speedKmH(newTopSpeedKmH) {


if (newTopSpeedKmH > 300){
console.log ("slow down")
}else {
return this.speed = newTopSpeedKmH;
}
}

get speedInMiles() {
return this.speed * 0.621371
}
get speedInHours(){
return this.speed / 3600
}
}

const audi = new Car("red","gas")
const tesla = new Car('red', "electric")

audi.paint('blue')

audi.speedKmH = 200
tesla.speedKmH = 100

// we call getter without paranthies
console.log(audi.speedInHours)


//console.log(Car.compareSpeed(audi, tesla))


// console.log(audi.hasTheSameProperties(tesla))