23.07.2013 Views

JavaScript/JScript: Control Structures I - Pearson Learning Solutions

JavaScript/JScript: Control Structures I - Pearson Learning Solutions

JavaScript/JScript: Control Structures I - Pearson Learning Solutions

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

2008934301<br />

Chapter 14 <strong>JavaScript</strong>/<strong>JScript</strong>: <strong>Control</strong> <strong>Structures</strong> I 419<br />

Good Programming Practice 14.7<br />

When converting a pseudocode program to <strong>JavaScript</strong>, keep the pseudocode in the Java-<br />

Script program as comments. 14.7<br />

Software Engineering Observation 14.10<br />

Experience has shown that the most difficult part of solving a problem on a computer is developing<br />

the algorithm for the solution. After a correct algorithm has been specified, the process<br />

of producing a working <strong>JavaScript</strong> program from the algorithm is normally<br />

straightforward. 14.10<br />

Software Engineering Observation 14.11<br />

Many experienced programmers write programs without ever using program development<br />

tools like pseudocode. These programmers feel that their ultimate goal is to solve the problem<br />

on a computer, and that writing pseudocode merely delays the production of final outputs.<br />

Although this may work for simple and familiar problems, it can lead to serious errors<br />

on large, complex projects. 14.11<br />

The <strong>JavaScript</strong> program and a sample execution are shown in Fig. 14.9. Although each<br />

grade is an integer, the averaging calculation is likely to produce a number with a decimal<br />

point (a real number).<br />

1 <br />

2 <br />

3 <br />

4<br />

5 <br />

6 Class Average Program:<br />

7 Sentinel-controlled Repetition<br />

8<br />

9 <br />

10 var gradeCounter, // number of grades entered<br />

11 gradeValue, // grade value<br />

12 total, // sum of grades<br />

13 average, // average of all grades<br />

14 grade; // grade typed by user<br />

15<br />

16 // Initialization phase<br />

17 total = 0; // clear total<br />

18 gradeCounter = 0; // prepare to loop<br />

19<br />

20 // Processing phase<br />

21 // prompt for input and read grade from user<br />

22 grade = window.prompt(<br />

23 "Enter Integer Grade, -1 to Quit:", "0" );<br />

24<br />

25 // convert grade from a String to an integer<br />

26 gradeValue = parseInt( grade );<br />

27<br />

Fig. 14.9 Class-average program with sentinel-controlled repetition (part 1 of 3).<br />

e-Business and e-Commerce: How to Program, by Harvey M. Deitel, Paul J. Deitel, and Tem R. Nieto. Published by Prentice Hall.<br />

Copyright © 2001 by <strong>Pearson</strong> Education, Inc.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!