1

I m learning typescript by doing some exercises, I m trying to implement a clock from the typescript track in exercism.io

My clock should be able to add and substract minuts and also I should handle negative values

here is what I tried

export default class Clock {

    hours: number
    min: number 
    constructor( hours: number,  min: number = 0) {
        this.hours = hours
        this.min = min
        this.format()
    }

    format() {
        let removedHours:number , addedHours :number
        if(this.min < 0){
            removedHours = Math.floor(Math.abs(this.min ) / 60) +1
            this.hours -= removedHours
        }
        if(this.min >= 0){
            addedHours = Math.floor(Math.abs(this.min ) / 60) 
            this.hours += addedHours
        }

        this.min =  this.min % 60
        this.hours = this.hours % 24


    }

    minus(min: number) {

        this.min -= min

        this.format()
        return this
    }

    plus(min: number) {
        this.min += min
        this.format()
        return this
    }

    equals(clock: Clock) {
        return clock.hours == this.hours && clock.min == this.min
    }

    toString() {
        return this.pad(this.hours) + ':' + this.pad(this.min)
    }

    pad(num: number, size: number = 2) {
        var s = "000000000" + num;
        return s.substr(s.length - size);
    }

}

as you see My idea was to make adding and substracting stupid then formating the values starting from minutes

I have 50 unit test to be checked in order to submit

the issue is that I feel that my modulo is not working fine

-1 modulo 24 should result to 23

but as my test states the -1 is staying the same

the test is as below

      test('subtract across midnight', () => {
        expect(new Clock(0, 3).minus(4).toString()).toEqual('23:59')
      })

the result image failed test M'I wrong somewhere ?

0 Answers0