Beginner here, but I have spent a long time trawling other questions to try to debug this. My issue is as follows:
I take two arrays, 'left' and 'right', calculate the ratios between them (with a function that I already have working) outputting a 3x11 array with the ratios.
What I'm trying to produce in the end is a table with 4 rows and 12 columns. The [0] row will list 'right', the [0] column will list left, and the rest of the table will then host the corresponding ratios.
It should then replace the relevant div within the html.
Maybe it's something obvious and I'm just totally missing it, or it's just a hole in my knowledge. Either way, I've spent most of the day on it!
<!-- html -->
<body>
<input type="input" value="Enter a value"></input>
<div id="results_table">
Ratios:
<div id="result">ratios listed here</div>
<div id="result1">tables here</div>
</div>
</body>
//Javascript\\
$("input").change(function () {
var left = new Array(3);
var right = new Array(11);
left = [30, 40, 50];
right = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21];
result = fullCalc(left, right); //this calculates the ratios
$("#result").append(result);
var ratios = result;
resultstable = tableCreate(left,right,ratios);
$("#result1").append(resultsTable);
});
function tableCreate(a, b, c) {
newTable = document.getElementById("result1");
var tbl = document.createElement('table');
var tbdy = document.createElement('tbody');
tbl.style.width = '100%';
tbl.setAttribute('border', '1');
tbl.setAttribute('id', "results_tbl");
for (var i = 0; i < a.length + 1; i++) {
var tr = document.createElement('tr');
tr.setAttribute('id', "tablerow" + i);
for (var j = 0; j < b.length + 1; j++) {
var td = document.createElement('td');
if (i >= 1 && j === 0) {
var frontText = document.createTextNode(a[i-1]);
td.append(leftText);
td.setAttribute('id', "leftTable");
} else if (i === 0 && j >= 1) {
var rearText = document.createTextNode(b[j-1]);
td.append(rearText);
td.setAttribute('id', "rightTable");
} else {
var ratioText = document.createTextNode(c[i-1][j-1]);
td.appendChild(ratioText);
td.setAttribute('id', "ratioTable");
}
tr.appendChild(td);
}
tbdy.appendChild(tr);
}
tbl.appendChild(tbdy);
}
The problem is that no table appears, and I'm unsure if one is even being created. Any ideas?
var left = new Array(3);fine, old syntax but valid.left = [30, 40, 50];now you overrided the array you allocated before, just use the second line. also, you never declaredresult.