0

I know there are a lot of similar questions out there. This code is a Frankenstein of a lot of other stack overflow questions. But I am so close I just don't understand the code I've been trying to use an examples very well. Here is my html page:

<!DOCTYPE html>
<html>
    <script src="Scripts.js"></script> 
    <script>


    </script>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"> </script>

</head>

<body>

    <p id="demo"></p>


    <script>

      obj.Blades.forEach(element => {
          var name = element.Name + " " + element.Damage;
          document.write(name + "<br >");
      });
    </script>

<input type="button" value="Generate Table" onclick="makeTable()" />
<hr />
<div id="dvTable"></div>

</body>
</html>

And here is the Java Script page:


var jsonStuff = '{ "Blades" : [' +
'{ "Name":"Longsword" , "Damage":"l2d" },' +
'{ "Name":"Dagger" , "Damage":"l3d" },' +
'{ "Name":"Mace" , "Damage":"l4d" },' +
'{ "Name":"Spear" , "Damage":"l5d" } ]}'; 


var obj = JSON.parse(jsonStuff);

function makeTable(){
  //Create a HTML Table element.
  var table = document.createElement("TABLE");
  table.border = "1"

 //Get the count of columns.
 var columnCount = Object.keys(obj.Blades).length;

 //Add the header row.
 var row = table.insertRow(-1);
   for (var i = 0; i < columnCount; i++) {
       var headerCell = document.createElement("TH");
       headerCell.innerHTML = obj.Blades[i].Name;
       row.appendChild(headerCell);
   }

    //Add the data rows.
    for (var i = 1; i < obj.Blades.length; i++) {

       row = table.insertRow(-1);
       for (var j = 0; j < columnCount; j++) {
           console.log(obj.Blades[j].Damage);
           var cell = row.insertCell(-1);
           cell.innerHTML = obj.Blades[i][j];
       }
   }

   var dvTable = document.getElementById("dvTable");
   dvTable.innerHTML = "";
   dvTable.appendChild(table);
}

This is what it looks like right now:

enter image description here

So I know the problem has to be somewhere in the section of JavaScript commented "add the Data rows". I'm just now sure how to go about it.

1
  • An image of your browser is not very helpful. Please open your browser's dev tools and look at the current generated HTML in your browser and see what is wrong. Commented Apr 22, 2020 at 3:06

1 Answer 1

1

I believe your problem is with the line:

cell.innerHTML = obj.Blades[i][j];

You are referring to Blades as if it were a 2-dimensional array, when in fact it is an array of objects. You're going to need to have something like this to avoid the undefined:

cell.innerHTML = obj.Blades[i].Name;
cell.innerHTML = obj.Blades[i].Damage;
Sign up to request clarification or add additional context in comments.

1 Comment

Yes this helped a lot. No longer undefined but the correct info isnt quite displaying. Thank you though.

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.