Home | | Internet & World Wide Web HOW TO PROGRAM | | Internet Programming | | Web Programming | Formulating Algorithms: Nested Control Statements - JavaScript(JS)

# Formulating Algorithms: Nested Control Statements - JavaScript(JS)

Let us work through another complete problem. We once again formulate the algorithm using pseudocode and top-down, stepwise refinement, and write a corresponding Java-Script program.

Formulating Algorithms: Nested Control Statements

Let us work through another complete problem. We once again formulate the algorithm using pseudocode and top-down, stepwise refinement, and write a corresponding Java-Script program.

Consider the following problem statement:

A college offers a course that prepares students for the state licensing exam for real estate brokers. Last year, several of the students who completed this course took the licensing exam. Naturally, the college wants to know how well its students performed. You have been asked to write a program to summarize the results. You have been given a list of these 10 students. Next to each name is written a 1 if the student passed the exam and a 2 if the student failed.

Your program should analyze the results of the exam as follows:

1.Input each test result (i.e., a 1 or a 2). Display the message “Enter result” on the screen each time the program requests another test result.

2. Count the number of test results of each type.

3. Display a summary of the test results indicating the number of students who passed and the number of students who failed.

4. If more than eight students passed the exam, print the message “Raise tuition.”

After reading the problem statement carefully, we make the following observations about the problem:

1.The program must process test results for 10 students. A counter-controlled loop will be used.

2. Each test result is a number—either a 1 or a 2. Each time the program reads a test result, the program must determine whether the number is a 1 or a 2. We test for a 1 in our algorithm. If the number is not a 1, we assume that it is a 2. (An exercise at the end of the chapter considers the consequences of this assumption.)

3. Two counters are used to keep track of the exam results—one to count the num-ber of students who passed the exam and one to count the number of students who failed the exam.

After the program processes all the results, it must decide whether more than eight students passed the exam. Let us proceed with top-down, stepwise refinement. We begin with a pseudocode representation of the top:

Analyze exam results and decide whether tuition should be raised

Once again, it is important to emphasize that the top is a complete representation of the program, but that several refinements are necessary before the pseudocode can be evolved naturally into a JavaScript program. Our first refinement is as follows:

Initialize variables

Input the ten exam grades and count passes and failures

Print a summary of the exam results and decide whether tuition should be raised

Here, too, even though we have a complete representation of the entire program, further refinement is necessary. We now commit to specific variables. Counters are needed to record the passes and failures; a counter will be used to control the looping process, and a variable is needed to store the user input. The pseudocode statement

Initialize variables

may be refined as follows:

Initialize passes to zero

Initialize failures to zero

Initialize student to one

Note that only the counters for the number of passes, the number of failures and the num-ber of students are initialized. The pseudocode statement

Input the ten quiz grades and count passes and failures

requires a loop that successively inputs the result of each exam. Here, it is known in ad-vance that there are precisely 10 exam results, so counter-controlled looping is appropriate. Inside the loop (i.e., nested within the loop), a double-selection structure will determine whether each exam result is a pass or a failure and will increment the appropriate counter accordingly. The refinement of the preceding pseudocode statement is then

While student counter is less than or equal to ten

Input the next exam result

If the student passed

Else

Note the use of blank lines to set off the If…Else control structure to improve program readability. The pseudocode statement

Print a summary of the exam results and decide whether tuition should be raised

may be refined as follows:

Print the number of passes

Print the number of failures

If more than eight students passed

Print “Raise tuition”

The complete second refinement appears in Fig. 7.10. Note that blank lines are also used to set off the While statement for program readability.

This pseudocode is now refined sufficiently for conversion to JavaScript. The Java-Script program and two sample executions are shown in Fig. 7.11.

Initialize passes to zero

Initialize failures to zero

Initialize student to one

While student counter is less than or equal to ten

Input the next exam result

If the student passed

Else

Print the number of passes

Print the number of failures

If more than eight students passed

Print “Raise tuition”

Fig. 7.10 | Examination-results problem pseudocode.

<?xml version = "1.0" encoding = "utf-8"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<!-- Fig. 7.11: analysis.html -->

<!-- Examination-results calculation. -->

<html xmlns = "http://www.w3.org/1999/xhtml">

<title>Analysis of Examination Results</title>

<script type = "text/javascript">

<!--

// initializing variables in declarations

var passes = 0; // number of passes

var failures = 0; // number of failures

var student = 1; // student counter

var result; // one exam result

// process 10 students; counter-controlled loop

while ( student <= 10 )

{

result = window.prompt( "Enter result (1=pass,2=fail)", "0" );

if ( result == "1" )

passes = passes + 1;

else

failures = failures + 1;

student = student + 1;

} // end while

// termination phase

document.writeln( "<h1>Examination Results</h1>" );

document.writeln(

"Passed: " +       passes + "<br />Failed: " + failures );

if ( passes >        8       )

document.writeln( "<br />Raise Tuition" );

// -->

</script>

<body>

<p>Click Refresh (or Reload) to run the script again</p>

</body>

</html>  Fig. 7.11 | Examination-results calculation.

Lines 13–16 declare the variables used to process the examination results. Note that JavaScript allows variable initialization to be incorporated into declarations (passes is assigned 0, failures is assigned 0 and student is assigned 1). Some programs may require reinitialization at the beginning of each repetition; such reinitialization would normally occur in assignment statements.

The processing of the exam results occurs in the while statement in lines 19–29. Note that the ifelse statement in lines 23–26 in the loop tests only whether the exam result was 1; it assumes that all other exam results are 2. Normally, you should validate the values input by the user (i.e., determine whether the values are correct). In the exercises, we ask you to modify this example to validate the input values to ensure that they are either 1 or 2.

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Related Topics