SHARE
Facebook X Pinterest WhatsApp

HTML Goodies: Script Tip: Week 17

Written By
thumbnail Joe Burns
Joe Burns
Jan 4, 2005

The Tipster… Tip-O-Rama…

Once again we’re dealing with a random number script or set of scripts (it’s actually two). Here again is what the script’s output looks like followed by the script itself.




And here’s what it all looks like:


<script language=”JavaScript”>
<!–
function RandomNumber(upper_limit)
{
return Math.round(upper_limit * Math.random());
}
//–&gt
</script>

<script language=”JavaScript”>
<!–
var upper_limit = 50;
document.write(‘Here is a random number between 0 and ‘
+ upper_limit + ‘:<P><center>’);
document.write(RandomNumber(upper_limit) + ‘</center>’);
//–>
</script>


Why does this script post a number between 1 and 50? Well, mainly because you tell it to. The upper limit can be any number you want. Let’s look at the first of the two working scripts. Here’s the function statement:

function RandomNumber(upper_limit)

Notice it follows the traditional function format except that the instance, those two parentheses, now have something written in them. That’s new, huh? That’s saying that the author wants this function to operate on that variable alone. By stating that, the author can give that variable a value later. In the case of this script, it’s a hard number written right into the code, but if you want you can set the variable to be filled in by a prompt. The prompt would pop up, ask the viewer which number should be the upper limit, and that number would be set as the “upper_limit” variable. Make sense?

Okay, so where do we get the upper limit? It comes in the second script in this line:

var upper_limit = 50;

You could have probably guessed that, though. See how it’s set as a variable?

So, look at what is happening: You create a function that will manipulate numbers to get a random number somewhere between one and “something” by using a variable called “upper_limit.” That’s all loaded into the memory. Then the second script is enacted and the variable “upper_limit” is given a value. The function runs and a number is produced. It’s actually a pretty clever and compact way of doing things.

The question now is… how do those numbers and letters get to the page? It is a mystery that will have to wait until next week.

Next Week: document.write


Learn to write your own JavaScripts with the
JavaScript Goodies!

You can find many other uses for JavaScript
on your Web pages here!

Recommended for you...

The Revolutionary ES6 Rest and Spread Operators
Rob Gravelle
Aug 23, 2022
Ahead of Time (AOT) Compilation in Angular
Tariq Siddiqui
Aug 16, 2022
Converting a JavaScript Object to a String
Rob Gravelle
Aug 14, 2022
Understanding Primitive Type Coercion in JavaScript
Rob Gravelle
Jul 28, 2022
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.