Chapter 7: Functions and Randomness
- pragmatic design
- A little of both. Planning will help get the job done in a more organized fashion. Trying out detail tasks in small programs will ensure that you can in fact accomplish the end goal.
- functions as problem solvers
- functions that give answers. Functions that give answers do so by returning a value. The value can be used in any JS expression. Examples of functions you have used that return a value:
var x = window.prompt("enter value", "1"); // value is text string the user enters
var y = document.getElementById("someID").value; // value is a property of some element
if ( isNaN(x) ) x = 0; // value is true or false
- predefined functions
- Javascript has many pre-defined functions for us to use including Math functions such as square root and rounding numbers.
Practical Example
var textAmt = document.getElementById("liters").value;;
var liters = parseFloat(textAmt); // now it is a number
var rounded = Math.round(liters*10)/10; // now its rounded to 1 decimal places
There are numerous Math functions. Some of the more frequently used are:
Math.floor (3.7); // return the integer below 3.7 (i.e., 3)
Math.ceil (4.2); // return the integer above 4.2 (i.e., 5)
Math.round (3.7); // round the integer 3.7 (i.e., 4)
Math.sqrt (25); // calculate square root
Math.min (x, y); // return the smallest of x and y
Math.max (x, y); // return the largest of x and y
Math functions can be called from within Javascript code as:
1 + Math.sqrt(4)
alert(Math.sqrt(4));
We can also write our own functions. Have you noticed how messy the onClick
is getting? Let's get that code out of the onClick and put it elsewhere.
- We generally place our javascript functions in the HEAD section of html
- functions just perform a sequence of tasks
Daily Chores
- Finish all homework
- Clean room
- Put away laundry
|
Weekly Chores
- Cut the grass
- Put out the trash
- Put away video games
|
- Each has a unique name: we don't want to get the shopping list confused with a list of chores.
- In programming we use this kind of function to
- Make the code look neater and more readable
- Test the code once and re-use it many times
- Make the code easier to maintain
- Try it
<script language = 'JavaScript'>
var shots = 1;
var drink = "none";
var ounce = 0;
</script>
<input type = button value = "Clear"
onClick = 'shots = 1;
drink = "none";
ounce = 0;
document.Bean.price.value = "0.00"'>
|
<script >
var shots = 1;
var drink = "none";
var ounce = 0;
</script>
function clearSettings()
{
shots = 1;
drink = "none";
ounce = 0;
document.Bean.price.value = "0.00"
}
<input type = button value = "Clear"
onClick = 'clearSettings()'>
|
PRACTICE
- Let's create a function from our previous example of converting pounds to kilograms
- Next, add html and a function to convert feet to meters (1 foot = 0.3048 meters
Random Function
- Math.random generates a pseudo-random number in the range [0…1)
- pseudo-random refers to the fact that the numbers appear randomly distributed, but are in fact generated by a complex algorithm
- this function has no inputs; it returns a different number each call
- Math.random() -> 0.33008525626748814
- Math.random() -> 0.213335955823927
- Math.random() -> 0.8975001737758223
- a call to Math.random can be placed in an expression to affect the range
- 2*Math.random() -> [0…2)
- Math.random() + 1 -> [1…2)
- 9*Math.random() + 1 -> [1…10)
- Math.floor(9*Math.random() + 1) -> 1, 2, 3, …, 9
Timing
- setInterval function used to repeatedly execute a Javascript statement
- setInterval('myFunction()', 5000)
- First parameter is code to run or function to call
- Second parameter is the frequency at which the first parameter
is run in milliseconds
- 5 milliseconds is 5 seconds
- Every 5 seconds myFunction() is called
- setTimeout function is the time that a Javascript statement is executed
- setTimeout('myFunction()', 5000)
- First parameter is code to run or function to call
- Second parameter is how long before the first parameter
is run in milliseconds
- 5 milliseconds is 5 seconds
- In 5 seconds myFunction() is called