Skip to content

Latest commit

 

History

History
90 lines (72 loc) · 3.9 KB

File metadata and controls

90 lines (72 loc) · 3.9 KB

Problem Explanation:

  • You have to create a program that will handle when the register does not have enough cash or will have no cash after the transaction. Other than that it needs to return an array of the change in the form of an array, so that will be a 2D array.

Hint: 1

  • Is easier to handle if you will have to close the register or if you will not have enough money to complete the transaction if you know beforehand how much money is on your register. For this it would be recommended to have a function get the information assigned to a variable.

Hint: 2

  • Life is easier when you get to know the value of each currency type in the register instead of how much money is composed of that particular currency. So be sure to watch out for that.

Hint: 3

  • You will have to get as much change from one type before moving to the next from greater value to lesser, and keep going until you have covered the whole change.

Spoiler Alert!

687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif

Solution ahead!

Code Solution:

// Create an object which hold the denominations and their values
var denom = [
	{ name: 'ONE HUNDRED', val: 100.00},
	{ name: 'TWENTY', val: 20.00},
	{ name: 'TEN', val: 10.00},
	{ name: 'FIVE', val: 5.00},
	{ name: 'ONE', val: 1.00},
	{ name: 'QUARTER', val: 0.25},
	{ name: 'DIME', val: 0.10},
	{ name: 'NICKEL', val: 0.05},
	{ name: 'PENNY', val: 0.01}
];

function drawer(price, cash, cid) {
    var change = cash - price;

    // Transform CID array into drawer object
    var register = cid.reduce(function(acc, curr) {
        acc.total += curr[1];
        acc[curr[0]] = curr[1];
        return acc;
    }, {total: 0});
    
    // Handle exact change
    if(register.total === change) {
        return 'Closed';
    }
    
    // Handle obvious insufficent funds
    if(register.total < change) {
        return 'Insufficient Funds';
    }
    
    // Loop through the denomination array
    var change_arr = denom.reduce(function(acc, curr) {
        var value = 0;
        // While there is still money of this type in the drawer
        // And while the denomination is larger than the change reminaing
        while(register[curr.name] > 0 && change >= curr.val) {
            change -= curr.val;
            register[curr.name] -= curr.val;
            value += curr.val;
            
            // Round change to the nearest hundreth to deal with precision errors
            change = Math.round(change * 100) / 100;
        }
        // Add this denomination to the output only if any was used.
        if(value > 0) {
            acc.push([ curr.name, value ]);
        }
        return acc; // Return the current Change Array
    }, []); // Initial value of empty array for reduce
  
  // If there are no elements in change_arr or we have leftover change, return "Insufficient Funds"
  if(change_arr.length < 1 || change > 0) {
    return "Insufficient Funds";
  }
    
  // Here is your change, ma'am.
  return change_arr;
}

Code Explanation:

  • Read comments in code.

Credits:

If you found this page useful, you can give thanks by copying and pasting this on the main chat: thanks @Rafase282 @SaintPeter for your help with Algorithm: Exact Change

NOTE: Please add your username only if you have added any relevant main contents to the wiki page. (Please don't remove any existing usernames.)