Sha256: 09d3306f36f666bb562f42a9a6d0a021d11e5c2cbb2b55d8c4b368efe046b56e

Contents?: true

Size: 1.49 KB

Versions: 139

Compression:

Stored size: 1.49 KB

Contents

export default class Clock {
  private hour: number
  private minute: number

  constructor(hour: number, minute: number = 0) {
    this.reset()
    const totalMinutes = hour * 60 + minute
    this.adjustTime(totalMinutes)
  }

  private reset(): void {
    this.hour = 0
    this.minute = 0
  }

  getHour(): number {
    return this.hour
  }

  getMinute(): number {
    return this.minute
  }

  toString(): string {
    return `${this.formatNumber(this.hour)}:${this.formatNumber(this.minute)}`
  }

  private formatNumber(numberToFormat: number): string {
    const numberString = numberToFormat.toString()
    return numberString.length === 1 ? `0${numberString}` : numberString
}

  plus(minutes: number): Clock {
    this.adjustTime(minutes)
    return this
  }

  minus(minutes: number): Clock {
    this.adjustTime(-1 * minutes)
    return this
  }

  equals(clock: Clock): boolean {
    return this.hour === clock.getHour() && this.minute === clock.getMinute()
  }

  private adjustTime(delta: number) {
    const minutesPerDay = 1440
    const minutesPerHour = 60
    const hoursPerDay = 24

    delta = Math.abs(delta) >= minutesPerDay ? delta % minutesPerDay : delta

    const currentMinutes = this.hour * minutesPerHour + this.minute
    let newMinutes = (currentMinutes + delta) % minutesPerDay

    newMinutes = newMinutes < 0 ? newMinutes += minutesPerDay : newMinutes

    this.hour = Math.floor(newMinutes / minutesPerHour) % hoursPerDay
    this.minute = newMinutes - this.hour * minutesPerHour
  }
}

Version data entries

139 entries across 139 versions & 1 rubygems

Version Path
trackler-2.2.1.98 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.97 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.96 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.95 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.94 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.93 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.92 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.91 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.90 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.89 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.88 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.87 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.86 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.85 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.84 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.83 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.82 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.81 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.80 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.79 tracks/typescript/exercises/clock/clock.example.ts