Chapter 4b Fundamentals of Java Script n n

Chapter 4(b): Fundamentals of Java. Script n n n n n 4. 1 Capabilities 4. 2 Essential Terminology 4. 3 Structure of Java. Script Code 4. 4 Data and Objects 4. 5 Tokens, Operators, Expressions, and Statements 4. 6 The Java. Script math Object 4. 7 Comparison Operators and Decision-Making Structures 4. 8 Loop Structures 4. 9 Using Java. Script to Change Values in Form Fields

Chapter 4. 8: Loop (Repetition) Structures n n n Loops provide a structured way to perform repetitive calculations. Count-controlled loops perform calculations a pre-defined number of times. Conditional loops perform calculations until, or as long as, some predefined condition is true.

Count-Controlled Loops for (counter=start; {expression based on high (or low) value of counter}; expression controlling incrementing (or decrementing) of counter} ) Document 4. 7 (counter 2. htm) <html> <head> <title>Counter</title> <script> var k; document. write("Here's a simple counter: "+" "); for (k=0; k<=10; k++) document. write(k+" "); </script> </head> <body> </html>

More Count-Controlled Loops Document 4. 8 (countdown 2. htm) <html> <head> <title>Countdown</title> <script> var k; document. write("Start launch sequence!" +" "); for (k=10; k>=0; k--) document. write(k+" "); document. write("FIRE!!"); </script> </head> <body> </html>

Conditional Loops n Pre-test loops – may not be executed at all, depending on initial settings. while ( {logical expression} ) { {statements that result in changing the value of the pretest logical expression} } n Post-test loops – always executed at least once. do { {statements that result in changing the value of the post -test logical expression} } while ( {logical expression} );

The Elevator Problem Document 4. 9 (gorilla 1. htm, partial) <script language="javascript" type="text/javascript"> var total. Weight=0. , limit. Weight=500. , max. Weight=500. ; var new. Weight; do { new. Weight=Math. floor(Math. random()*(max. Weight+1)); if ((total. Weight + new. Weight) <= limit. Weight) { total. Weight += new. Weight; document. write("New weight = " + new. Weight + " total weight = " + total. Weight + " "); new. Weight=0. ; } else document. write("You weigh " + new. Weight + " lb. I'm sorry, but you can't get on. "); } while ((total. Weight + new. Weight) <= limit. Weight); </script>

Newton's Square Root Algorithm Given a number n: 1. Make a guess for the square root of n. n/2 is a reasonable guess. 2. Replace g with (g + n/g)/2. 3. Repeat step 2 until the absolute difference between g*g and n is smaller than some specified value. Document 4. 10 (newton. Sqrt 2. htm) <html> <head> <title>Newton's square root algorithm</title> <script language="javascript" type="text/javascript"> var n=parse. Float(prompt("Enter a positive number: ")); var g=n/2; do { g = (g + n/g)/2. ; } while (Math. abs(g*g-n) > 1 e-5); alert(g+" is the square root of "+n+". "); </script> </head> <body> </html>

Using Java. Script to Change Form Field Values n n The basic use for Java. Script is to access and change the values in <input /> elements used in forms "Event handlers" allow us to do this from within forms.

Pressure Calculation <form> Fill in elevation and sea-level pressure: <input type="text" name="elevation" value="0" size="8" maxlength="7" /> (m) <input type="text" name="sea_level_pressure" value="1013. 25" size="8" maxlength="7" /> (mbar) <input type="button" name="Calculate" value="Click here to get station pressure: " onclick="result. value= parse. Float(sea_level_pressure. value)parse. Float(elevation. value)/9. 2; " /> <input type="text" name="result" value="1013. 25" size="8" maxlength="7" /> (mbar) <input type="reset" value="Reset all fields. " /> </form>

Rectangular Rule Integration How do we do it?

Rectangular Rule Integration Use pseudocode to develop the algorithm: INITIALIZE integral = 0 (Initialize the value to 0. ) LOOP for i = 0 to n – 1, x = x 0 + i • dx + dx/2 y=x • x (This could be any function of x. ) integral = integral + y END LOOP ASSIGN integral = integral • dx

The code: Document 4. 13 <html><head><title></head> <body> <h 2>Rectangular Rule integration</h 2> for f(x)=x<sup>2</sup> <form> x<sub>0</sub>: <input type="text" name="x 0" value="1" /> x<sub>1</sub>: <input type="text" name="x 1" value="3" /> <input type="button" value="Click here to integrate. " onclick="var x, X 0, X 1, i, n=20, integral=0, y; //y=x*x X 1=parse. Float(x 1. value); X 0=parse. Float(x 0. value); dx=(X 1 -X 0)/n; for(i=0; i<n; i++) { x=X 0 + i*dx + dx/2; y=x*x; integral+=y; } result. value=integral*dx; "/> <input type="text" name="result" value="result" /></br /> </form></body></html>
- Slides: 12