In the previous examples, all the numbers in the subarrays were the same. Let's make the numbers go up now, like this:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
To do this, you need to make a special counter
variable that will increase its value by 1
with each iteration of the inner loop. We will
write the value of this counter to the array,
like this:
let arr = [];
let k = 1; // the counter
for (let i = 0; i < 3; i++) {
arr[i] = [];
for (let j = 0; j < 3; j++) {
arr[i].push(k); // write down the counter
k++; // increment the counter
}
}
console.log(arr);
You can simplify the code by incrementing the counter after the assignment:
let arr = [];
for (let i = 0, k = 1; i < 3; i++) {
arr[i] = [];
for (let j = 0; j < 3; j++) {
arr[i].push(k++);
}
}
console.log(arr);
Please note that in this case, it must be
k++
, and not ++k
, since the
second option will first increment the
counter, and only then write to the array
(that is, as a result, the array will start
from two, and not from one), like this:
[[2, 3, 4], [5, 6, 7], [8, 9, 10]])
Form the following array using two nested loops:
[[1, 2], [3, 4], [5, 6], [7, 8]]
Form the following array using two nested loops:
[[2, 4, 6], [8, 10, 12], [14, 16, 18], [20, 22, 24]]
Form the following 3D array using three nested loops:
[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
The author of the following code wanted to make such an array:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The following code was written for this:
let arr = [];
for (let i = 0; i < 3; i++) {
arr[i] = [];
for (let j = 0; j < 3; j++) {
arr[i][j] = k;
k++;
}
}
console.log(arr);
The written code, however, does not do what it was intended. Find and correct the author's mistake.
The author of the following code wanted to make such an array:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The following code was written for this:
let arr = [];
let k = 1;
for (let i = 0; i < 3; i++) {
arr[i] = [];
for (let j = 0; j < 3; j++) {
arr[i][j] = k;
}
}
console.log(arr);
The written code, however, does not do what it was intended. Find and correct the author's mistake.
The author of the following code wanted to make such an array:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The following code was written for this:
let arr = [];
let k = 1;
for (let i = 0; i < 3; i++) {
arr[i] = [];
for (let j = 0; j < 3; j++) {
arr[i][j] = k;
k--;
}
}
console.log(arr);
The written code, however, does not do what it was intended. Find and correct the author's mistake.
The author of the following code wanted to make such an array:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The following code was written for this:
let arr = [];
let k;
for (let i = 0; i < 3; i++) {
arr[i] = [];
for (let j = 0; j < 3; j++) {
arr[i][j] = k;
k++;
}
}
console.log(arr);
The written code, however, does not do what it was intended. Find and correct the author's mistake.
The author of the following code wanted to make such an array:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The following code was written for this:
let arr = [];
for (let i = 0, k = 1; i < 3; i++) {
arr[i] = [];
for (let j = 0; j < 3; j++) {
arr[i][j] = k;
}
}
console.log(arr);
The written code, however, does not do what it was intended. Find and correct the author's mistake.
The author of the following code wanted to make such an array:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The following code was written for this:
let arr = [];
for (let i = 0, k = 1; i < 3; i++) {
arr[i] = [];
for (let j = 0, k++; j < 3; j++) {
arr[i][j] = k;
}
}
console.log(arr);
The written code, however, does not do what it was intended. Find and correct the author's mistake.
The author of the following code wanted to make such an array:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The following code was written for this:
let arr = [];
for (let i = 0, k = 1; i < 3; i++) {
arr[i] = [];
for (let j = 0; j < 3; j++; k++) {
arr[i][j] = k;
}
}
console.log(arr);
The written code, however, does not do what it was intended. Find and correct the author's mistake.
The author of the following code wanted to make such an array:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The following code was written for this:
let arr = [];
for (let i = 0, k = 1; i < 3; i++) {
arr[i] = [];
for (let j = 0; j < 3; j++) {
arr[i][j] = ++k;
}
}
console.log(arr);
The written code, however, does not do what it was intended. Find and correct the author's mistake.
The author of the following code wanted to make such an array:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The following code was written for this:
let arr = [];
let k = 1;
for (let i = 0; i < 3; i++) {
arr[k] = [];
for (let j = 0; j < 3; j++) {
arr[i][j] = k;
k++;
}
}
console.log(arr);
The written code, however, does not do what it was intended. Find and correct the author's mistake.
The author of the following code wanted to make such an array:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The following code was written for this:
let arr = [];
for (let i = 0, k = 1; i < 3; i++) {
arr[i] = [];
for (let j = 0; j < 3; k++) {
arr[i][j] = k;
}
}
console.log(arr);
The written code, however, does not do what it was intended. Find and correct the author's mistake.