|
Sample programs to predict output. Try these. See if you can predict the output. Change them and try again. Email others with your example code.
<?xml version = "1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- Fig. 8.7: average.html --> <!-- Class Average Program -->
<html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>Class Average Program</title>
<script type = "text/javascript"> <!-- var a=1, b=7, c=9;
while ( a < c ) { document.writeln("Junk" + a + "<br>"); a= a + 2; }
// --> </script>
</head> <body> <p>Click Refresh (or Reload) to run the script again</p> </body> </html>
Example 2
<?xml version = "1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- Fig. 8.7: average.html --> <!-- Class Average Program -->
<html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>Class Average Program</title>
<script type = "text/javascript"> <!-- var a=1, b=7, c=9;
while ( a < c ) { document.writeln("Junk" + a + "<br>"); if (a ==3) { a++; document.writeln("Stuff" + a + "<br>"); } if (a == 5) document.writeln("Skip" + a + "<br>"); if (a < 8) { b = b - 1; if ( a < 5) document.writeln("Small" + b + "<br>"); } document.writeln("Again" + a + "<br>"); a = a + 2 }
// --> </script>
</head> <body> <p>Click Refresh (or Reload) to run the script again</p> </body> </html>
Example 3.
<?xml version = "1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- Fig. 8.7: average.html --> <!-- Class Average Program -->
<html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>Class Average Program</title>
<script type = "text/javascript"> <!-- var a=1, b=7, c=9;
while ( a < c ) { document.writeln("Junk" + a + "<br>"); if (a ==3) { while ( b > 2) { document.writeln("Loop" + b + "<br>"); if ( b == 3) { document.writeln("go" + b + "<br>"); b=b-2; } b--; } } if (a == 5) document.writeln("Skip" + a + "<br>"); if (a > 7) { b = b - 1; if ( a < 5) document.writeln("Small" + b + "<br>"); } document.writeln("Again" + a + "<br>"); a = a + 2 }
// --> </script>
</head> <body> <p>Click Refresh (or Reload) to run the script again</p> </body> </html>
Challenge
There is a WebX discussion group called Output Challenge. Your task is to write a program that is hard to trace and post it to the website. Make sure it works first. Here are the rules:
1. Use 3 integer variables. No fractions 2. Keep the numbers generated less than 20. 3. Use only +, -, *, /, and % operators 4. The code should be less than 20 lines long 5. Use only ==, >, or < in your if and while 6. Use only if and while, along with assignment statements 7. Have at most 2 while statements 8. Keep the output to 20 lines or less
See how hard you can make the flow of control. We will have a vote on the best program online.
More Examples:
<?xml version = "1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
<!-- Fig. 8.7: average.html --> <!-- Class Average Program -->
<html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>Class Average Program</title>
<script type = "text/javascript"> <!-- var a=1, b=8, c=9;
if ( a < c ) { document.writeln("No" + a + "<br>"); while (a < b) { document.writeln("Junk" + a + "<br>"); if (a < 3) { a = a + 2; document.writeln("Stuff" + a + "<br>"); } if (a == 5) { document.writeln("Skip" + a + "<br>"); if (a < 8) { b = b - 1; if ( a < 5) document.writeln("Small" + b + "<br>"); } } document.writeln("Again" + a + "<br>"); a = a + 2 } } // --> </script>
</head> <body> <p>Click Refresh (or Reload) to run the script again</p> </body> </html>
Another Example
<?xml version = "1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
<!-- Fig. 8.7: average.html --> <!-- Class Average Program -->
<html xmlns = "http://www.w3.org/1999/xhtml"> <head> <title>Class Average Program</title>
<script type = "text/javascript"> <!-- { var a,b,c; a=0; b=5; c=9; while ( a < c ) { document.writeln("Junk" + a+ "<br>"); a= a + 2; if ( a > 4) { document.writeln("loop"+ "<br>"); a++; } if ( a<4 ) { document.writeln("little" + a+ "<br>"); c--; } } if ( b == 7 ) document.writeln("do again"+ "<br>"); while ( b < 10 ) { b++; if (b == 9) document.writeln("NOPE"+ "<br>"); document.writeln("skip"+ "<br>"); } document.writeln(b+ "<br>"); if ( b > 10 ) document.writeln("done"+ "<br>"); document.writeln("again" + b+ "<br>");
}
// --> </script>
</head> <body> <p>Click Refresh (or Reload) to run the script again</p> </body> </html>
|