freeCodeCamp/curriculum/challenges/espanol/02-javascript-algorithms-an.../basic-data-structures/create-complex-multi-dimens...

217 lines
7.2 KiB
Markdown
Raw Normal View History

---
id: 587d7b7b367417b2b2512b16
title: Crear arreglos complejos multidimensionales
challengeType: 1
forumTopicId: 301159
dashedName: create-complex-multi-dimensional-arrays
---
# --description--
¡Fantástico! ¡Acabas de aprender un montón sobre arreglos! Esta ha sido una visión general de alto nivel, y hay mucho más que aprender sobre el trabajo con arreglos, mucho de lo cual verás en secciones posteriores. Pero antes de pasar a ver los <dfn>Objetos</dfn>, vamos a echar un vistazo más, y ver cómo los arreglos pueden llegar a ser un poco más complejos de lo que hemos visto en los desafíos anteriores.
Una de las características más poderosas cuando se piensa en los arreglos como estructuras de datos, es que los arreglos pueden contener, o incluso estar completamente formados por otros arreglos. Hemos visto arreglos que contienen arreglos en desafíos anteriores, pero bastante simples. Sin embargo, los arreglos pueden contener una profundidad infinita de arreglos que pueden contener otros arreglos, cada uno con sus propios niveles arbitrarios de profundidad, y así sucesivamente. De esta manera, un arreglo puede convertirse rápidamente en una estructura de datos muy compleja, conocida como <dfn>multidimensional</dfn>, o arreglo anidado. Considera el siguiente ejemplo:
```js
let nestedArray = [ // top, or first level - the outer most array
['deep'], // an array within an array, 2 levels of depth
[
['deeper'], ['deeper'] // 2 arrays nested 3 levels deep
],
[
[
['deepest'], ['deepest'] // 2 arrays nested 4 levels deep
],
[
[
['deepest-est?'] // an array nested 5 levels deep
]
]
]
];
```
Aunque este ejemplo pueda parecer enrevesado, este nivel de complejidad no es inaudito, ni siquiera inusual, cuando se trata de grandes cantidades de datos. Sin embargo, podemos acceder muy fácilmente a los niveles más profundos de un arreglo tan complejo con la notación de corchetes:
```js
console.log(nestedArray[2][1][0][0][0]);
// logs: deepest-est?
```
Y ahora que sabemos dónde está ese dato, podemos restablecerlo si lo necesitamos:
```js
nestedArray[2][1][0][0][0] = 'deeper still';
console.log(nestedArray[2][1][0][0][0]);
// now logs: deeper still
```
# --instructions--
Hemos definido una variable, `myNestedArray`, como un arreglo. Modifica `myNestedArray`, utilizando cualquier combinación de <dfn>cadenas</dfn>, <dfn>números</dfn> y <dfn>booleanos</dfn> para los elementos de datos, de modo que tenga exactamente cinco niveles de profundidad (recuerda que el arreglo más externo es el nivel 1). En algún lugar del tercer nivel, incluye la cadena `'deep'`, en el cuarto nivel, incluye la cadena `'deeper'`, y en el quinto nivel, incluye la cadena `'deepest'`.
# --hints--
`myNestedArray` debe contener sólo números, booleanos y cadenas como elementos de datos
```js
assert.strictEqual(
(function (arr) {
let flattened = (function flatten(arr) {
const flat = [].concat(...arr);
return flat.some(Array.isArray) ? flatten(flat) : flat;
})(arr);
for (let i = 0; i < flattened.length; i++) {
if (
typeof flattened[i] !== 'number' &&
typeof flattened[i] !== 'string' &&
typeof flattened[i] !== 'boolean'
) {
return false;
}
}
return true;
})(myNestedArray),
true
);
```
`myNestedArray` debe tener exactamente 5 niveles de profundidad
```js
assert.strictEqual(
(function (arr) {
let depth = 0;
function arrayDepth(array, i, d) {
if (Array.isArray(array[i])) {
arrayDepth(array[i], 0, d + 1);
} else {
depth = d > depth ? d : depth;
}
if (i < array.length) {
arrayDepth(array, i + 1, d);
}
}
arrayDepth(arr, 0, 0);
return depth;
})(myNestedArray),
4
);
```
`myNestedArray` debe contener exactamente una aparición de la cadena `"deep"` en un arreglo anidado a 3 niveles de profundidad
```js
assert(
(function howDeep(array, target, depth = 0) {
return array.reduce((combined, current) => {
if (Array.isArray(current)) {
return combined.concat(howDeep(current, target, depth + 1));
} else if (current === target) {
return combined.concat(depth);
} else {
return combined;
}
}, []);
})(myNestedArray, 'deep').length === 1 &&
(function howDeep(array, target, depth = 0) {
return array.reduce((combined, current) => {
if (Array.isArray(current)) {
return combined.concat(howDeep(current, target, depth + 1));
} else if (current === target) {
return combined.concat(depth);
} else {
return combined;
}
}, []);
})(myNestedArray, 'deep')[0] === 2
);
```
`myNestedArray` debe contener exactamente una aparición de la cadena `"deeper"` en un arreglo anidado a 4 niveles de profundidad
```js
assert(
(function howDeep(array, target, depth = 0) {
return array.reduce((combined, current) => {
if (Array.isArray(current)) {
return combined.concat(howDeep(current, target, depth + 1));
} else if (current === target) {
return combined.concat(depth);
} else {
return combined;
}
}, []);
})(myNestedArray, 'deeper').length === 1 &&
(function howDeep(array, target, depth = 0) {
return array.reduce((combined, current) => {
if (Array.isArray(current)) {
return combined.concat(howDeep(current, target, depth + 1));
} else if (current === target) {
return combined.concat(depth);
} else {
return combined;
}
}, []);
})(myNestedArray, 'deeper')[0] === 3
);
```
`myNestedArray` debe contener exactamente una aparición de la cadena `"deepest"` en un arreglo anidado a 5 niveles de profundidad
```js
assert(
(function howDeep(array, target, depth = 0) {
return array.reduce((combined, current) => {
if (Array.isArray(current)) {
return combined.concat(howDeep(current, target, depth + 1));
} else if (current === target) {
return combined.concat(depth);
} else {
return combined;
}
}, []);
})(myNestedArray, 'deepest').length === 1 &&
(function howDeep(array, target, depth = 0) {
return array.reduce((combined, current) => {
if (Array.isArray(current)) {
return combined.concat(howDeep(current, target, depth + 1));
} else if (current === target) {
return combined.concat(depth);
} else {
return combined;
}
}, []);
})(myNestedArray, 'deepest')[0] === 4
);
```
# --seed--
## --seed-contents--
```js
let myNestedArray = [
// Only 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']
// Only change code above this line
];
```
# --solutions--
```js
let myNestedArray = [
['unshift', ['deep', ['deeper', ['deepest']]],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']
];
```