# javascript – 在功能上将整数分解为块

``````> breakupMillis(100000000)
Array [ 0, 40, 46, 3, 1 ]
``````

``````> breakup(100000000, [1000, 60, 60, 24])
Array [ 0, 40, 46, 3, 1 ]
``````

``````> breakup(1000, [8, 8, 8])
Array [ 0, 5, 7, 1 ]
``````

``````const breakup = (n, l) => l.map(p =>
{ const q = n % p; n = (n - q) / p; return q; }).concat(n);
``````

>地图.这感觉就像减少工作,虽然我不知道如何.
>重写变量n.我根本不喜欢使用var;使用秘密var会使情况变得更糟.

``````const quotrem = (n, d) => [n / d >> 0, n % d]

const breakup = (n, [x,...xs]) => {
if (x === undefined) {
return [n]
}
else {
let [q, r] = quotrem(n, x)
return [r, ...breakup(q, xs)]
}
}

console.log(breakup(1000, [8, 8, 8]))
// [ 0, 5, 7, 1 ]

console.log(breakup(100000000, [1000, 60, 60, 24]))
// [ 0, 40, 46, 3, 1 ]``````

``````const isEmpty = xs => xs.length === 0
const head = xs => xs[0]
const tail = xs => xs.slice(1)
const quotrem = (n, d) => [n / d >> 0, n % d]

const breakup = (n, xs) => {
if (isEmpty(xs)) {
return [n]
}
else {
let [q, r] = quotrem(n, head(xs))
return [r, ...breakup(q, tail(xs))]
}
}

console.log(breakup(1000, [8, 8, 8]))
// [ 0, 5, 7, 1 ]

console.log(breakup(100000000, [1000, 60, 60, 24]))
// [ 0, 40, 46, 3, 1 ]``````