Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

Improved formatting
Source Link
Alex McMillan
  • 18.1k
  • 13
  • 63
  • 91

I have an array—array: [1, 2, 3, 4, 5, 6, 7, 8, 9]

I want to create a 2D array with three 1D arrays. Each NUM in the function variables is the length of each 1D array. The result should be [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

But all I get is ,,3,,,6,,,9. What am I doing wrong?

function infiniteLoop(arr, num) {
 var answer = [];
 var count = 0;
 for (let i = 0; i < num.length; i++) {
 for (let j = 0; j < num[i]; j++, count++) {
 answer[i] = [];
 answer[i][j] = arr[count];
 }
 }
 return answer;
}
document.write(infiniteLoop([1, 2, 3, 4, 5, 6, 7, 8, 9], [3, 3, 3]));

I have an array—[1, 2, 3, 4, 5, 6, 7, 8, 9]

I want to create a 2D array with three 1D arrays. Each NUM in the function variables is the length of each 1D array. The result should be [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

But all I get is ,,3,,,6,,,9. What am I doing wrong?

function infiniteLoop(arr, num) {
 var answer = [];
 var count = 0;
 for (let i = 0; i < num.length; i++) {
 for (let j = 0; j < num[i]; j++, count++) {
 answer[i] = [];
 answer[i][j] = arr[count];
 }
 }
 return answer;
}
document.write(infiniteLoop([1, 2, 3, 4, 5, 6, 7, 8, 9], [3, 3, 3]));

I have an array: [1, 2, 3, 4, 5, 6, 7, 8, 9]

I want to create a 2D array with three 1D arrays. Each NUM in the function variables is the length of each 1D array. The result should be [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

But all I get is ,,3,,,6,,,9. What am I doing wrong?

function infiniteLoop(arr, num) {
 var answer = [];
 var count = 0;
 for (let i = 0; i < num.length; i++) {
 for (let j = 0; j < num[i]; j++, count++) {
 answer[i] = [];
 answer[i][j] = arr[count];
 }
 }
 return answer;
}
document.write(infiniteLoop([1, 2, 3, 4, 5, 6, 7, 8, 9], [3, 3, 3]));

I have an array - [1, 2, 3, 4, 5, 6, 7, 8, 9]array—[1, 2, 3, 4, 5, 6, 7, 8, 9]

I want to create a 2D array with three 1D arrays. Each NUM in the function variables is the length of each 1D array. The result should be - [[1, 2, 3], [4, 5, 6], [7, 8, 9]][[1, 2, 3], [4, 5, 6], [7, 8, 9]]

But all iI get it ",,3,,,6,,,9"is ,,3,,,6,,,9. What am I doing wrong?

function infiniteLoop(arr, num) {
 var answer = [];
 var count = 0;
 for (let i = 0; i < num.length; i++) {
 for (let j = 0; j < num[i]; j++, count++) {
 answer[i] = [];
 answer[i][j] = arr[count];
 }
 }
 return answer;
}
document.write(infiniteLoop([1, 2, 3, 4, 5, 6, 7, 8, 9], [3, 3, 3]));

I have an array - [1, 2, 3, 4, 5, 6, 7, 8, 9]

I want to create a 2D array with three 1D arrays. Each NUM in the function variables is the length of each 1D array. The result should be - [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

But all i get it ",,3,,,6,,,9" What am I doing wrong?

function infiniteLoop(arr, num) {
 var answer = [];
 var count = 0;
 for (let i = 0; i < num.length; i++) {
 for (let j = 0; j < num[i]; j++, count++) {
 answer[i] = [];
 answer[i][j] = arr[count];
 }
 }
 return answer;
}
document.write(infiniteLoop([1, 2, 3, 4, 5, 6, 7, 8, 9], [3, 3, 3]));

I have an array—[1, 2, 3, 4, 5, 6, 7, 8, 9]

I want to create a 2D array with three 1D arrays. Each NUM in the function variables is the length of each 1D array. The result should be [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

But all I get is ,,3,,,6,,,9. What am I doing wrong?

function infiniteLoop(arr, num) {
 var answer = [];
 var count = 0;
 for (let i = 0; i < num.length; i++) {
 for (let j = 0; j < num[i]; j++, count++) {
 answer[i] = [];
 answer[i][j] = arr[count];
 }
 }
 return answer;
}
document.write(infiniteLoop([1, 2, 3, 4, 5, 6, 7, 8, 9], [3, 3, 3]));

added 10 characters in body
Source Link
Alexey Tseitlin
  • 1.3k
  • 6
  • 19
  • 35

I have an array - [1, 2, 3, 4, 5, 6, 7, 8, 9]

I want to create a 2D array with three 1D arrays. Each NUM in the function variables is the length of each 1D array. The result should be - [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

But the "i" in "answer[i][j]" gives an error...all i get it ",,3,,,6,,,9" What am I doing wrong?

function infiniteLoop(arr, num) {
 var answer = [];
 var count = 0;
 for (let i = 0; i < num.length; i++) {
 for (let j = 0; j < num[i]; j++, count++) {
 answer[i] = [];
 answer[i][j] = arr[count];
 }
 }
 return answer;
}
document.write(infiniteLoop([1, 2, 3, 4, 5, 6, 7, 8, 9], [3, 3, 3]));

I have an array - [1, 2, 3, 4, 5, 6, 7, 8, 9]

I want to create a 2D array with three 1D arrays. Each NUM in the function variables is the length of each 1D array. The result should be - [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

But the "i" in "answer[i][j]" gives an error... What am I doing wrong?

function infiniteLoop(arr, num) {
 var answer = [];
 var count = 0;
 for (let i = 0; i < num.length; i++) {
 for (let j = 0; j < num[i]; j++, count++) {
 answer[i][j] = arr[count];
 }
 }
 return answer;
}
document.write(infiniteLoop([1, 2, 3, 4, 5, 6, 7, 8, 9], [3, 3, 3]));

I have an array - [1, 2, 3, 4, 5, 6, 7, 8, 9]

I want to create a 2D array with three 1D arrays. Each NUM in the function variables is the length of each 1D array. The result should be - [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

But all i get it ",,3,,,6,,,9" What am I doing wrong?

function infiniteLoop(arr, num) {
 var answer = [];
 var count = 0;
 for (let i = 0; i < num.length; i++) {
 for (let j = 0; j < num[i]; j++, count++) {
 answer[i] = [];
 answer[i][j] = arr[count];
 }
 }
 return answer;
}
document.write(infiniteLoop([1, 2, 3, 4, 5, 6, 7, 8, 9], [3, 3, 3]));

Source Link
Alexey Tseitlin
  • 1.3k
  • 6
  • 19
  • 35
Loading
lang-js

AltStyle によって変換されたページ (->オリジナル) /