Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4(b): Fundamentals of JavaScript 4.1 Capabilities 4.2 Essential Terminology 4.3 Structure of JavaScript Code 4.4 Data and Objects 4.5 Tokens, Operators,

Similar presentations


Presentation on theme: "Chapter 4(b): Fundamentals of JavaScript 4.1 Capabilities 4.2 Essential Terminology 4.3 Structure of JavaScript Code 4.4 Data and Objects 4.5 Tokens, Operators,"— Presentation transcript:

1 Chapter 4(b): Fundamentals of JavaScript 4.1 Capabilities 4.2 Essential Terminology 4.3 Structure of JavaScript Code 4.4 Data and Objects 4.5 Tokens, Operators, Expressions, and Statements 4.6 The JavaScript math Object 4.7 Comparison Operators and Decision-Making Structures 4.8 Loop Structures 4.9 Using JavaScript to Change Values in Form Fields

2 Chapter 4.8: Loop (Repetition) Structures 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.

3 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 (counter2.htm) Counter var k; document.write("Here's a simple counter: "+" "); for (k=0; k ");

4 More Count-Controlled Loops Document 4.8 (countdown2.htm) Countdown var k; document.write("Start launch sequence!" +" "); for (k=10; k>=0; k--) document.write(k+" "); document.write("FIRE!!");

5 Conditional Loops 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 pre- test logical expression} } 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} );

6 The Elevator Problem Document 4.9 ( gorilla1.htm, partial ) var totalWeight=0.,limitWeight=500.,maxWeight=500.; var newWeight; do { newWeight=Math.floor(Math.random()*(maxWeight+1)); if ((totalWeight + newWeight) <= limitWeight) { totalWeight += newWeight; document.write("New weight = " + newWeight + " total weight = " + totalWeight + " "); newWeight=0.; } else document.write("You weigh " + newWeight + " lb. I'm sorry, but you can't get on."); } while ((totalWeight + newWeight)

7 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 ( newtonSqrt2.htm ) Newton's square root algorithm var n=parseFloat(prompt("Enter a positive number:")); var g=n/2; do { g = (g + n/g)/2.; } while (Math.abs(g*g-n) > 1e-5); alert(g+" is the square root of "+n+".");

8 Using JavaScript to Change Form Field Values The basic use for JavaScript is to access and change the values in elements used in forms "Event handlers" allow us to do this from within forms.

9 Pressure Calculation Fill in elevation and sea-level pressure: (m) (mbar) (mbar)

10 Rectangular Rule Integration How do we do it?

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

12 The code: Document 4.13 Rectangular Rule integration for f(x)=x 2 x 0 : x 1 :


Download ppt "Chapter 4(b): Fundamentals of JavaScript 4.1 Capabilities 4.2 Essential Terminology 4.3 Structure of JavaScript Code 4.4 Data and Objects 4.5 Tokens, Operators,"

Similar presentations


Ads by Google