【发布时间】:2021-01-11 10:22:43
【问题描述】:
如果输入等于答案,则假设此代码执行 oninput。 相反,如果我有一个等于 0 的乘法问题,一旦我从前一个乘法问题中删除答案(让输入为空),结果就会返回 true 并执行 if 语句。我怎样才能防止这种情况发生?
ngOnInit() { this.multiply(); }
f1 = 0;
f2 = 0;
answer:number = 0;
input:number = 0;
multiply():void{
this.f1 = Math.floor(Math.random() * 12);
this.f2 = Math.floor(Math.random() * 12);
this.answer = this.f1 * this.f2;
console.log(this.answer);
}
checkAnswer(event){
this.input = event.target.value;
if(this.input == this.answer){
console.log(this.input + " " + "is correct!");
this.multiply();
} else{
console.log(this.input + " " + "is incorrect" + " " + this.answer);
}
}
}
【问题讨论】:
-
试试
this.input = event.target.value.trim(); -
尝试使用 "===" 而不是 "==" 我更新了下面的答案
标签: javascript angular if-statement input null