I have a vector containing data that needs to be stored in the following order:
cAge[0] = 'Age (1) (1)';
cAge[1] = 'Age (1) (2)';
cAge[2] = 'Age (1) (3)';
cAge[3] = 'Age (2) (1)';
cAge[4] = 'Age (2) (2)';
cAge[5] = 'Age (2) (3)';
cAge[6] = 'Age (3) (1)';
cAge[7] = 'Age (3) (2)';
cAge[8] = 'Age (3) (3)';
cAge[9] = 'Age (4) (1)';
cAge[10] = 'Age (4) (2)';
cAge[11] = 'Age (4) (3)';
This can be achieved with a for
loop without any complications:
var cAge = [];
for (var i=1; i<=4; i++) {
for (var j=1; j<=3; j++) {
cAge.push('Age (' + i + ')' + ' ' + '(' + j + ')');
}
}
However, I am facing an issue when trying to display all the elements using a nested loop:
for (var i=1; i<=4; i++) {
for (var j=1; j<=3; j++) {
console.log(cAge[j-1]); // <- What should j-1 be?
}
}
Currently, it outputs:
Age (1) (1)
Age (1) (2)
Age (1) (3)
Age (1) (1)
Age (1) (2)
Age (1) (3)
Age (1) (1)
Age (1) (2)
Age (1) (3)
Age (1) (1)
Age (1) (2)
Age (1) (3)
What should the value of j-1
represent here?