• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

What is wrong with this Javascript?

Mean MrMustard

Diamond Member
It is supposed to calculate the sqare and cube of the numbers 0 to 10 then display them in a table. I can't think of what I'm doing wrong though. I'm sure it's an easy mistake though.

<SCRIPT LANGUAGE = "Javascript">
var 0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
03,
13,
23,
33,
43,
53,
63,
73,
83,
93,
103,
n0,
n1,
n2,
n3,
n4,
n5,
n6,
n7,
n8,
n9,
n10,
n23,
n33,
n43,
n53,
n63,
n73,
n83,
n93,
n103;

n0 = parseInt( 0 );
n1 = parseInt( 1 );
n2 = parseInt( 2 );
n3 = parseInt( 3 );
n4 = parseInt( 4 );
n5 = parseInt( 5 );
n6 = parseInt( 6 );
n7 = parseInt( 7 );
n8 = parseInt( 8 );
n9 = parseInt( 9 );
n10 = parseInt( 10 );
n03 = parseInt( 03 );
n13 = parseInt( 13 );
n23 = parseInt( 23 );
n33 = parseInt( 33 );
n43 = parseInt( 43 );
n53 = parseInt( 53 );
n63 = parseInt( 63 );
n73 = parseInt( 73 );
n83 = parseInt( 83 );
n93 = parseInt( 93 );
n103 = parseInt( 103 );

n0 = 0 * 0;
n1 = 1 * 1;
n2 = 2 * 2;
n3 = 3 * 3;
n4 = 4 * 4;
n5 = 5 * 5;
n6 = 6 * 6;
n7 = 7 * 7;
n8 = 8 * 8;
n9 = 9 * 9;
n10 = 10 * 10;

n03 = 0 * 0 * 0;
n13 = 1 * 1 * 1;
n23 = 2 * 2 * 2;
n33 = 3 * 3 * 3;
n43 = 4 * 4 * 4;
n53 = 5 * 5 * 5;
n63 = 6 * 6 * 6;
n73 = 7 * 7 * 7;
n83 = 8 * 8 * 8;
n93 = 9 * 9 * 9;
n103 = 10 * 10 * 10;

document.writeln( "<TABLE BORDER = '0' WIDTH = '25%'>");
document.writeln( "<COLGROUP>");
document.writeln( "<COL ALIGN = 'left'>");
document.writeln( "<COL SPAN = '3'>");
document.writeln( "</COLGROUP>");
document.writeln( "<THEAD>");
document.writeln( "<TR VALIGN = 'bottom'>");
document.writeln( "<TH>number</TH>");
document.writeln( "<TH>square</TH>");
document.writeln( "<TH>cube</TH>");
document.writeln( "<TR>");
document.writeln( "</THEAD>");
document.writeln( "<TBODY>");
document.writeln( "<TR>");
document.writeln( "<TH>0</TH>");
document.writeln( "<TD>" + n0 + "</TD>");
document.writeln( "<TD>" + n03 + "</TD>");
document.writeln( "</TR>");
document.writeln( "<TH>1</TH>");
document.writeln( "<TD>" + n1 + "</TD>");
document.writeln( "<TD>" + n13 + "</TD>");
document.writeln( "</TR>");
document.writeln( "<TH>2</TH>");
document.writeln( "<TD>" + n2 + "</TD>");
document.writeln( "<TD>" + n23 + "</TD>");
document.writeln( "</TR>");
document.writeln( "<TH>3</TH>");
document.writeln( "<TD>" + n3 + "</TD>");
document.writeln( "<TD>" + n33 + "</TD>");
document.writeln( "</TR>");
document.writeln( "<TH>4</TH>");
document.writeln( "<TD>" + n4 + "</TD>");
document.writeln( "<TD>" + n43 + "</TD>");
document.writeln( "</TR>");
document.writeln( "<TH>5</TH>");
document.writeln( "<TD>" + n5 + "</TD>");
document.writeln( "<TD>" + n53 + "</TD>");
document.writeln( "</TR>");
document.writeln( "<TH>6</TH>");
document.writeln( "<TD>" + n6 + "</TD>");
document.writeln( "<TD>" + n63 + "</TD>");
document.writeln( "</TR>");
document.writeln( "<TH>7</TH>");
document.writeln( "<TD>" + n7 + "</TD>");
document.writeln( "<TD>" + n73 + "</TD>");
document.writeln( "</TR>");
document.writeln( "<TH>8</TH>");
document.writeln( "<TD>" + n8 + "</TD>");
document.writeln( "<TD>" + n83 + "</TD>");
document.writeln( "</TR>");
document.writeln( "<TH>9</TH>");
document.writeln( "<TD>" + n9 + "</TD>");
document.writeln( "<TD>" + n93 + "</TD>");
document.writeln( "</TR>");
document.writeln( "<TH>10</TH>");
document.writeln( "<TD>" + n10 + "</TD>");
document.writeln( "<TD>" + n103 + "</TD>");
document.writeln( "</TR>");
document.writeln( "</TBODY>");
document.writeln( "</TABLE>");

