lundi 11 janvier 2021

Why an empty input value equal to true?

This code is suppose to execute oninput if the input is equal to the answer. Instead, if i have a multiplication problem that equals 0, once i delete the answer from the previous multiplcation problem (leaving the input empty), the result comes back true and executes the if statement. How can i prevent this?

 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);
      }
    }
}

Aucun commentaire:

Enregistrer un commentaire