Download presentation
Presentation is loading. Please wait.
1
The Internet 11/29/11 Functions
CIS 228 The Internet 11/29/11 Functions
2
Comments XHTML comments CSS comments JavaScript comments
<!--This is an HTML comment--> CSS comments /* This is a CSS comment */ JavaScript comments // text to the end of line ignored /* Multi-line comment */ Temporary comments are useful for debugging
3
Assignments Form: variable = expression ; Examples: i = i+1;
s = “hello world!”; b = isPrime(p) && (q < p || isPrime(q)); x = * gcd(15, 33); a[i] = “seat ” + i; var j = i*(i-1)/2; var v = document.getElementById(s).value; document.getElementById(s).value = 99*v;
4
Conditionals Conditional expressions Conditional statements
cond ? expr1 : expr2 Conditional statements if ( cond ) statement statement1 else statement2
5
Block A sequence of statements packaged into one
A block can be used anywhere a statement can { statement1 statement2 … statementn }
6
Arrays A collection of variables with the same name
differentiated by an index Declaration: var a = new array(5); // size, 5, optional var b = [“red”, “yellow”, “blue”]; Use: var c = “color: ” + b[2]; a[2] = (a[1] + a[3]) / 2; Size: a.length
7
Repetition while loops for loops while ( cond ) statement
var i=0 ; while (i<a.length) { a[i] = a[i]*a[i] ; i = i+1 ; } for loops for ( init ; cond ; inc ) statement for ( var i=0 ; i<a.length ; i = i+1 )
8
User-Defined Functions
Declaration: function name (param * ,) block function fib (n) { if (n<=1) return 1; return fib(n-2) + fib(n-1); } Call (expression or statement): name (args * ,) var x = fib(17)/3; complainToUser(x, “ is not a number!”); Return statement: return expression? ; return;
9
JavaScript Functions Built in functions Example User defined functions
Reusable chunks of code that accomplish common tasks Built in functions alert('hello world'); Displays text in a pop up window prompt('first message', 'second message'); Solicits data from user Example <body onload=“alert('hello world');”> User defined functions Bundle together small tasks into bigger ones
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.