+ 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
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
+ 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);
+ 3
visph Didn't aware, thanks for pointing out đ
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
0
You can also do something like this:-
https://code.sololearn.com/WSRkar2m94IB/?ref=app