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.78 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.77 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.76 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.75 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.74 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.73 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.72 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.71 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.70 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.69 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.68 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.67 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.66 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.65 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.64 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.63 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.62 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.61 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.60 tracks/typescript/exercises/clock/clock.example.ts
trackler-2.2.1.59 tracks/typescript/exercises/clock/clock.example.ts