Multiplication Table Generation

Nested for loops to create a multiplication table

Page 1 of 1

1 Replies - 11508 Views - Last Post: 05 November 2008 - 04:14 PM

#1 jklaroe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-February 05

Multiplication Table Generation

Post icon  Posted 05 November 2008 - 03:48 PM

Hey, working on some homework for a javascript class. The idea behind the assignment is to create an addition table and a multiplication table for the integers 0 - 10. I did the addition table just fine and started working on the multiplication table. Perhaps I'm just over thinking something, I'm not quite sure. The problem is, is that the first row and first column aren't as they are supposed to be.

Here's my code:
var arrayOne = new Array(11);

for (var i = 0; i <= 10; i++) {

  arrayOne[i] = i;

}

var newWindow;

function openWindow() {

  if (!(newWindow)) {

	newWindow = window.open();

  }

}

function closeWindow() {

  newwindow.document.close();

}

/* make a addition table... */
function additionTable() {

  openWindow();

  newwindow.document.write("<table border=1 cellspacing=1 cellpadding=5>");
	
  for (var i = 0; i < 11; i++) {

	newwindow.document.write("<tr>");

	for (var j = 0; j < 11; j++) {

	  newwindow.document.write("<td>" + (i + j) + "</td>");

	}

	newwindow.document.write("</tr>");

  }

  closeWindow();

}

/* make a multiplication table... */
function multiplicationTable() {

  openWindow();

  newwindow.document.write("<table border=1 cellspacing=1 cellpadding=5>");
	
  for (var i = 0; i < 11; i++) {

	newwindow.document.write("<tr>");

	for (var j = 0; j < 11; j++) {

	  newwindow.document.write("<td>" + i * j + "</td>");

	}

	newwindow.document.write("</tr>");

  }

  closeWindow();

}



My results are a table similar to this:
000	000	000	000	000	000	000	000	000	000	000
000	001	002	003	004	005	006	007	008	009	010
000	002	004	006	008	010	012	014	016	018	020
000	003	006	009	012	015	018	021	024	027	030
000	004	008	012	016	020	024	028	032	036	040
000	005	010	015	020	025	030	035	040	045	050
000	006	012	018	024	030	036	042	048	054	060
000	007	014	021	028	035	042	049	056	063	070
000	008	016	024	032	040	048	056	064	072	080
000	009	018	027	036	045	054	063	072	081	090
000	010	020	030	040	050	060	070	080	090	100



I've tried doing this:
				
for (var i = 0; i < 11; i++) {

  newwindow.document.write("<tr>");

  /* doing this gets me the first colum but not the first row correct */
  newwindow.document.write("<td>" + i + "</td>");
 
  /* to make up for adding an extra column *basicly*, i start j at 1 instead of 0 */
  for (var j = 1; j < 11; j++) {

	newwindow.document.write("<td>" + (i + j) + "</td>");

  }

  newwindow.document.write("</tr>");

}



The other functions are for creating a new window and outputting the table. It believe that part works ok, my main concern is multiplication table output.

Thanks in advance,
Josh

Is This A Good Question/Topic? 0
  • +

Replies To: Multiplication Table Generation

#2 jklaroe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-February 05

Re: Multiplication Table Generation

Posted 05 November 2008 - 04:14 PM

Solved! :ph34r:

for (var i = 0; i < 11; i++) {

  newwindow.document.write("<tr>");

  /* this will create the first column of numbers instead of all 0's */
  newwindow.document.write("<td>" + i + "</td>");
  
  for (var j = 0; j < 10; j++) {

	/* if it's the first iteration then we should spit out the first row, instead of all 0's */
	if (i == 0) {

	  newwindow.document.write("<td>" + (j + 1) + "</td>");

	}

	else {

	  newwindow.document.write("<td>" + i * (j + 1) + "</td>");

	}

  }

  newwindow.document.write("</tr>");

}



Regards,
Josh
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1