日常一练——实现一个拼手气抢红包

177 阅读1分钟

1.题目描述

实现一个拼手气抢红包

提供了一个 RedPackage 的类,初始化时传入红包金额和个数,需要实现一个 openRedPackage 方法,每调一次都进行一次“抢红包”,并以 console.log 的形式输出抢到的红包金额。

2.举例

const redPackage = new RedPackage(100, 5);
redPackage.openRedPackage(); // 随机输出一个金额,例如:20
redPackage.openRedPackage(); // 随机输出一个金额,例如:15
redPackage.openRedPackage(); // 随机输出一个金额,例如:10
redPackage.openRedPackage(); // 随机输出一个金额,例如:5
redPackage.openRedPackage(); // 随机输出一个金额,例如:50
redPackage.openRedPackage(); // 输出: "红包已抢光"

3.code 实现

function getRandomByRange(min, max) {
  return Math.floor(Math.random() * (max - min + 1) + min);
}

class RedPackage {
  allMoney = 0; // 总金额(元)
  curMoneyCent = 0; // 剩余金额(分)
  count = 0; // 总个数
  minMoney = 1; //单个红包最小金额 1分

  constructor(allMoney, count) {
    if (!allMoney || !count || allMoney <= 0 || count <= 0) {
      throw new Error('allMoney or count is not valid');
    }
    if (this.minMoney * count > allMoney * 100) {
      throw new Error('allMoney is not enough');
    }
    this.allMoney = allMoney;
    this.curMoneyCent = allMoney * 100;
    this.count = count;
  }

  openRedPackage() {
    if (this.count <= 0) {
      console.log('红包已抢光');
      return 0;
    }
    // 随机生成当前红包的金额(分)
    let randomMoney = 0;
    if (this.count === 1) {
      // 只有一个红包
      randomMoney = this.curMoneyCent;
    } else {
      const min = this.minMoney;
      const max = this.curMoneyCent - min * (this.count - 1);
      randomMoney = getRandomByRange(min, max);
    }

    this.count--; // 减少红包个数
    this.curMoneyCent -= randomMoney; // 减去抢到的红包金额
    console.log(`抢到的红包金额: ${randomMoney / 100}`);
    return randomMoney / 100; // 返回抢到的红包金额
  }
}