Javascript table issues

Creating a javascript table

Page 1 of 1

7 Replies - 5836 Views - Last Post: 10 November 2010 - 09:05 PM

#1 niteMaresInKoad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 02-April 09

Javascript table issues

Posted 09 November 2010 - 07:29 PM

I am trying to create a javascript table using the numbers 5-15 that squares and then cubes a number in a row. My result should look like this

5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
11 121 1331
goes all
the way to
15

Here is what I have so far
<body>
<script language="Javascript">//
alert("Table of Squares and Cubes from 5-15");
document.write('<center><table width="20" border="1">');

//loop for rows
for (j=5;j<=15;j++)
{

document.write('<tr>');
//loop for columns

for (i=5;i<=15;i++)
{

document.write('<td>' + i+j + '</td>');
}

document.write('</tr>');
}

document.write('</table></center>');
document.write('<center><table width="20" border="0">');
</script>
</body>



This code is way off. I know that this entire table is based on the loop...for (i=5;i<=15;i++), but I am having trouble putting it in javascript table output. Could anyone offer some advice?

This post has been edited by niteMaresInKoad: 09 November 2010 - 07:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Javascript table issues

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,135
  • Joined: 07-September 06

Re: Javascript table issues

Posted 09 November 2010 - 09:00 PM

Okay, well since your loops are just adding the values together you won't achieve a square or cube of the values.

Also, your loops are just making a square table...

Take a little time to think about what you are attempting to accomplish here. You want to have a 5x3 table when you are done that has the number, then the number * the number (number squared) and then the number * the number * the number (number cubed) in the columns going from left to right... maybe there is a way to take a look at which column you are writing to at the moment and find the power of the number accordingly...

Hopefully that points you in the right direction.
Was This Post Helpful? 0
  • +
  • -

#3 niteMaresInKoad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 02-April 09

Re: Javascript table issues

Posted 09 November 2010 - 09:39 PM

I kind of had that on the back burner as
myString = i
myString2 = i*i
myString3 = i*i*i


but I am having trouble incorporating this and then presenting the output in table form so...
<script language="Javascript">//
alert("Table of Squares and Cubes from 5-15");
document.write('<center><table width="20" border="1">');

//loop for rows
//for (j=5;j<=15;j++)
{

document.write('<tr>');
//loop for columns

for (i=5;i<=15;i++)
{
myString = i
myString2 = i*i
myString3 = i*i*i

//document.write('<td>' + i+j + '</td>');
}

document.write('</tr>');
}

document.write('</table></center>');
document.write('<center><table width="20" border="0">');
</script>



I know that I do not need the "j" variable in there because that is creating an unwanted table.
Struggling a lot with outputing in table format as well...
Was This Post Helpful? 0
  • +
  • -

#4 niteMaresInKoad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 02-April 09

Re: Javascript table issues

Posted 09 November 2010 - 09:45 PM

View PostniteMaresInKoad, on 09 November 2010 - 08:39 PM, said:

I kind of had that on the back burner as
myString = i
myString2 = i*i
myString3 = i*i*i


but I am having trouble incorporating this and then presenting the output in table form so...
<script language="Javascript">//
alert("Table of Squares and Cubes from 5-15");
document.write('<center><table width="20" border="1">');

//loop for rows
//for (j=5;j<=15;j++)
{

document.write('<tr>');
//loop for columns

for (i=5;i<=15;i++)
{
myString = i
myString2 = i*i
myString3 = i*i*i

//document.write('<td>' + i+j + '</td>');
}

document.write('</tr>');
}

document.write('</table></center>');
document.write('<center><table width="20" border="0">');
</script>



I know that I do not need the "j" variable in there because that is creating an unwanted table.
Struggling a lot with outputing in table format as well...


or maybe this


<script language="Javascript">//
alert("Table of Squares and Cubes from 5-15");
document.write('<center><table width="20" border="1">');



document.write('<tr>');
//loop for columns

for (i=5;i<=15;i++)
{
var myString = i
var myString2 = i*i
var myString3 = i*i*i

document.write('<td>' myString + myString2 + myString3  '</td>');
}

document.write('</tr>');


document.write('</table></center>');
document.write('<center><table width="20" border="0">');
</script>



and have this now
<script language="Javascript">//
alert("Table of Squares and Cubes from 5-15");
document.write('<center><table width="20" border="1">');



document.write('<tr>');
//loop for columns

for (i=5;i<=15;i++)
{
var x = i;
var y = i*i;
var z = i*i*i;
}

document.write('<td>' x + y + z '</td>');


document.write('</tr>');


document.write('</table></center>');
document.write('<center><table width="20" border="0">');
</script>



gotta be close...

This post has been edited by niteMaresInKoad: 09 November 2010 - 09:55 PM

Was This Post Helpful? 0
  • +
  • -

#5 niteMaresInKoad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 02-April 09

Re: Javascript table issues

Posted 09 November 2010 - 10:05 PM

View PostBetaWar, on 09 November 2010 - 08:00 PM, said:

Okay, well since your loops are just adding the values together you won't achieve a square or cube of the values.

Also, your loops are just making a square table...

Take a little time to think about what you are attempting to accomplish here. You want to have a 5x3 table when you are done that has the number, then the number * the number (number squared) and then the number * the number * the number (number cubed) in the columns going from left to right... maybe there is a way to take a look at which column you are writing to at the moment and find the power of the number accordingly...

