Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
Oh friends, the question is closed, I have an error in if(r<0.05 && lap<10), I was looking for the condition incorrectly. I apologize for the flood) If it is useful to someone (I saw questions on recursions here), then here is a working version
function a(callback){ // некая асинхронная функция с колбэком
setTimeout(function(){
var random = Math.random()
console.log(random);
callback(random);
}, 1000);
}
(function b(lap=0){
// нужно выполнять a() пока не
// получим <0.05 но не более 10 раз
a(function(r){
if(r>0.05 && lap<10) b(lap+1);
});
})(); // поехали
if(r<0.05 && lap<10) b(lap+1);
You have a 5% chance that the result will not work the first time. Therefore, almost always only one cycle.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question