Create complex multi-dimensional arrays

Create complex multi-dimensional arrays
0

#1

Tell us what’s happening:

This is really confused with multi array and I am lost the track… I don’t know which one

However I have looked up at challenge solution but its wrong

let myNestedArray = [
	[
		1, 2, 3, 3, 5, 6, [true, false, false, 12]
	]
];

the console log said
// running test
myNestedArray should have exactly 5 levels of depth
myNestedArray should contain exactly one occurrence of the string “deep” on an array nested 3 levels deep
myNestedArray should contain exactly one occurrence of the string “deeper” on an array nested 4 levels deep
myNestedArray should contain exactly one occurrence of the string “deepest” on an array nested 5 levels deep
// tests completed

Your code so far


let myNestedArray = [
	[
		1, 2, 3, 3, 5, 6, [true, false, false, 12]
	]
];

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/create-complex-multi-dimensional-arrays


#2

the question asked us to slove it

  • myNestedArray should contain only numbers, booleans, and strings as data elements
  • myNestedArray should have exactly 5 levels of depth
  • myNestedArray should contain exactly one occurrence of the string “deep” on an array nested 3 levels deep
  • myNestedArray should contain exactly one occurrence of the string “deeper” on an array nested 4 levels deep
  • myNestedArray should contain exactly one occurrence of the string “deepest” on an array nested 5 levels deep
let myNestedArray = [
  // change code below this line
  ['unshift', false, 1, 2, 3, 'complex', 'nested'],
  ['loop', 'shift', 6, 7, 1000, 'method'],
  [
    [
      ['deep'],
      ['deeper'],
      [['deepest']]
    ]
  ]
  // change code above this line
];

lkas;dkfklasdf


#3

Hi @arduino731,

This is the solution:

let myNestedArray = [
  // change code below this line
  // top, or first level - the outer most array
  ['dee'], // an array within an array, 2 levels of depth
  [
    ['deep'], ['dee'] // 2 arrays nested 3 levels deep
  ],
  [
    [
      ['deeper'], ['deepe'] // 2 arrays nested 4 levels deep
    ],
    [
      [
        ['deepest'] // an array nested 5 levels deep
      ]
    ]
  ]
  // change code above this line
];

Just remove the code in challenge and replace my code into your challenge, it will worked probably.

Hope this will help you to pass this challenge, good luck and keep going up :muscle:

Regards,
Ali Mosaad


#4

@ali-admin Instead of just posting the solution, try to give the OP hints. If you do post a full working solution, please blur it out. This allows the OP to figure out more on his/her own before seeing a working solution. The OP will learn more by writing his/her own code instead of copying and pasting.

In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.


#5

Hi @randelldawson,

Thank you for your advice, i will work with your advice in the future.

Regards,
Ali Mosaad


#6

When I see a question like this, I usually try to explain why the code written does not work, which should hopefully point the OP in the correct direction. This accomplishes two things.

  1. It does not just give the answer.

  2. It improves my debugging skills and my ability to break down wrong solutions to others.


#7

Hi @randelldawson,

I think you are right, next help i will explain solutions and make a full depth teaching.

Thank you very much for your great advice again.

Regards,
Ali Mosaad


#8

can you explain your code?
The way i look at it deeper and deepest are at the same level?
I am struggling with this a little.

i tried the code below and it passed the test

let myNestedArray = [
  // change code below this line
  //this is level 1
  ['level2'],
  [['deep','level3']],
  [[['deeper','level4']]],
  [[[['deepest','level5']]]]
  // change code above this line
];
for (let i=0;i<myNestedArray.length;i++){
console.log(myNestedArray[0]+" = level 2");
console.log(myNestedArray[1]+" = level 3");
console.log(myNestedArray[2]+" = level 4");
console.log(myNestedArray[3]+" = level 5");
}

the console logs were just me trying to keep track of the levels.


#9

Fellow newbie, here.

Alright, so here’s an EASY way to understand what’s really going on.

in the original code, the entire set is actually only one array, within an array…

let myNestedArray = [

  // change code below this line
  ['unshift', false, 1, 2, 3, 'complex', 'nested'],
  ['loop', 'shift', 6, 7, 1000, 'method'],
  ['concat', false, true, 'spread', 'array'],
  ['mutate', 1327.98, 'splice', 'slice', 'push'],
  ['iterate', 1.3849, 7, '8.4876', 'arbitrary', 'depth']
  // change code above this line
]; 

For this exercise just imagine it’s the same as


let myNestedArray = [

  ["arrayOfStuff"], ["arrayOfStuff2"], ["arrayOfStuff3"], ["arrayOfStuff4"], ["arrayOfStuff5"]
]; 
//this is an array of arrays, 2 levels. myNestedArrays = [level1, otherStuff, [level2] ]

It’s a bit of a trick question with lots of useless info!

The goal in mind is to put certain strings at certain depths, right?

So, the easiest way to think of this would be…


meNestedArray = [ "level 1", [ "level 2", [ "level 3", ["level 4", ["level 5"]  ]   ]   ]    ]

//myNestedArray contains EVERYTHING! Level 1 contains everything inside it's own brackets. if you think of each listing in an array as an "item", then level 1 still only has 2 items in it. The string "Level 1, and the array that contains "level 2, along with all its contents, as one, single item.  

Tl;dr - Everything that is separated with a comma, is an “item” within a single array. To nest an array within an array, or add “levels”, you create a new set of brackets WITHIN the previous set. IE: [ [a, b, c],[x,y,z] ] is an array with 2 smaller arrays nested inside, however [ [a,b,c, [d,e,f]] ] is an array that has an array nested within it, consisting of the letters a,b,c, and an array, within that array, giving it an extra level of depth.


#11