</SCRIPT>
 
Heh. You're right, it's an easy mistake: You don't declare integers. 🙂

This is what Mozilla's JavaScript Console has to say:
Error: missing variable name
Source File: http://localhost/atjs.html
Line: 4, Column: 4
Source Code:
var 0,


You only need to declare variables, so in that first JS line var 0, 1, ... you can remove all the integers and just leave the variable names: var n0, n1, ...
Also, the parseInt( x ) lines don't really do anything. They set the variables to certain values, but since those values get overwritten just a few lines later (n0 = 0 * 0; etc.), you can just remove them.

For completeness' sake, here's the working program:

<SCRIPT LANGUAGE = "Javascript">
var n0,
n1,
n2,
n3,
n4,
n5,
n6,
n7,
n8,
n9,
n10,
n23,
n33,
n43,
n53,
n63,
n73,
n83,
n93,
n103;

n0 = 0 * 0;
n1 = 1 * 1;
n2 = 2 * 2;
n3 = 3 * 3;
n4 = 4 * 4;
n5 = 5 * 5;
n6 = 6 * 6;
n7 = 7 * 7;
n8 = 8 * 8;
n9 = 9 * 9;
n10 = 10 * 10;

n03 = 0 * 0 * 0;
n13 = 1 * 1 * 1;
n23 = 2 * 2 * 2;
n33 = 3 * 3 * 3;
n43 = 4 * 4 * 4;
n53 = 5 * 5 * 5;
n63 = 6 * 6 * 6;
n73 = 7 * 7 * 7;
n83 = 8 * 8 * 8;
n93 = 9 * 9 * 9;
n103 = 10 * 10 * 10;

