Your code is only generating this output: <table><tr>
.
As you can see, only a small part of your code was being assigned to the variable results
, this why you start a for
(that by itself nothing returns).
After you start this structure nothing else is concatenated to Results, due to some problems of "semantics".
In javascript the ;
can be omitted after a line break, so you didn’t have a syntax "error" with the start of for
and yet he was executed without a problem.
Besides, <tr>
creates a single line, apparently you need to create one for each record of your JSON, right? If yes, you should concatenate it in each iteration of your for
.
So consider changing from:
var tamanhoJson = json.length;
results =
"<table>"+
"<tr>"
for (var i = 0; i < json.length; i++)
{
+"<td>"+json[i].tamanhoArray + " Elementos</td>"+
"<td>"+json[i].nome + " </td>"+
"<td>"+json[i].tempoNS + " ns</td>"+
"<td>"+json[i].tempoMS + " ms</td>"
}
+"</tr>"+
"</table>"+
"<br />";
For something like this:
var tamanhoJson = json.length;
var results = "<table>";
for (var i = 0; i < tamanhoJson; i++) {
results += "<tr>";
results += "<td>" + json[i].tamanhoArray + " Elementos</td>";
results += "<td>" + json[i].nome + " </td>";
results += "<td>" + json[i].tempoNS + " ns</td>";
results += "<td>" + json[i].tempoMS + " ms</td>";
results += "</tr>";
}
results +="</table>";
results += "<br />";
Below is a full and functional example, see if this is what you really need:
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
var json = [{
"tempoNS": 104095548,
"tempoMS": 104,
"tamanhoArray": 9000,
"nome": "Bubble iterativo"
}, {
"tempoNS": 84671736,
"tempoMS": 84,
"tamanhoArray": 9000,
"nome": "Bubble recursivo"
}, {
"tempoNS": 22793428,
"tempoMS": 22,
"tamanhoArray": 9000,
"nome": "Insertion iterativo"
}, {
"tempoNS": 243897911,
"tempoMS": 243,
"tamanhoArray": 9000,
"nome": "Insertion recursivo"
}, {
"tempoNS": 57896229,
"tempoMS": 57,
"tamanhoArray": 9000,
"nome": "Selection iterativo"
}, {
"tempoNS": 34119355,
"tempoMS": 34,
"tamanhoArray": 9000,
"nome": "Selection recursivo"
}, {
"tempoNS": 6322368,
"tempoMS": 6,
"tamanhoArray": 9000,
"nome": "Merge iterativo"
}, {
"tempoNS": 2057750,
"tempoMS": 2,
"tamanhoArray": 9000,
"nome": "Merge recursivo"
}, {
"tempoNS": 1600741,
"tempoMS": 1,
"tamanhoArray": 9000,
"nome": "Quick recursivo"
}, {
"tempoNS": 2621617,
"tempoMS": 2,
"tamanhoArray": 9000,
"nome": "Heap recursivo"
}, {
"tempoNS": 3081947,
"tempoMS": 3,
"tamanhoArray": 9000,
"nome": "CombSort Sem Otimização"
}, {
"tempoNS": 3163448,
"tempoMS": 3,
"tamanhoArray": 9000,
"nome": "CombSort Com Otimização"
}];
var tamanhoJson = json.length;
var results = "<table>";
for (var i = 0; i < tamanhoJson; i++) {
results += "<tr>";
results += "<td>" + json[i].tamanhoArray + " Elementos</td>";
results += "<td>" + json[i].nome + " </td>";
results += "<td>" + json[i].tempoNS + " ns</td>";
results += "<td>" + json[i].tempoMS + " ms</td>";
results += "</tr>";
}
results +="</table>";
results += "<br />";
var div = document.getElementById("tabelaDinamica2");
console.log(results);
div.innerHTML = results;
});
</script>
<body>
<div id="tabelaDinamica2"></div>
</body>
</html>