{{ message }}

Instantly share code, notes, and snippets.

# leonardofaria/quiz3.js

Created Oct 20, 2015
WMDD 4820 - Quiz 3
 // 1. How many times are the following loop bodies repeated? // What is the printout of each loop? (3 points) // a. var i = 1; while (i < 10) if (i % 2 == 0) document.write( i + "
"); // Answer: infinity loop. i will be never incremented. // b. var i = 1; while (i < 10) if (i % 2 == 0) document.write( i++ + "
"); // Answer: infinity loop. i will be never incremented. // c. var i = 1; while (i < 10) if (i++ % 2 == 0) document.write( i ); // Answer: // 3 // 5 // 7 // 9 // ----------------------------------------------------------------------------- // 2. Will the following programs terminate? If so, give the output. (4 points) // a. var balance = 49; while (true) { if (balance < 9) break; balance = balance - 9; } document.write ( "Balance is " + balance ); // Answer: Balance is 4 // b. var balance = 49; while (true) { if (balance < 9) continue; balance = balance - 9; } document.write ( "Balance is " + balance ); // ----------------------------------------------------------------------------- // 3. Show the output of the following code segments. (6 points) // a. var i = 0; while (i < 4) { for (var j = i; j > 1; j--) document.write ( j ); document.write( "****" + "
" ); i++; } // Answer: // **** // **** // 2**** // 32**** // b. var i = 4; while (i >= 1) { var num = 1; for (var j = 1; j <= i; j++) { document.write( num + "xxx" ); num *= 2; } document.write( "
" ); i--; } // Answer: // 1xxx2xxx4xxx8xxx // 1xxx2xxx4xxx // 1xxx2xxx // 1xxx // c. var row = 4; while ( row >= 1 ) { column = 1; while ( column <= 5 ) { document.write ( row % 2 ? "<" : ">" ); ++column; } --row; document.write("
"); } // Answer // >>>>> // <<<<< // >>>>> // <<<<< // ----------------------------------------------------------------------------- // 4. How many times is the print statement executed? (2 points) for (var i = 0; i < 10; i++) for (var j = 0; j < i; j++) document.write( i * j + "