softnshare / kata

Code Kata 這個概念是由 The Pragmatic Programmer 的作者之一Dave Thomas提出的, 想要提升自己的coding skill嗎? 歡迎加入這個slack channel, 加入請參考右邊網頁說明
https://softnshare.wordpress.com/slack/kata/
38 stars 4 forks source link

xD-Arrays for dimmies #8

Open jawayang opened 8 years ago

jawayang commented 8 years ago

題目: 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..

dim( d1 [,d2 [,d3 [... ]]], value )

... 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-陣列,並用默認值填充它。

所以..

dim( d1 [,d2 [,d3 [... ]]], value )

...將創建 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

bucker commented 8 years ago

Pass

jawayang commented 8 years ago

有點難度,有請大師開釋

larry0220 commented 8 years ago

我的解法,請參考

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 }

jawayang commented 8 years ago

真聰明的解法

vampireneo commented 8 years ago

Pass