Jump to content

Simple code is behaving oddly...


ShootingBlanks

Recommended Posts

Hello.  Please note the following code, placed on its own between the BODY tags of a webpage:

<script type="text/javascript">
var outputNumber;
var numbersGenerated;
var startNumber = prompt("Pick a number to start with","");
var endNumber = prompt("Pick a number to end with","")
var numberCount = prompt("How many random numbers would you like generated?","");
var numberDifference = Math.abs(endNumber-startNumber);

for (numbersGenerated = 0; numbersGenerated < numberCount; numbersGenerated++) {
outputNumber = (Math.floor(Math.random() * numberDifference) + 50);
document.write(numbersGenerated+1 + ". " + outputNumber + "<br />");
}
</script>

 

Enter "50" in the first prompt, then "75" in the second, then "50" in the third.  The code seems to work as I'd like (showing 50 random numbers bewtween 50 & 75)...

 

NOW, go ahead and change this line:

outputNumber = (Math.floor(Math.random() * numberDifference) + 50);

To this:

outputNumber = (Math.floor(Math.random() * numberDifference) + startNumber);

 

And the code totally messes up.  But "startNumber" IS "50", so I'm not understanding why changing it from a number to a variable would yield such different results!...

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.