0

I wish to put my result like this:

Enter image description here

But what I get is like this:

Enter image description here

And here is my code. What is the correct way?

<script>
    var myObj, x= "", y="";

    myObj={
        "Movie": ["Deadpool", "Xmen", "Hunger Games", "RoboCop", "LOTR", "Starwar"],
        "Rating": [ 8, 6, 7, 2, 8, 7 ]
    };

    document.write("<table border='1'>")
    document.write("<tr>")

    for (i in myObj.Movie)
    {
        document.write("<td width='80' >" + myObj.Movie[i] + "</td>");
    }

    for (i in myObj.Rating)
    {
        document.write("<td width='80' >" + myObj.Rating[i] + "</td>");
    }
    document.write("</tr>")
    document.write("</table>")
</script>

4 Answers 4

2
document.write("<table border='1'>");
for (i in myObj.Movie) 
{
    document.write("<tr>");
    document.write("<td width='80' >"+myObj.Movie[i]+"</td>");
    document.write("<td width='80' >"+myObj.Rating[i]+"</td>");
    document.write("</tr>")
}
document.write("</table>");
Sign up to request clarification or add additional context in comments.

1 Comment

I am glad to help you... Thank you!
1

Try this.

<script>

var myObj, x= "" ,y="";

myObj={ 
    "Movie":["Deadpool", "Xmen", "Hunger Games" , "RoboCop" , "LOTR" ,"Starwar"],
    "Rating" : [ 8 , 6 , 7 , 2 , 8 , 7 ] 
};

document.write("<table border='1'>");
document.write("<tr>Moving<td></td><td>Rating</td></tr>");
for (i in myObj.Movie) 
{
    document.write("<tr>");
    document.write("<td width='80' >"+myObj.Movie[i]+"</td>");
    document.write("<td width='80' >"+myObj.Rating[i]+"</td>");
    document.write("</tr>")
}

document.write("</table>");

</script>

1 Comment

Thank you! but the ''movie'' move out from the border already but the other work fine.
1

After document.write("<table border='1'>"), try adding th's like this:

<tr>
    <th>Movie</th>
    <th>Rating</th>
</tr>

It will look like this:

<table>
  <tr>
    <th>Movie</th>
    <th>Rating</th>
  </tr>
  <tr>
    <td>Batman</td>
    <td>9001</td>
  </tr>

Then you can add some CSS like this to make it easier to read:

table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}

tr:nth-child(even) {
    background-color: #dddddd;
}

The CSS is from HTML Tables (W3Schools).

1 Comment

Thank you for your explaination! That's great for me.
1

Try this:

<script>
    var myObj, x= "" ,y="";

    myObj={
        "Movie":["Deadpool", "Xmen", "Hunger Games" , "RoboCop" , "LOTR" ,"Starwar"],
        "Rating" : [ 8 , 6 , 7 , 2 , 8 , 7 ]
    };

    document.write("<table border='1'>")
    for (var i=0;i<myObj.Movie.length;i++)
    {
        document.write("<tr>");
        document.write("<td width='80' >"+myObj.Movie[i]+"</td>");
        document.write("<td width='80' >"+myObj.Rating[i]+"</td>");
        document.write("</tr>")
    }

    document.write("</table>")
</script>

1 Comment

I was also use this method previously, but I didn't write it correctly. Thanks for showing me the correct one :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.