lundi 21 décembre 2020

Is there a shorter way to remove whole numbers with if statements?

This is a simplified version of the code which removes whole numbers with if statements:

function F1() {
  A = Math.floor((Math.random() * 100 + 1));
  B = Math.floor((Math.random() * 100 + 1));
  Dez = document.getElementById("Dez").innerHTML = A / B;
  if (Math.round(Dez * 10) !== Dez * 10) {
    F1();
  }
  if (Dez <= 0) {
    F1();
  }
  
  if (Dez === 1) {
    F1();
  }

  if (Dez === 2) {
    F1();
  }

//...

  if (Dez === 100) {
    F1();
  }


}
<button onclick="F1()"> New </button>
<label id="Dez"></label>

Is there a shorter way to remove whole numbers with if statements? I want a shorter solution for this part of the code:

if (Dez === 1) {
  F1();
}

if (Dez === 2) {
  F1();
}

if (Dez === 3) {
  F1();
}

...

if (Dez === 100) {
  F1();
}

Aucun commentaire:

Enregistrer un commentaire