Multiplication Table Using Nested Loops


<SCRIPT>

// Use single-spaced text for Multiplication table
document.write("<CENTER><BLOCKQUOTE><STRONG><PRE><FONT COLOR='FF0000'">)

var i, j, total; // global variables

for (i = 1; i <= 10; i++) {
      for (j = 1; j < 10; j++) {
         total = i * j;
         total = " " + total //add space before each number

         // Add another space before single digits
         if (total < 10) total = " " + total;

         // Display result
         document.write(total);
       } // end inner j loop

    document.write("<BR>"); // end of line break
   } // end of i outer loop

document.write("</FONT></PRE></STRONG></BLOCKQUOTE></CENTER>");

</SCRIPT>

for (i = 1; i <= 10; i++) { // when i = 1
      for (j = 1; j < 10; j++) {

      when j = 1 => i * j = 1 * 1 = 1
      when j = 2 => i * j = 1 * 2 = 2
      when j = 3 => i * j = 1 * 3 = 3
      etc...
      when j = 10 => i * j = 1 * 10 = 10
break out of the inner loop & go back to the outer loop

for (i = 1; i <= 10; i++) { // when i = 2
      for (j = 1; j < 10; j++) {

      when j = 1 => i * j = 2 * 1 = 2
      when j = 2 => i * j = 2 * 2 = 4
      when j = 3 => i * j = 2 * 3 = 6
      etc...
      when j = 10 => i * j = 2 * 10 = 20
break out of the inner loop & go back to the outer loop

etc...

for (i = 1; i <= 10; i++) { // when i = 10
      for (j = 1; j < 10; j++) {

      when j = 1 => i * j = 10 * 1 = 10
      when j = 2 => i * j = 10 * 2 = 20
      when j = 3 => i * j = 10 * 3 = 30
      etc...
      when j = 10 => i * j = 10 * 10 = 100

Finished at this point