typescript - 打字稿模数不起作用:测试失败
问题描述
我正在通过做一些练习来学习打字稿,我正在尝试从 exercism.io 中的打字稿轨道实现一个时钟
我的时钟应该能够加减分钟,而且我应该处理负值
这是我尝试过的
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);
}
}
如您所见,我的想法是使加减法变得愚蠢,然后从几分钟开始格式化值
我有 50 个单元测试要检查才能提交
问题是我觉得我的模不能正常工作
-1 模 24 应该得到 23
但正如我的测试所述 -1 保持不变
测试如下
test('subtract across midnight', () => {
expect(new Clock(0, 3).minus(4).toString()).toEqual('23:59')
})