lundi 4 juin 2018

Generating random multiples of 100

Hi am trying to generate random multiples of 100 using javascript.

I would expect the following to generate a random number between 1000 and 4000. Then check to see if it is a multiple of 100 and if so return it. And if not try generating another number.

I expected that in the else section of the if loop, rand() would just call the function again.

 function rand() {
    num = Math.floor(Math.random() * 4000) + 1000;
    if (num % 100 == 0) {
      return num;
    } else {
      rand();
    }
  }

Plunker: https://plnkr.co/edit/7tSGNiGQBUAYJsdMVeEr?p=preview

For the distances I am getting undefined instead of multiples of 100




Aucun commentaire:

Enregistrer un commentaire