A loop inside another runs completely for every round of the outer one: two loops of ten give a hundred rounds. That is how you fill a grid or visit every pair. Build a grid as an array of rows: make an empty row in the outer loop, fill it in the inner loop, push it to the result.
To pair items without repeats, start the inner index one past the outer index, for (let j = i + 1; ...).
for (let row = 0; row < 2; row++) {
for (let col = 0; col < 3; col++) {
console.log(row, col);
}
}
// 0 0, 0 1, 0 2, 1 0, 1 1, 1 2Your task
Write multiplicationTable(n) returning n rows where row i holds i * 1 to i * n, so multiplicationTable(3) is [[1, 2, 3], [2, 4, 6], [3, 6, 9]]. Then pairs(items) returning every pair once, in order, as two-element arrays: pairs(["a", "b", "c"]) is [["a", "b"], ["a", "c"], ["b", "c"]].
Hint 1
for (let i = 1; i <= n; i++) { const row = []; for (let j = 1; j <= n; j++) row.push(i * j); rows.push(row); }
Hint 2
for (let i = 0; i < items.length; i++) for (let j = i + 1; j < items.length; j++) result.push([items[i], items[j]]);
Show the solution
function multiplicationTable(n) {
const rows = [];
for (let i = 1; i <= n; i++) {
const row = [];
for (let j = 1; j <= n; j++) {
row.push(i * j);
}
rows.push(row);
}
return rows;
}
function pairs(items) {
const result = [];
for (let i = 0; i < items.length; i++) {
for (let j = i + 1; j < items.length; j++) {
result.push([items[i], items[j]]);
}
}
return result;
}