Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

7. JavaScript: Control Statements I > Answers to Self-Review Exercises

Answers to Self-Review Exercises

7.1

a. Sequence, selection and repetition.

b. if...else.

c. Counter-controlled (or definite).

d. Sentinel, signal, flag or dummy.

7.2

x = x + 1;
x += 1;
++x;
x++;

7.3

a. z = x++ + y;

b.

if ( count > 10 )
   document.writeln( "Count is greater than 10" );

c. total -= --x;

d.

q %= divisor;
q = q % divisor;

7.4

a. var sum, x;

b. x = 1;

c. sum = 0;

d. sum += x; or sum = sum + x;

e. document.writeln( "The sum is: " + sum );

7.5 The solution is as follows:

 1   <!DOCTYPE html>
 2
 3   <!-- Exercise 7.5: ex08_05.html -->
 4   <html>
 5      <head>
 6         <meta charset = "utf-8">
 7         <title>Sum the Integers from 1 to 10</title>
 8         <script>
 9            var sum; // stores the total
10            var x; //counter control variable
11
12            x = 1;
13            sum = 0;
14
15            while ( x <= 10 )
16            {
17               sum += x;
18               ++x;
19            } // end while
20            document.writeln( "The sum is: " + sum );
21         </script>
22      </head><body></body>
23   </html>


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint