I have the code below. How do I make it to be 6 arrays inside an array, and the value to be increased by 1 for every variable | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

I have the code below. How do I make it to be 6 arrays inside an array, and the value to be increased by 1 for every variable

because there is 0x0 at the first variable, there was a error so i gave to p the starting value of 1. Also, I don't know how to properly avoid the px0 or 0xz cases. It should look like this: [1, 2, 3, 4, 5, 6] [7, 8, 9, 10, 11, 12] ... [31, 32, 33, 34, 35, 36] https://code.sololearn.com/Wa5a22A21a10

23rd May 2021, 6:38 PM
🍇 Alex Tușinean 💜
🍇 Alex Tușinean 💜 - avatar
5 Answers
+ 6
let arr1 = new Array(6).fill(0).map((_, i) => i+1); let arr2 = arr1.map(n=>n+6); Same concept for nested loop: https://code.sololearn.com/W4y19PXpwQFB/?ref=app
23rd May 2021, 6:39 PM
Gordon
Gordon - avatar
+ 4
Gordon as you don't use the new array values, you doesn't need to put argument in fill() method (array will be filled with undefined) Array() constructor doesn't require 'new' keyword (and work same without: that will shorter the line) 🍇 Alex Tușinean 💜 to initialize an array of six elements starting at x and increasing by one for each element, you could do: var x = 42; var a = Array(6).fill().map((_,i) => x+i);
23rd May 2021, 6:47 PM
visph
visph - avatar
+ 3
visph Didn't aware, thanks for pointing out 🙂
23rd May 2021, 6:51 PM
Gordon
Gordon - avatar
0
🍇 Alex Tușinean 💜 as alternative, you could build your nested arrays in one assignement (splitted to multilines for readability): https://code.sololearn.com/WcMEdULT4SW5/?ref=app
23rd May 2021, 7:19 PM
visph
visph - avatar
0
You can also do something like this:- https://code.sololearn.com/WSRkar2m94IB/?ref=app
25th May 2021, 8:41 AM
Usman Adam Imam
Usman Adam Imam - avatar