I am currently working on a task that involves converting three separate inputs into a multidimensional array.
Although I am aware that users can input up to 8 values, my existing code is generating empty arrays. I need to find a way to dynamically modify my code in order to avoid this issue. Below is the function that I have created:
a = 'Hi'
b = 878
c = 654
function convertToTable(a, b, c) {
for (var i = 0; i < 8; i++) {
result[i] = new Array(8);
}
result[0][0] = a[0];
result[0][1] = b[0];
result[0][2] = c[0];
result[1][0] = a[1];
result[1][1] = b[1];
result[1][2] = c[1];
result[2][0] = a[2];
result[2][1] = b[2];
result[2][2] = c[2];
result[3][0] = a[3];
result[3][1] = b[3];
result[3][2] = c[3];
result[4][0] = a[4];
result[4][1] = b[4];
result[4][2] = c[4];
result[5][0] = a[5];
result[5][1] = b[5];
result[5][2] = c[5];
result[6][0] = a[6];
result[6][1] = b[6];
result[6][2] = c[6];
result[7][0] = a[7];
result[7][1] = b[7];
result[7][2] = c[7];
for (var i = 0; i < 8; i++) {
for (var j = 0; j < 3; j++) {
document.write(result[i][j] + ' ');
}
document.write('</br>');
}
}
The expected output of the array should look like the following if a = 'Hi', b = 878, and c = 654:
However, please note that 'a', 'b', and 'c' are arrays storing their own data. Each element in 'a' will be a string, while each element in 'b' and 'c' will be integers.
Hi 878 654
Hi 878 654
Hi 878 654
ETC....