Lecture Summary 7
Prepared by: Anthony Larrain
Javascript Functions.
- A function typically takes one or more inputs and returns a single value. Some functions do not return values.
- Javascript provides many built in functions that perform various tasks. Such as
prompt("Enter name"); parseFloat("35.3"); document.write("Welcome");
prompt returns the string we enter into the textbox. parseFloat returns the string converted to a numeric value.
name = prompt("Enter name"); temperature = parseFloat("35.3");
Math
followed by the dot '.' operator and the name of the function.Example
To find the square root of a number we would write
Math.sqrt(16);
This function inputs 16 and returns 4, which we would normally assign to a variable.
result = Math.sqrt(16);
The Power Function
Power Function Tester
Lotto Numbers
Pick Four
Alert Window
- A Javascript function used to alert a visitor of your page to a message.
- The alert window is a dialog box, like prompt, except no textbox for input.
- Alerts are annoying to users and should be used sparingly.
In alert2 demo, the body tag has an attribute called onUnLoad
. This attribute can be used to specify
code that is executed when the user leaves the page.