-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcar.js
More file actions
52 lines (52 loc) · 1.15 KB
/
car.js
File metadata and controls
52 lines (52 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Car
{
constructor(brand,model)
{
this.brand=brand;
this.model=model;
this.speed=0;
}
accelerate(s)
{
let newspeed=this.speed+s;
if(newspeed>140)
console.log("OverSpeed Can't Increase!");
else
this.speed=newspeed;
}
decelerate(s)
{
let newspeed=this.speed-s;
if(newspeed<0)
{
this.speed=0;
console.log("Car Stopped");
}
else
this.speed=newspeed;
}
brake()
{
console.log("Car is stopped after brake");
this.speed=0;
}
status()
{
console.log("Brand: "+this.brand+"\nModel: "+this.model+"\nCurrentSpeed: "+this.speed);
if(this.speed!=0)
console.log("Status: Running\n");
else
console.log("Status: Stopped\n");
}
}
let c1=new Car('Hyundai','i10');
c1.status();
console.log("After Acceleration of 40kmph");
c1.accelerate(40);
c1.status();
console.log("After Deceleration of 20kmph");
c1.decelerate(20);
c1.status();
console.log("After Break");
c1.brake();
c1.status();