07 Ch19
07 Ch19
Styles
Variables
Creating the page…radio buttons
CS170
Computer Applications for Business
Functions
A JavaScript function is a block of code
designed to perform one particular task.
function convertFtoC(degreesF){
var degreesC = (degreesF - 32)* 5/9;
return degreesC;
}
Parameters
• See CH19-FtoC-functions.html
• Parameter naming conventions follow the same
rules as variables
Argument
Arguments and Parameters
Pass no arguments:
coinFlip( );
function coinFlip( ){…}
Pass literal values:
add2Nums(3, 5);
function add2Nums(num1, num2){…}
ans? 13
function calcBeta (a, b, c) {
var x, y; x? 1
x = 2*a; y= b+3*c; z = 7; y? 2
return x+y; 7
z?
}
Sample – CH19-Sample1.html
<style>
defines format of HTML elements
to make font in buttons bigger, use button
instead of input type = "button"
</style>
Create the function
<h1> Simple Example </h1>
<script> //PROGRAMMING PART HERE
function simple(){
var ans = 1 + 5 * 2;
textbox1.value = ans;
}
</script>
Call the function
Value calculated in
function in stored in
variable answer and
then put into
textbox.
Weekly Pay
function– Weekly Pay
Weekly Pay
Weekly Pay
Weekly Pay – the button
The code?
Coin Toss
flip = Math.random();
Math.round(flip));
CH 19 Topics
Chapter 19 – JavaScript Functions
• JavaScript rules for: functions, declarations,
return values, function calls, scope of
reference, and local/global variable reference,
Forms and Functions
• You should be able to write and use simple
functions
• Built-in functions: Date(), Math.random(),
math.floor(), toString()
• Reasons for using functions
• The Coin Flipping and Memory Bank example