Why won't my do-while loop work? (multiple variables generated within function)

Multi tool use
Multi tool use












1














I have a function that generates two numbers. The function has a loop to keep generating numbers until it finds two whose product divides evenly into 24. This often works, but occasionally fails to do so.






function randomBetween(min, max) {
var ceiling = max + 1;
return Math.floor(Math.random() * (ceiling - min)) + min;
}


function DOBINGenerateNonWhole() {
var random;
var random2;

do {
random = randomBetween(3, 36);
random2 = randomBetween(3, 36);

} while ((24 % (random * random2) != 0));
return {
random: random,
random2: random2,

}
}

var Span1 = DOBINGenerateNonWhole().random;
$('.Span1').html(Span1);

var Span2 = DOBINGenerateNonWhole().random2;
$('.Span2').html(Span2);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="Span1"></div>
<div class="Span2"></div>












share|improve this question
























  • You can paste your code into any editor which has an auto-formatting option (such as the one here on SO on JSFiddle, there are many more).
    – CertainPerformance
    Dec 28 '18 at 3:26










  • From JSFiddle, press "tidy", then select everything and press tab twice, and then you can copy it into a Markdown editor (like SO's)
    – CertainPerformance
    Dec 28 '18 at 3:28










  • Okay, I figured it out. Does anyone have any advice that's actually relevant to the issue? I'd be much obliged.
    – Snoops
    Dec 28 '18 at 3:28






  • 1




    @Snoops What does "occasionally fails to do so" mean? What does it do when it fails to do so?
    – JLRishe
    Dec 28 '18 at 3:31












  • It generates two numbers whose product is not a factor of 24. (I just ran the JSFiddle a couple times, for example, and it set the two variables as 3 & 6.)
    – Snoops
    Dec 28 '18 at 3:31


















1














I have a function that generates two numbers. The function has a loop to keep generating numbers until it finds two whose product divides evenly into 24. This often works, but occasionally fails to do so.






function randomBetween(min, max) {
var ceiling = max + 1;
return Math.floor(Math.random() * (ceiling - min)) + min;
}


function DOBINGenerateNonWhole() {
var random;
var random2;

do {
random = randomBetween(3, 36);
random2 = randomBetween(3, 36);

} while ((24 % (random * random2) != 0));
return {
random: random,
random2: random2,

}
}

var Span1 = DOBINGenerateNonWhole().random;
$('.Span1').html(Span1);

var Span2 = DOBINGenerateNonWhole().random2;
$('.Span2').html(Span2);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="Span1"></div>
<div class="Span2"></div>












share|improve this question
























  • You can paste your code into any editor which has an auto-formatting option (such as the one here on SO on JSFiddle, there are many more).
    – CertainPerformance
    Dec 28 '18 at 3:26










  • From JSFiddle, press "tidy", then select everything and press tab twice, and then you can copy it into a Markdown editor (like SO's)
    – CertainPerformance
    Dec 28 '18 at 3:28










  • Okay, I figured it out. Does anyone have any advice that's actually relevant to the issue? I'd be much obliged.
    – Snoops
    Dec 28 '18 at 3:28






  • 1




    @Snoops What does "occasionally fails to do so" mean? What does it do when it fails to do so?
    – JLRishe
    Dec 28 '18 at 3:31












  • It generates two numbers whose product is not a factor of 24. (I just ran the JSFiddle a couple times, for example, and it set the two variables as 3 & 6.)
    – Snoops
    Dec 28 '18 at 3:31
















1












1








1







I have a function that generates two numbers. The function has a loop to keep generating numbers until it finds two whose product divides evenly into 24. This often works, but occasionally fails to do so.






function randomBetween(min, max) {
var ceiling = max + 1;
return Math.floor(Math.random() * (ceiling - min)) + min;
}


function DOBINGenerateNonWhole() {
var random;
var random2;

do {
random = randomBetween(3, 36);
random2 = randomBetween(3, 36);

} while ((24 % (random * random2) != 0));
return {
random: random,
random2: random2,

}
}

var Span1 = DOBINGenerateNonWhole().random;
$('.Span1').html(Span1);

var Span2 = DOBINGenerateNonWhole().random2;
$('.Span2').html(Span2);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="Span1"></div>
<div class="Span2"></div>












share|improve this question















I have a function that generates two numbers. The function has a loop to keep generating numbers until it finds two whose product divides evenly into 24. This often works, but occasionally fails to do so.






function randomBetween(min, max) {
var ceiling = max + 1;
return Math.floor(Math.random() * (ceiling - min)) + min;
}


function DOBINGenerateNonWhole() {
var random;
var random2;

do {
random = randomBetween(3, 36);
random2 = randomBetween(3, 36);

} while ((24 % (random * random2) != 0));
return {
random: random,
random2: random2,

}
}

var Span1 = DOBINGenerateNonWhole().random;
$('.Span1').html(Span1);

var Span2 = DOBINGenerateNonWhole().random2;
$('.Span2').html(Span2);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="Span1"></div>
<div class="Span2"></div>








function randomBetween(min, max) {
var ceiling = max + 1;
return Math.floor(Math.random() * (ceiling - min)) + min;
}


function DOBINGenerateNonWhole() {
var random;
var random2;

do {
random = randomBetween(3, 36);
random2 = randomBetween(3, 36);

} while ((24 % (random * random2) != 0));
return {
random: random,
random2: random2,

}
}

var Span1 = DOBINGenerateNonWhole().random;
$('.Span1').html(Span1);

var Span2 = DOBINGenerateNonWhole().random2;
$('.Span2').html(Span2);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="Span1"></div>
<div class="Span2"></div>





function randomBetween(min, max) {
var ceiling = max + 1;
return Math.floor(Math.random() * (ceiling - min)) + min;
}


function DOBINGenerateNonWhole() {
var random;
var random2;

do {
random = randomBetween(3, 36);
random2 = randomBetween(3, 36);

} while ((24 % (random * random2) != 0));
return {
random: random,
random2: random2,

}
}

var Span1 = DOBINGenerateNonWhole().random;
$('.Span1').html(Span1);

var Span2 = DOBINGenerateNonWhole().random2;
$('.Span2').html(Span2);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="Span1"></div>
<div class="Span2"></div>






javascript jquery while-loop do-while






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '18 at 3:30









JLRishe

75.5k1077116




75.5k1077116










asked Dec 28 '18 at 3:23









Snoops

12010




12010












  • You can paste your code into any editor which has an auto-formatting option (such as the one here on SO on JSFiddle, there are many more).
    – CertainPerformance
    Dec 28 '18 at 3:26










  • From JSFiddle, press "tidy", then select everything and press tab twice, and then you can copy it into a Markdown editor (like SO's)
    – CertainPerformance
    Dec 28 '18 at 3:28










  • Okay, I figured it out. Does anyone have any advice that's actually relevant to the issue? I'd be much obliged.
    – Snoops
    Dec 28 '18 at 3:28






  • 1




    @Snoops What does "occasionally fails to do so" mean? What does it do when it fails to do so?
    – JLRishe
    Dec 28 '18 at 3:31












  • It generates two numbers whose product is not a factor of 24. (I just ran the JSFiddle a couple times, for example, and it set the two variables as 3 & 6.)
    – Snoops
    Dec 28 '18 at 3:31




















  • You can paste your code into any editor which has an auto-formatting option (such as the one here on SO on JSFiddle, there are many more).
    – CertainPerformance
    Dec 28 '18 at 3:26










  • From JSFiddle, press "tidy", then select everything and press tab twice, and then you can copy it into a Markdown editor (like SO's)
    – CertainPerformance
    Dec 28 '18 at 3:28










  • Okay, I figured it out. Does anyone have any advice that's actually relevant to the issue? I'd be much obliged.
    – Snoops
    Dec 28 '18 at 3:28






  • 1




    @Snoops What does "occasionally fails to do so" mean? What does it do when it fails to do so?
    – JLRishe
    Dec 28 '18 at 3:31












  • It generates two numbers whose product is not a factor of 24. (I just ran the JSFiddle a couple times, for example, and it set the two variables as 3 & 6.)
    – Snoops
    Dec 28 '18 at 3:31


















You can paste your code into any editor which has an auto-formatting option (such as the one here on SO on JSFiddle, there are many more).
– CertainPerformance
Dec 28 '18 at 3:26




You can paste your code into any editor which has an auto-formatting option (such as the one here on SO on JSFiddle, there are many more).
– CertainPerformance
Dec 28 '18 at 3:26












From JSFiddle, press "tidy", then select everything and press tab twice, and then you can copy it into a Markdown editor (like SO's)
– CertainPerformance
Dec 28 '18 at 3:28




From JSFiddle, press "tidy", then select everything and press tab twice, and then you can copy it into a Markdown editor (like SO's)
– CertainPerformance
Dec 28 '18 at 3:28












Okay, I figured it out. Does anyone have any advice that's actually relevant to the issue? I'd be much obliged.
– Snoops
Dec 28 '18 at 3:28




Okay, I figured it out. Does anyone have any advice that's actually relevant to the issue? I'd be much obliged.
– Snoops
Dec 28 '18 at 3:28




1




1




@Snoops What does "occasionally fails to do so" mean? What does it do when it fails to do so?
– JLRishe
Dec 28 '18 at 3:31






@Snoops What does "occasionally fails to do so" mean? What does it do when it fails to do so?
– JLRishe
Dec 28 '18 at 3:31














It generates two numbers whose product is not a factor of 24. (I just ran the JSFiddle a couple times, for example, and it set the two variables as 3 & 6.)
– Snoops
Dec 28 '18 at 3:31






It generates two numbers whose product is not a factor of 24. (I just ran the JSFiddle a couple times, for example, and it set the two variables as 3 & 6.)
– Snoops
Dec 28 '18 at 3:31














1 Answer
1






active

oldest

votes


















3














You are running DOBINGenerateNonWhole() twice which is causing the issue. random and random2 are not from the same invocation so there is no reason for their product to be a factor of 24



If you update your code to store the result it should work:



var result = DOBINGenerateNonWhole();
$('.Span1').html(result.random);
$('.Span2').html(result.random2);





share|improve this answer



















  • 1




    Perfect - makes a lot of sense. Thank you for your input!
    – Snoops
    Dec 28 '18 at 3:38











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53953320%2fwhy-wont-my-do-while-loop-work-multiple-variables-generated-within-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














You are running DOBINGenerateNonWhole() twice which is causing the issue. random and random2 are not from the same invocation so there is no reason for their product to be a factor of 24



If you update your code to store the result it should work:



var result = DOBINGenerateNonWhole();
$('.Span1').html(result.random);
$('.Span2').html(result.random2);





share|improve this answer



















  • 1




    Perfect - makes a lot of sense. Thank you for your input!
    – Snoops
    Dec 28 '18 at 3:38
















3














You are running DOBINGenerateNonWhole() twice which is causing the issue. random and random2 are not from the same invocation so there is no reason for their product to be a factor of 24



If you update your code to store the result it should work:



var result = DOBINGenerateNonWhole();
$('.Span1').html(result.random);
$('.Span2').html(result.random2);





share|improve this answer



















  • 1




    Perfect - makes a lot of sense. Thank you for your input!
    – Snoops
    Dec 28 '18 at 3:38














3












3








3






You are running DOBINGenerateNonWhole() twice which is causing the issue. random and random2 are not from the same invocation so there is no reason for their product to be a factor of 24



If you update your code to store the result it should work:



var result = DOBINGenerateNonWhole();
$('.Span1').html(result.random);
$('.Span2').html(result.random2);





share|improve this answer














You are running DOBINGenerateNonWhole() twice which is causing the issue. random and random2 are not from the same invocation so there is no reason for their product to be a factor of 24



If you update your code to store the result it should work:



var result = DOBINGenerateNonWhole();
$('.Span1').html(result.random);
$('.Span2').html(result.random2);






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 28 '18 at 3:36









JLRishe

75.5k1077116




75.5k1077116










answered Dec 28 '18 at 3:34









Mike Gillett

536413




536413








  • 1




    Perfect - makes a lot of sense. Thank you for your input!
    – Snoops
    Dec 28 '18 at 3:38














  • 1




    Perfect - makes a lot of sense. Thank you for your input!
    – Snoops
    Dec 28 '18 at 3:38








1




1




Perfect - makes a lot of sense. Thank you for your input!
– Snoops
Dec 28 '18 at 3:38




Perfect - makes a lot of sense. Thank you for your input!
– Snoops
Dec 28 '18 at 3:38


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53953320%2fwhy-wont-my-do-while-loop-work-multiple-variables-generated-within-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







FIqxndt Ef WH6zAGFEQ vMRnNs1IRzna6OPcIp0l29EHxYykZ,yvNh jPj8C piA,y q57,px0,UBT
4ogPyCWlif8khp79,249RRN9i7H0UpMbjX0x4g1D QnSQZVo,7Gja2Or3sw5IY20VjQzJQnqJtL 0bQ,o2tYfUnkCkghXowFaE,ckwl,d

Popular posts from this blog

Monofisismo

compose and upload a new article using a custom form

“attempting to read past stream EOM” using Sybase.AdoNet4.AseClient