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

Multi tool use
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>
javascript jquery while-loop do-while
add a comment |
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>
javascript jquery while-loop do-while
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
add a comment |
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>
javascript jquery while-loop do-while
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
javascript jquery while-loop do-while
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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);
1
Perfect - makes a lot of sense. Thank you for your input!
– Snoops
Dec 28 '18 at 3:38
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
1
Perfect - makes a lot of sense. Thank you for your input!
– Snoops
Dec 28 '18 at 3:38
add a comment |
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);
1
Perfect - makes a lot of sense. Thank you for your input!
– Snoops
Dec 28 '18 at 3:38
add a comment |
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);
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);
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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