I’m trying to get all the possible combinations of pairs
const arr = [1,2,3];
const groups = [];
for (let i = 0, i < arr.length, i++) {
for (let j = i + 1, j < arr.length, j++) {
groups.push(arr[i] + ", " + arr[j]);
}
}
document.body.innerHTML = groups;
3
Answers
You’ve used commas
,
instead of semicolons;
in thefor loop
definitions.The code should look like this:
The code you provided has some syntax errors in the for loops. The correct syntax for a for loop is for (initialization; condition; increment). You have used commas instead of semicolons to separate the initialization, condition, and increment parts.
There are two mistakes here. The first is that the syntax is the for loop is
and not
Secondly, in the seond for loop, you initalized j, as
However, if you would like all possible combinations, then it j should be 0 instead.
So you would have: