I am currently trying to take 3 seperate inputs and convert them into a multidimensional array.
I know the most inputs that a user can enter is 8 however my current code prints the empty arrays. I am looking for a way to dynamically create what I have coded to avoid this issue. Below is a copy of the function I made.
a = 'Hi'
b = 878
c = 654
function exitToTable(a, b, c) {
for (var i = 0; i < 8; i++) {
tester[i] = new Array(8);
}
tester[0][0] = a[0];
tester[0][1] = b[0];
tester[0][2] = c[0];
tester[1][0] = a[1];
tester[1][1] = b[1];
tester[1][2] = c[1];
tester[2][0] = a[2];
tester[2][1] = b[2];
tester[2][2] = c[2];
tester[3][0] = a[3];
tester[3][1] = b[3];
tester[3][2] = c[3];
tester[4][0] = a[4];
tester[4][1] = b[4];
tester[4][2] = c[4];
tester[5][0] = a[5];
tester[5][1] = b[5];
tester[5][2] = c[5];
tester[6][0] = a[6];
tester[6][1] = b[6];
tester[6][2] = c[6];
tester[7][0] = a[7];
tester[7][1] = b[7];
tester[7][2] = c[7];
for (var i = 0; i < 8; i++) {
for (var j = 0; j < 3; j++) {
document.write(tester[i][j] + ' ');
}
document.write('</br>');
}
}
I am hoping for the array to print out as follows if a = 'Hi' b = 878 c = 654
However note that 'a' 'b' and 'c' are all arrays storing their own data. Each element in a will be a string and each element in b and c are integers.
Hi 878 654
Hi 878 654
Hi 878 654
ETC....
This should give you the result you expect:
const a = ['Hi', 'Hi', 'g', 'j', 'm', 'p', 's', 'v']; const b = ['900', '900', 'h', 'k', 'n', 'q', 't', 'w']; const c = ['654', '654', 'i', 'l', 'o', 'r', 'u', 'x']; function exitToTable(a, b, c) { let length = a.length; if (length !== b.length || length !== c.length) { console.warn('arrays must be of same length'); } [...Array(length)].map((_, index) => { document.write(`${[a[index], b[index], c[index]].join(' ')}<br/>`); }); } exitToTable(a, b, c);
Hope this helps,
var arr_a = [], arr_b = [], arr_c = []; for (var i = 0; i <= 7; i++) { arr_a[i] = 'Hi'; arr_b[i] = 878; arr_c[i] = 654; } exitToTable(arr_a, arr_b, arr_c); function exitToTable(a, b, c) { let tester = []; tester[0] = []; tester[0][0] = a[0]; tester[0][1] = b[0]; tester[0][2] = c[0]; tester[1] = []; tester[1][0] = a[1]; tester[1][1] = b[1]; tester[1][2] = c[1]; tester[2] = []; tester[2][0] = a[2]; tester[2][1] = b[2]; tester[2][2] = c[2]; tester[3] = []; tester[3][0] = a[3]; tester[3][1] = b[3]; tester[3][2] = c[3]; tester[4] = []; tester[4][0] = a[4]; tester[4][1] = b[4]; tester[4][2] = c[4]; tester[5] = []; tester[5][0] = a[5]; tester[5][1] = b[5]; tester[5][2] = c[5]; tester[6] = []; tester[6][0] = a[6]; tester[6][1] = b[6]; tester[6][2] = c[6]; tester[7] = []; tester[7][0] = a[7]; tester[7][1] = b[7]; tester[7][2] = c[7]; for (var i = 0; i < 8; i++) { for (var j = 0; j < 3; j++) { //document.write(tester[i][j] + ' '); } //document.write('</br>'); } console.log(tester); console.log(tester[0][0]); }
var array = []; var array2 = []; var array3 = []; array2.push('text-1'); array2.push('text-2'); array3.push('item-1'); array3.push('item-2'); array.push(array2); array.push(array3); console.log(array); console.log(array[0][1]);
Find out length of array: array.length
.
var array = []; var array2 = []; array2.push('text-1'); array2.push('text-2'); array[0] = array2; console.log(array);
The answer I came upon with the help of my employer brainstorming with me to push me in the right direction is as follows:
function generateTable(a, b, c) {
document.write('<table cellspacing="5" cellpadding="1" border="1"><tr><th>State/Teritory</th><th>population</th><th>Growth Rate</th></tr>')
for (var i = 0; i < a.length; i++) {
document.write('<tr><td>' + a[i] + '</td><td>' + b[i] + '</td><td>' + c[i] + '</td></tr>');
}
document.write('</table><br><hr>');
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.