MY #100 DAYS OF CODE CHALLENGE JOURNEY-DAY 8

in #coding6 years ago

8.png

Problem: Cash Register

Problem Definition: The objective of the the program is to accept three inputs which is the purchase price, the amount paid and the array of currency denominations with their amount i.e cash in drawer array. This was tricky at first but I got my way around it alas! The program will return user change in the form of an array.

Algorithm

  1. I created a function which takes three parameters namely purchase price(price), amount tendered(cash) and cid(cash in drawer array).

  2. I created a variable change which is the difference between the cash and the price.

  3. A variable called totalCid is created to find the total amount of the cash in the cid array

  4. A denominations array was declared consisting of various denominations in the expected outcomes.

  5. The totalCid value was compared against the denominations array.

  6. The program returns closed if the cid (cash in drawer) is equal to the change, returns insufficient funds if the caash in drawer is less than the change or gives the change value as an array of denominations if the cash in drawer can cater for the user change(cid is greater than change).

function checkCashRegister(price, cash, cid) {
var denominations = [
{
denomination: "ONE HUNDRED",
value: 100.00
}, {
denomination: "TWENTY",
value: 20.00
}, {
denomination: "TEN",
value: 10.00
}, {
denomination: "FIVE",
value: 5.00
}, {
denomination: "ONE",
value: 1.00
}, {
denomination: "QUARTER",
value: 0.25
}, {
denomination: "DIME",
value: 0.10
}, {
denomination: "NICKEL",
value: 0.05
}, {
denomination: "PENNY",
value: 0.01
}
];
var change = cash - price;
var totalCid = cid.reduce(function(accumulator, next) {

    return accumulator + next[1];
}, 0.0);
if (totalCid === change) {
    return "Closed";
}

cid = cid.reverse();


var result = denominations.reduce(function(accumulator, next, index) {
   
    var currentValue = 0.0;
    if (change >= next.value) {
        while (change >= next.value && cid[index][1] >= next.value) {
            currentValue += next.value;
            change -= next.value;
           
            change = Math.round(change * 100) / 100;
            cid[index][1] -= next.value;
        }
        accumulator.push([next.denomination, currentValue]);
        return accumulator;
    } else {
        return accumulator;
    }
   
}, []);

return result.length && change === 0
    ? result
    : "Insufficient Funds";

}
// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.10],
// ["QUARTER", 4.25],
// ["ONE", 90.00],
// ["FIVE", 55.00],
// ["TEN", 20.00],
// ["TWENTY", 60.00],
// ["ONE HUNDRED", 100.00]]

checkCashRegister(19.50, 20.00, [
[
"PENNY", 1.01
],
[
"NICKEL", 2.05
],
[
"DIME", 3.10
],
[
"QUARTER", 4.25
],
[
"ONE", 90.00
],
[
"FIVE", 55.00
],
[
"TEN", 20.00
],
[
"TWENTY", 60.00
],
["ONE HUNDRED", 100.00]
]);

Sort:  

A hard challange, good job!

Coin Marketplace

STEEM 0.22
TRX 0.20
JST 0.034
BTC 98713.95
ETH 3352.77
USDT 1.00
SBD 3.07