0

Im studying javascript with a book and there is this exercise: Calculate the squares and the cubes of the numbers from 0 to 10 and display values in an html table. They are supposed to display like this : Table

My code is this :

<head>

<script type="text/javascript">
<!--
var square;
var cube;
document.write("<table border='0'>");
for (var number = 0; number <=10; number +=1){
    square = number*number;
    cube = number*number*number;    
    document.write("<tr>");
    document.write("<td>");
    document.writeln(number);
    document.write("</td>");
    document.write("<td>");
    document.writeln(square);
    document.write("</td>");
    document.write("<td>");
    document.writeln(cube);
    document.write("</td>");
    document.write("</tr>");
}   
document.write("</table>");         
// -->
</script>
</head>

Problem is,when I run this I dont get the title which says Number Square Cube

I only get the numerical results...where is my mistake?

Jane D.
  • 25
  • 6
  • 1
    For starters, writing this *into the * is entirely incorrect. Start by putting this into the site's . Also, where would it say "number, square, cube"? You're not outputting any titles! – deceze Nov 30 '15 at 10:37
  • @deceze - wow .. not one answer has picked up on the fact that it's all in the `` and will never display – Jaromanda X Nov 30 '15 at 10:40
  • @JaromandaX Well, it probably *will* display, because a in the head is invalid, so the browser will implicitly close the ... But it's still nonsense.
    – deceze Nov 30 '15 at 10:41

5 Answers5

1

you can generate table like

document.write("<table><tr><th>number</th><th>square</th><th>cube</th></tr>");
for(i=1;i<=10;i++){
var square=i*i;
var cube=i*i*i;
    document.write("<tr><td>"+i+"</td><td>"+square+"</td><td>"+cube+"</td></tr>");

}
Muhammad Waqas
  • 1,290
  • 12
  • 19
0

I'm not entirely sure what you're asking, but if you want text also you'll need:

document.writeln('Number: ' + number);

Also expect a telling off here for using document.write

Community
  • 1
  • 1
JBux
  • 1,378
  • 8
  • 17
0

Add the <thead> section like this:

<head>

<script type="text/javascript">
<!--
var square;
var cube;
document.write("<table border='0'>");
document.write("<thead><th>number</th><th>square</th><th>cube</th></thead>");
for (var number = 0; number <=10; number +=1){
    square = number*number;
    cube = number*number*number;    
    document.write("<tr>");
    document.write("<td>");
    document.writeln(number);
    document.write("</td>");
    document.write("<td>");
    document.writeln(square);
    document.write("</td>");
    document.write("<td>");
    document.writeln(cube);
    document.write("</td>");
    document.write("</tr>");
}   
document.write("</table>");         
// -->
</script>
</head>
Sanjay Kumar N S
  • 4,165
  • 3
  • 18
  • 34
0

You're missing the header row...

<head>

  <script type="text/javascript">
    <!--
    var square;
    var cube;
    document.write("<table border='0'>");
    document.write("<tr>");
    document.write("<td>");
    document.writeln("number");
    document.write("</td>");
    document.write("<td>");
    document.writeln("square");
    document.write("</td>");
    document.write("<td>");
    document.writeln("cube");
    document.write("</td>");
    document.write("</tr>");
    for (var number = 0; number <= 10; number += 1) {
      square = number * number;
      cube = number * number * number;
      document.write("<tr>");
      document.write("<td>");
      document.writeln(number);
      document.write("</td>");
      document.write("<td>");
      document.writeln(square);
      document.write("</td>");
      document.write("<td>");
      document.writeln(cube);
      document.write("</td>");
      document.write("</tr>");
    }
    document.write("</table>");
     // -->
  </script>
</head>
edeboursetty
  • 5,344
  • 1
  • 31
  • 59
0

<script type="text/javascript">
    <!--
    var square;
    var cube;
    document.write("<table border='0'>");
    //document.write("<td>")

    document.write("<tr>");
    document.write("<td>");
    document.writeln("number");
    document.write("</td>");
    document.write("<td>");
    document.writeln("Square");
    document.write("</td>");
    document.write("<td>");
    document.writeln("Cube");
    document.write("</td>");
    document.write("</tr>");

    for (var number = 0; number <=10; number +=1){
        square = number*number;
        cube = number*number*number;
        document.write("<tr>");
        document.write("<td>");
        document.writeln(number);
        document.write("</td>");
        document.write("<td>");
        document.writeln(square);
        document.write("</td>");
        document.write("<td>");
        document.writeln(cube);
        document.write("</td>");
        document.write("</tr>");
    }
    document.write("</table>");
    // -->
</script>

Try this code instead of your code. In your code you missed the headers of the column.

Nutan
  • 740
  • 1
  • 8
  • 17
  • 1
    Its always been a good practice to describe your code. Based on your answer how a questioner understand what is the problem with their code and what changes you have made to the respective code. Make it more descriptive in order to understand it properly. – J-D Nov 30 '15 at 10:56
  • Thank you so much, I will remember this in my further posts – Nutan Nov 30 '15 at 14:32