-
Notifications
You must be signed in to change notification settings - Fork 0
/
Workout.js
53 lines (44 loc) · 1.32 KB
/
Workout.js
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
53
class Workout {
id = crypto.randomUUID();
date = new Date();
constructor(coords, distance, duration) {
this.coords = coords; // [lat, lng]
this.distance = distance; // in km
this.duration = duration; // in min
}
_setDescription() {
// prettier-ignore
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
this.description = `${this.type[0].toUpperCase()}${this.type.slice(1)} on ${
months[this.date.getMonth()]
} ${this.date.getDate()}`;
}
}
export class Running extends Workout {
type = 'running';
constructor(coords, distance, duration, cadence) {
super(coords, distance, duration);
this.cadence = cadence; // steps/min
this.calcPace();
this._setDescription();
}
calcPace() {
// min/km
this.pace = +(this.duration / this.distance).toFixed(2);
return this.pace;
}
}
export class Cycling extends Workout {
type = 'cycling';
constructor(coords, distance, duration, elevationGain) {
super(coords, distance, duration);
this.elevationGain = +elevationGain.toFixed(1); // in meters
this.calcSpeed();
this._setDescription();
}
calcSpeed() {
// km/h
this.speed = +(this.distance / (this.duration / 60)).toFixed(2);
return this.speed;
}
}