How do I iterate over an array?
-
I need to create a code that will calculate the potentials for solving a transport problem.
Some of the code was created by another person as a separate task. I need to insert the equation calculation part into it.
But an error occurs when I drive the data into an array.
var klick_button = document.getElementById('ras'); klick_button.onclick = function() { var cel_fun = 0; var rows = document.getElementById("x").value, columns = document.getElementById("y").value; for(var i = 0; i < rows; i++){ for(var j = 0; j < columns; j++){ var value_1, value_2; value_1 = parseInt(document.getElementById('input-' + i + "-" + j).value); value_2 = parseInt(document.getElementById('input2-' + i + "-" + j).value); cel_fun = cel_fun + (value_1 * value_2); var u=[]; var v=[]; var mat=[ [], [], ]; n=n+1; if (value_1 != 0) { mat[n][0]=u[i]; mat[n][1]=v[j]; mat[n][2]=value_2; } } } alert(mat[0][2]); }
value_1 and value_2 are data that the user enters into the table.
When I use mat [i] [2] during the iteration, the function works, but this is correct, since the volume should be larger for me.
When I write mat [n] [2], the function simply does not work and does not output anything.
Tell me what the problem is and what method is more optimal to solve a system of equations, the number of which depends on the size of the table.JavaScript Arya Stuart, Jan 8, 2019 -
If I understand correctly, then you should make the declaration of materts outside the for loop.
var klick_button = document.getElementById('ras');
klick_button.onclick = function() {
var cel_fun = 0;
var u=[];
var v=[];
var mat=[
[],
[],
];
var rows = document.getElementById("x").value,
columns = document.getElementById("y").value;
for(var i = 0; i < rows; i++){
for(var j = 0; j < columns; j++){
var value_1, value_2;
value_1 = parseInt(document.getElementById('input-' + i + "-" + j).value);
value_2 = parseInt(document.getElementById('input2-' + i + "-" + j).value);
cel_fun = cel_fun + (value_1 * value_2);
n=n+1;
if (value_1 != 0) {
mat[n][0]=u[i];
mat[n][1]=v[j];
mat[n][2]=value_2;
}
}
}
alert(mat[0][2]);
}Anonymous
1 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!