Open jawayang opened 8 years ago
Pass
有點難度,有請大師開釋
我的解法,請參考
function dim(...args) { if (args.length === 1) { let value = args[0]; return (typeof value === 'function' ? value() : value); }
let l = args.shift(); let result = [] for (let i = 0; i < l; i++) { result.push(dim(...args)); } return result }
真聰明的解法
Pass
題目: Sometimes we need to use multiDIMensional arrays (array of arrays). The goal of this kata is to code a dim function which will create an xD-array and fill it with a default value.
So..
... will create an array of d1 subarrays of d2 sub-subarrays of d3 sub-sub-subarrays (and so on), with each final item being equal to value. You may assume: ● at least 2 arguments (d1and value) are provided ● each d argument will be a positive integer ● value argument may be a number, a string, a boolean, ... If value is a function then the result of this function will be attribued to the item.
http://www.codewars.com/kata/5402724fd39b43c075000116/train/javascript
翻譯: 有時候,我們需要使用多維數組(數組的數組)。 目標是編寫一個 dim 函數,這將建構xD-陣列,並用默認值填充它。
所以..
...將創建 an array of d1 subarrays of d2 sub-subarrays of d3 sub-sub-subarrays (and so on),與每個最終項目等於value。
你可以假設: ● 至少有2個參數設置(d1 and value) ● 每個參數 d 將是一個正整數 ● 參數 value 可以是一個數字,字符串,布爾值,... 如果 value 是一個函數,則該函數的結果將被attribued到的項目。
翻譯 by Larry Huang