daily pastebin goal
14%
SHARE
TWEET

Untitled

a guest Jan 23rd, 2019 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let changeMapping = [];
  2. changeMapping.push(createChangeObj("twentyDollar", 2000));
  3. changeMapping.push(createChangeObj("tenDollar", 1000));
  4. changeMapping.push(createChangeObj("fiveDollar", 500));
  5. changeMapping.push(createChangeObj("twoDollar", 200));
  6. changeMapping.push(createChangeObj("oneDollar", 100));
  7. changeMapping.push(createChangeObj("quarter", 25));
  8. changeMapping.push(createChangeObj("dime", 10));
  9. changeMapping.push(createChangeObj("nickel", 5));
  10. changeMapping.push(createChangeObj("penny", 1));
  11.  
  12. var createChangeObj = function(key, value) {
  13.   let changeObj = {};
  14.   changeObj.key = key;
  15.   changeObj.value = value;
  16.   return changeObj;
  17. };
  18.  
  19. var calculateChange = function(total, cash) {
  20.   if (cash <= total) {
  21.     // Cash given is less than total owed.
  22.     return {};
  23.   }
  24.  
  25.   let retChangeObj = {};
  26.   let difference = cash - total;
  27.   for (let changeObj of changeMapping) {
  28.     let val = changeObj.value;
  29.     // This denomination is greater than amount remaining
  30.     let numDenom = 0;
  31.     while (val <= difference) {
  32.       difference -= val;
  33.       numDenom++;
  34.     }
  35.  
  36.     if (numDenom > 0) {
  37.       retChangeObj[changeObj.key] = numDenom;
  38.     }
  39.   }
  40.   return retChangeObj;
  41. };
  42.  
  43. var runTask4 = function() {
  44.   console.log(calculateChange(1787, 2000));
  45.   console.log(calculateChange(2623, 4000));
  46.   console.log(calculateChange(501, 1000));
  47.   /*
  48.     { twoDollar: 1, dime: 1, penny: 3 }
  49.     { ten: 1, twoDollar: 1, dollar: 1, quarter: 3, penny: 2 }
  50.     { twoDollar: 2, quarter: 3, dime: 2, penny: 4 }
  51.     */
  52. };
  53.  
  54. runTask4();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top