zoukankan      html  css  js  c++  java
  • FCC JavaScript Algorithms and Data Structures Projects ( 5 ) :Cash Register

    Design a cash register drawer function checkCashRegister() that accepts purchase price as the first argument (price), payment as the second argument (cash), and cash-in-drawer (cid) as the third argument.

    cid is a 2D array listing available currency.

    The checkCashRegister() function should always return an object with a status key and a change key.

    Return {status: "INSUFFICIENT_FUNDS", change: []} if cash-in-drawer is less than the change due, or if you cannot return the exact change.

    Return {status: "CLOSED", change: [...]} with cash-in-drawer as the value for the key change if it is equal to the change due.

    Otherwise, return {status: "OPEN", change: [...]}, with the change due in coins and bills, sorted in highest to lowest order, as the value of the change key.

    Currency Unit Amount
    Penny $0.01 (PENNY)
    Nickel $0.05 (NICKEL)
    Dime $0.1 (DIME)
    Quarter $0.25 (QUARTER)
    Dollar $1 (ONE)
    Five Dollars $5 (FIVE)
    Ten Dollars $10 (TEN)
    Twenty Dollars $20 (TWENTY)
    One-hundred Dollars $100 (ONE HUNDRED)

    Test Case:

    checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]) should return an object.

    checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]) should return {status: "OPEN", change: [["QUARTER", 0.5]]}.

    checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]) should return {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}.

    checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) should return {status: "INSUFFICIENT_FUNDS", change: []}.

    checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) should return {status: "INSUFFICIENT_FUNDS", change: []}.

    checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) should return {status: "CLOSED", change: [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]}.

    解决方案:

    function checkCashRegister(price, cash, cid) {
        //需要返回的对象
        var output = { status: null, change: [] };
        //钱币面值,单位为美分
        var denom = [
            ["ONE HUNDRED", 10000],
            ["TWENTY", 2000],
            ["TEN", 1000],
            ["FIVE", 500],
            ["ONE", 100],
            ["QUARTER", 25],
            ["DIME", 10],
            ["NICKEL", 5],
            ["PENNY", 1]
        ];
        //收银箱中的总额
        var register = cid.reduce((acc, curr) => {
            acc[curr[0]] = Math.round(curr[1] * 100);
            acc.total += acc[curr[0]];
            return acc;
        }, { total: 0 });
        //找零,单位美分
        var change = Math.round((cash - price) * 100);
        // 当找零刚好等于钱柜中的总额
        if (change == register.total) {
            output.status = 'CLOSED';
            output.change = cid;
            return output;
        }
        //当找零大于钱柜中的总额显示金额不足
        if (change > register.total) {
            output.status = 'INSUFFICIENT_FUNDS';
            return output;
        }
        //一般情况,收银箱中的总额大于找零,但不一定就能找零,如果全是大面额的,就无法找零,只能返回金额不足
        var arr = [];
        for (let item of denom) {
            var value = 0;
            while (register[item[0]] > 0 && change >= item[1]) {
                change -= item[1];
                register[item[0]] -= item[1];
                value += item[1];
            }
            if (value > 0) {
                arr.push([item[0], value / 100]);
            }
        }
        //虽然柜子里面的总额超过找零,但是没有小额钱币了,所以显示金额不足
        if (change > 0) {
            output.status = 'INSUFFICIENT_FUNDS';
            return output;
        }
        output.status = 'OPEN';
        output.change = arr;
        return output;
    }
    
  • 相关阅读:
    开源资源大汇总(C#)
    javascript必知必会之this关键字及scope(转)
    MSMQ 消息队列 安装MSMQ 创建发送接收队例消息
    精确获取页面元素的位置(转)
    javascript无缝滚动(转)
    C#多线程
    javascript十个最常用的自定义函数(转)
    ASP.NET——ASP.NET 数据库缓存依赖
    jQuery三维展示插件(转)
    Action和Func的区别
  • 原文地址:https://www.cnblogs.com/arduka/p/13321781.html
Copyright © 2011-2022 走看看