0

I have an array and I want to make an big array which consist of the original array number from zero to that number, it means if my array is:

a = [3,5,9] 

then I want to make an array like below array:

b = [ 
      [0,1,2,3],
      [0,1,2,3,4,5],
      [0,1,2,3,4,5,6,7,8,9]
    ]

how can I get "b" from "a" ?

and this is my code:

let indices = [3,5,9];
let arrIndices = [];

for (let i = 0; i < indices.length; i++) {
  console.log("k", indices[i]);
  let a = [];
  for (let j = 0; j < indices[i]; j++) {
    console.log("jj", j);
    a = a.concat(j);
    arrIndices.push(a);
  }
}

console.log(arrIndices);

3
  • 3
    Use some loops. Where are you stuck exactly? What did you try so far? Commented Sep 22, 2020 at 16:30
  • this is my code so far Commented Sep 22, 2020 at 16:37
  • let arrIndices = []; for (let i = 0; i < indices.length; i++) { console.log("k", indices[i]); let a = []; for (let j = 0; j < indices[i]; j++) { console.log("jj", j); a = a.concat(indices[i]); arrIndices.push(a); } } Commented Sep 22, 2020 at 16:43

2 Answers 2

1

Your outer loop, which loops i from 0 to indices.length, should produce exactly one array in the result per iteration. Because you want to transform each number to one array - it's a one to one mapping.

By putting arrIndices.push(a); inside the inner loop, you ensure that it runs many more times. So the result will have many more arrays than just one per number.

All you need to do is build the result array using the inner loop, then push it after the inner loop is done.

Also, a = a.concat(indices[i]) will only push the original number (3, 5, or 9) over and over, not anything else like 0, 1, 2, etc. For that just use j itself.

Finally, since you want it to be inclusive, you should have j <= indices[i] so it will loop up to and including indices[i].

let indices = [3,5,9];
let arrIndices = [];

for (let i = 0; i < indices.length; i++) {
  console.log("k", indices[i]);
  let a = [];
  for (let j = 0; j <= indices[i]; j++) {
    console.log("jj", j);
    a.push(j);
  }
  arrIndices.push(a);
}

console.log(arrIndices);

However JavaScript has several array helpers and they can make this code very short. Remember how I said this is a one to one mapping? Javascript has a helper for that - Array#map. There is also a shortcut to making an array of 0..N from this answer using the Array constructor and Array#keys() method.

let indices = [3,5,9];

let result = indices.map(n => [...Array(n+1).keys()]);

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

0

First of all you have to add the a outside the inside for loop, because you want to create an array of i elemenets not an array of j*i elements.

let indices = [3,5,9];
let arrIndices = [];

for (let i = 0; i < indices.length; i++) {
  let a = [];
  for (let j = indices[i]; j >= 0; j--) {
    a.push(indices[i]-j)
    
  }
  arrIndices.push(a);
}

console.log(arrIndices);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.