Hopefully that points you in the right direction.


I appreciate your help sir, this has got to be close, but still not working just yet...
<script language="Javascript">//
alert("Table of Squares and Cubes from 5-15");
document.write('<center><table width="20" border="1">');



document.write('<tr>');
//loop for columns

for (i=5;i<=15;i++)
{
var x = i;
var y = i*i;
var z = i*i*i;
}

document.write('<td>' x + y + z '</td>');


document.write('</tr>');


document.write('</table></center>');
document.write('<center><table width="20" border="0">');
</script>


Was This Post Helpful? 0
  • +
  • -

#6 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,135
  • Joined: 07-September 06

Re: Javascript table issues

Posted 10 November 2010 - 08:21 AM

Okay, there are a few errors in your code so I have commented it to show you what is going on.

<script language="Javascript">// starting the javascript
alert("Table of Squares and Cubes from 5-15"); // alert a message
document.write('<center><table width="20" border="1">'); // start the output of a table to the document



document.write('<tr>'); // create a table row

//loop for columns <---------------- No, this is looping through the rows...
for (i=5;i<=15;i++) // loop through the numbers from 5 to 15 (inclusive)
{
var x = i; // set the loop variable x to i
var y = i*i; // set the loop variable y to i^2
var z = i*i*i; // set the loop variable z to i^3
} // end loop
// loop variables no longer exist

document.write('<td>' x + y + z '</td>'); // output a table column with the number x y and z added together
// This will cause errors because x, y, and z are undefined or null at this point (at least should be), additionally you are never concatenating them to the string.


document.write('</tr>'); // end a table row


document.write('</table></center>'); // end the table
// you should delete the next line... it just starts a new table that you never use...
document.write('<center><table width="20" border="0">');
</script>


Now, as for getting closer to the correct path your first code was more correct. So lets look at it a bit more:
<body>
<script language="Javascript">//
alert("Table of Squares and Cubes from 5-15");
document.write('<center><table width="20" border="1">');

//loop for rows  <--- correct
for (j=5;j<=15;j++)
{

document.write('<tr>'); // create a row <--- correct

//loop for columns <--- correct
for (i=5;i<=15;i++) // your variables here are off. You only want three columns, not 10 so how would you change this to only loop three times with the i values 1, 2, and 3?
{

document.write('<td>' + i+j + '</td>'); // here why not try looking at the i values and putting j to that power (j^i)?
// HINT - there is a built in JS function that can exponents.
} // end i loop

document.write('</tr>'); // end the table row <--- correct
} // end j loop

document.write('</table></center>'); // end table <--- correct
document.write('<center><table width="20" border="0">'); // again this is an extra line
</script>
</body>


Hopefully that helps.
Was This Post Helpful? 0
  • +
  • -

#7 niteMaresInKoad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 02-April 09

Re: Javascript table issues

Posted 10 November 2010 - 08:03 PM

View PostBetaWar, on 10 November 2010 - 07:21 AM, said:

Okay, there are a few errors in your code so I have commented it to show you what is going on.

<script language="Javascript">// starting the javascript
alert("Table of Squares and Cubes from 5-15"); // alert a message
document.write('<center><table width="20" border="1">'); // start the output of a table to the document



document.write('<tr>'); // create a table row

//loop for columns <---------------- No, this is looping through the rows...
for (i=5;i<=15;i++) // loop through the numbers from 5 to 15 (inclusive)
{
var x = i; // set the loop variable x to i
var y = i*i; // set the loop variable y to i^2
var z = i*i*i; // set the loop variable z to i^3
} // end loop
// loop variables no longer exist

document.write('<td>' x + y + z '</td>'); // output a table column with the number x y and z added together
// This will cause errors because x, y, and z are undefined or null at this point (at least should be), additionally you are never concatenating them to the string.


document.write('</tr>'); // end a table row


document.write('</table></center>'); // end the table
// you should delete the next line... it just starts a new table that you never use...
document.write('<center><table width="20" border="0">');
</script>


Now, as for getting closer to the correct path your first code was more correct. So lets look at it a bit more:
<body>
<script language="Javascript">//
alert("Table of Squares and Cubes from 5-15");
document.write('<center><table width="20" border="1">');

//loop for rows  <--- correct
for (j=5;j<=15;j++)
{

document.write('<tr>'); // create a row <--- correct

//loop for columns <--- correct
for (i=5;i<=15;i++) // your variables here are off. You only want three columns, not 10 so how would you change this to only loop three times with the i values 1, 2, and 3?
{

document.write('<td>' + i+j + '</td>'); // here why not try looking at the i values and putting j to that power (j^i)?
// HINT - there is a built in JS function that can exponents.
} // end i loop

document.write('</tr>'); // end the table row <--- correct
} // end j loop

document.write('</table></center>'); // end table <--- correct
document.write('<center><table width="20" border="0">'); // again this is an extra line
</script>
</body>


Hopefully that helps.



Thanks for all of your help, got it solved!
This is the winning line of code
document.write('<td>' + Math.pow(j,i)+" " + Math.pow(j,i+1)+" "+Math.pow( j,i+2)+ '</td>');


Was This Post Helpful? 0
  • +
  • -

#8 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,135
  • Joined: 07-September 06

Re: Javascript table issues

Posted 10 November 2010 - 09:05 PM

No problem, just glad I was of service :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1