JavaScript How can I run a function until it meets the desired outcome?

Consider this code. It is a gross simplification of the program I am writing.

let numberOne;
let numberTwo;

function assignNumbers() {
  numberOne = Math.random()*10
  numberTwo = Math.random()*10
}

I'm trying to figure out how to run the assignNumbers()function until both numberOne and numberTwo are less than 5. Here's what I've come up with, but I'm not sure if I'm on the right path:

if (numberOne < 5 && numberTwo < 5) {
    console.log('success');
    return;
  } else {
    //what goes in here?
  }

Of course, I could put assignNumbers() in the else block, but it could fail again. How do I ensure that assignNumbers() runs until both numberOne and NumberTwo are less than 5?

Answer:1

Just providing a one-liner:

do assignNumbers(); while (numberOne > 4 && numberTwo > 4);

Then you know both numberOne and numberTwo have the correct value.

Answer:2

You can do this either using loops or recursively (as said by Ashay Mandwarya)

// Using Loops

let numberOne;
let numberTwo;

function assignNumbers() {
  numberOne = Math.random()*10;
  numberTwo = Math.random()*10;
}

while(true){
  assignNumbers();
  if(numberOne < 5 && numberTwo < 5){
    console.log(numberOne + ' ' + numberTwo);
    break;
  }
}
Answer:3

I have a table in my html document that is styled with css. When I hard code the table rows and cells in html, the styling is working as desired. For example when I hover the mouse over a row, the ...

I have a table in my html document that is styled with css. When I hard code the table rows and cells in html, the styling is working as desired. For example when I hover the mouse over a row, the ...

I have the following array: let arr = [ {"id": 123, "lastUpdate": 1543229793}, {"id": 456, "lastUpdate": 1545269320}, {"id": 123, "lastUpdate": 1552184795} ] I need to filter the array ...

I have the following array: let arr = [ {"id": 123, "lastUpdate": 1543229793}, {"id": 456, "lastUpdate": 1545269320}, {"id": 123, "lastUpdate": 1552184795} ] I need to filter the array ...

  1. filter unique objects in array javascript
  2. filter unique objects in array
  3. filter unique objects js
  4. filter unique objects javascript
  5. filter unique objects
  6. filter unique objects in array js
  7. javascript filter unique objects by property
  8. typescript filter unique objects
  9. django filter unique objects
  10. filter out unique objects in array javascript
  11. js filter unique objects in array

Is there anyway to stream an audio file without giving complete path of the file. For example whenever we tried to play mp3 file with HTML5 Audio tag or With javascript, then i have to supply ...

Is there anyway to stream an audio file without giving complete path of the file. For example whenever we tried to play mp3 file with HTML5 Audio tag or With javascript, then i have to supply ...

  1. stream audio files
  2. stream audio files php
  3. stream audio files on ipad
  4. microsoft stream audio files
  5. stream wav audio files
  6. stream audio/video files
  7. vlc stream audio files
  8. gta sa audio stream files
  9. stream audio files dropbox

I have two .env files like dev.env and staging.env. I am using typeorm as my database ORM. I would like to know how to let typeorm read either of the config file whenever I run the application. Error: ...

I have two .env files like dev.env and staging.env. I am using typeorm as my database ORM. I would like to know how to let typeorm read either of the config file whenever I run the application. Error: ...

  1. nestjs typeorm configuration
  2. nestjs typeorm config