document.writeln( "<TABLE BORDER = '0' WIDTH = '25%'>");
document.writeln( "<COLGROUP>");
document.writeln( "<COL ALIGN = 'left'>");
document.writeln( "<COL SPAN = '3'>");
document.writeln( "</COLGROUP>");
document.writeln( "<THEAD>");
document.writeln( "<TR VALIGN = 'bottom'>");
document.writeln( "<TH>number</TH>");
document.writeln( "<TH>square</TH>");
document.writeln( "<TH>cube</TH>");
document.writeln( "<TR>");
document.writeln( "</THEAD>");
document.writeln( "<TBODY>");
document.writeln( "<TR>");
document.writeln( "<TH>0</TH>");
document.writeln( "<TD>" + n0 + "</TD>");
document.writeln( "<TD>" + n03 + "</TD>");
document.writeln( "</TR>");
document.writeln( "<TH>1</TH>");
document.writeln( "<TD>" + n1 + "</TD>");
document.writeln( "<TD>" + n13 + "</TD>");
document.writeln( "</TR>");
document.writeln( "<TH>2</TH>");
document.writeln( "<TD>" + n2 + "</TD>");
document.writeln( "<TD>" + n23 + "</TD>");
document.writeln( "</TR>");
document.writeln( "<TH>3</TH>");
document.writeln( "<TD>" + n3 + "</TD>");
document.writeln( "<TD>" + n33 + "</TD>");
document.writeln( "</TR>");
document.writeln( "<TH>4</TH>");
document.writeln( "<TD>" + n4 + "</TD>");
document.writeln( "<TD>" + n43 + "</TD>");
document.writeln( "</TR>");
document.writeln( "<TH>5</TH>");
document.writeln( "<TD>" + n5 + "</TD>");
document.writeln( "<TD>" + n53 + "</TD>");
document.writeln( "</TR>");
document.writeln( "<TH>6</TH>");
document.writeln( "<TD>" + n6 + "</TD>");
document.writeln( "<TD>" + n63 + "</TD>");
document.writeln( "</TR>");
document.writeln( "<TH>7</TH>");
document.writeln( "<TD>" + n7 + "</TD>");
document.writeln( "<TD>" + n73 + "</TD>");
document.writeln( "</TR>");
document.writeln( "<TH>8</TH>");
document.writeln( "<TD>" + n8 + "</TD>");
document.writeln( "<TD>" + n83 + "</TD>");
document.writeln( "</TR>");
document.writeln( "<TH>9</TH>");
document.writeln( "<TD>" + n9 + "</TD>");
document.writeln( "<TD>" + n93 + "</TD>");
document.writeln( "</TR>");
document.writeln( "<TH>10</TH>");
document.writeln( "<TD>" + n10 + "</TD>");
document.writeln( "<TD>" + n103 + "</TD>");
document.writeln( "</TR>");
document.writeln( "</TBODY>");
document.writeln( "</TABLE>");

</SCRIPT>
 
Might I also suggest that you use arrays and loops instead of declaring each integer variable:

<script language ="Javascript">
var arrSquare, arrCubic;
arrSquare= new Array(11);
arrCubic = new Array(11);

for(var intCount = 0; intCount <11; intCount++)
{
arrSquare[intCount] = intCount * intCount;
}

for(intCount = 0; intCount <11; intCount++)
{
arrCubic[intCount] = intCount * intCount * intCount;
}

document.writeln( "<TABLE BORDER = '0' WIDTH = '25%'>");
document.writeln( "<COLGROUP>");
document.writeln( "<COL ALIGN = 'left'>");
document.writeln( "<COL SPAN = '3'>");
document.writeln( "</COLGROUP>");
document.writeln( "<THEAD>");
document.writeln( "<TR VALIGN = 'bottom'>");
document.writeln( "<TH>number</TH>");
document.writeln( "<TH>square</TH>");
document.writeln( "<TH>cube</TH>");
document.writeln( "<TR>");
document.writeln( "</THEAD>");
document.writeln( "<TBODY>");
document.writeln( "<TR>");
for(intCount = 0; intCount <11; intCOunt++)
{
document.writeln("<TH>" + intCount + "</TH>");
document.writeln("<TD>" + arrSquare[intCount] + "</TD>");
document.writeln("<TD>" + arrCubic[intCount] + "</TD>");
document.writeln( "</TR>");
}
document.writeln( "</TR>");
document.writeln( "</TBODY>");
document.writeln( "</TABLE>");
</script>

This will greatly reduce the bloat of the code.

Also of note is the pow() function
You could also use pow() instead of multiplying a number by itself a certain amount of times. Usage is:
Math.pow(number, power) (ie- Math.pow(1,3) would be one to the third)
you could incorporate this in the loops above as Math.pow(intCount, 2) and Math.pow(intCount, 3) respectivally.

You could also reduce the number of document.writeln() by extending each line by using "+"
document.writeln( "<TABLE BORDER = '0' WIDTH = '25%'>" +
"<COLGROUP>"+
"<COL ALIGN = 'left'>" ....


Hope this helps🙂
 
Thanks for the help guys. Nexus09, yeah I know the code could be condensed a lot but we're only supposed to use things we learned in this particular chapter (first chapter if you can't already tell).

Craig
 
ELP, yeah I know the drill 😉
Don't you hate it when you know much more than you're "supposed" to and you can't use it.

Good luck learning Javascript, it's great fun 🙂
 
Back
Top