UNPKG

js-red-package

Version:

用 js 实现一个类似微信红包的随机算法

71 lines (50 loc) 1.34 kB
"use strict"; /** * * @author xgqfrms * @license MIT * @copyright xgqfrms * @created 2020-09-0 * @modified * * @description * @difficulty Easy Medium Hard * @complexity O(n) * @augments * @example * @link * @solutions * */ // import {autoRandomRedPackage} from "js-red-package"; import {autoRandomRedPackage} from "./index.min.mjs"; const log = console.log; const total = arr => arr.result.reduce((acc, i) => acc += i*100, 0) / 100; // 测试 const test = autoRandomRedPackage(0.1, 11);// ❌ 异常处理 const test0 = autoRandomRedPackage(0.1, 5); const test1 = autoRandomRedPackage(0.1, 10); // ✅ ok const test2 = autoRandomRedPackage(1, 10); const test3 = autoRandomRedPackage(10, 10); const test4 = autoRandomRedPackage(100, 10); const test5 = autoRandomRedPackage(100, 11); log(`\ntest =`, test); log(`total =`, test && total(test)); log(`\ntest =`, test0); log(`total =`, test0 && total(test0)); log(`\ntest =`, test1); log(`total =`, test1 && total(test1)); log(`\ntest =`, test2); log(`total =`, test2 && total(test2)); log(`\ntest =`, test3); log(`total =`, test3 && total(test3)); log(`\ntest =`, test4); log(`total =`, test4 && total(test4)); log(`\ntest =`, test5); log(`total =`, test5 && total(test5)); /* # ES6 ESM --experimental-modules $ node --experimental-modules test.mjs */ /* */