6.9 KiB
6.9 KiB
id, title, challengeType, videoUrl, localeTitle
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
587d7b7b367417b2b2512b16 | Create complex multi-dimensional arrays | 1 | 创建复杂的多维数组 |
Description
让nestedArray = [// top,或first level - 最外面的数组虽然这个例子看似令人费解,但在处理大量数据时,这种复杂程度并非闻所未闻,甚至不同寻常。但是,我们仍然可以使用括号表示法轻松访问此复杂数组的最深层次:
['deep'],//数组中的数组,2个深度级别
[
['更深'],['更深'] // 2个数组嵌套3层深
]
[
[
['deepest'],['最深'] // 2个数组嵌套了4个级别
]
[
[
['deepest-est?'] //一个数组嵌套5层深
]
]
]
]。
的console.log(nestedArray [2] [1] [0] [0] [0]);现在我们知道那条数据在哪里,如果需要,我们可以重置它:
//日志:最深?
nestedArray [2] [1] [0] [0] [0] =“更深”;
的console.log(nestedArray [2] [1] [0] [0] [0]);
//现在记录:更深入
Instructions
myNestedArray
,它等于一个数组。修改myNestedArray
,使用数据元素的字符串 , 数字和布尔值的任意组合,以便它具有五个深度级别(请记住,最外层的数组是级别1)。某处在第三级上,包括字符串'deep'
,在第四级,包括字符串'deeper'
,并且在第五层上,包括字符串'deepest'
。 Tests
tests:
- text: <code>myNestedArray</code>应仅包含数字,布尔值和字符串作为数据元素
testString: '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, "<code>myNestedArray</code> should contain only numbers, booleans, and strings as data elements");'
- text: <code>myNestedArray</code>应该有5个级别的深度
testString: '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, "<code>myNestedArray</code> should have exactly 5 levels of depth");'
- text: <code>myNestedArray</code>应该在嵌套3级深度的数组中恰好包含一个字符串<code>"deep"</code>
testString: '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, "<code>myNestedArray</code> should contain exactly one occurrence of the string <code>"deep"</code> on an array nested 3 levels deep");'
- text: <code>myNestedArray</code>应该在嵌套4级深度的数组中恰好包含一个字符串<code>"deeper"</code> deep”
testString: '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, "<code>myNestedArray</code> should contain exactly one occurrence of the string <code>"deeper"</code> on an array nested 4 levels deep");'
- text: <code>myNestedArray</code>应该在嵌套5级深度的数组中恰好包含一个字符串<code>"deepest"</code> <code>myNestedArray</code> <code>"deepest"</code>
testString: '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, "<code>myNestedArray</code> should contain exactly one occurrence of the string <code>"deepest"</code> on an array nested 5 levels deep");'
Challenge Seed
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
];
Solution
// solution required