JavaScript || How To Make Change – Display The Total Sales Amount In Dollars & Cents Using Modulus
The following is a module with functions which demonstrates the use of the modulus (%) operator to make change.
So for example, given the value of 2.34, this function would return the result of 2 dollars, 1 quarters, 1 nickels, and 4 pennies.
1. Make Change
The example below demonstrates how to make change.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
// Make change <script> (() => { let values = [ 19.87, 11.93, 187.91, 3, 20.19, 1991 ]; values.forEach(value => { // Make change let change = Utils.makeChange(value); console.log(`\n$${value} is made up of: `); for (let amount of change) { console.log(`\t${amount.denomination.description}: x${amount.quantity}`); } }); })(); </script> // expected output: /* $19.87 is made up of: Ten-Dollar Bill: x1 Five-Dollar Bill: x1 One-Dollar Bill: x4 Quarter: x3 Dime: x1 Penny: x2 $11.93 is made up of: Ten-Dollar Bill: x1 One-Dollar Bill: x1 Quarter: x3 Dime: x1 Nickel: x1 Penny: x3 $187.91 is made up of: Hundred-Dollar Bill: x1 Fifty-Dollar Bill: x1 Twenty-Dollar Bill: x1 Ten-Dollar Bill: x1 Five-Dollar Bill: x1 One-Dollar Bill: x2 Quarter: x3 Dime: x1 Nickel: x1 Penny: x1 $3 is made up of: One-Dollar Bill: x3 $20.19 is made up of: Twenty-Dollar Bill: x1 Dime: x1 Nickel: x1 Penny: x4 $1991 is made up of: Hundred-Dollar Bill: x19 Fifty-Dollar Bill: x1 Twenty-Dollar Bill: x2 One-Dollar Bill: x1 */ |
2. Utils Namespace
The following is the Utils.js Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
// ============================================================================ // Author: Kenneth Perkins // Date: Aug 29, 2020 // Taken From: http://programmingnotes.org/ // File: Utils.js // Description: Javascript that handles general utility functions // ============================================================================ /** * NAMESPACE: Utils * USE: Handles general utility functions. */ var Utils = Utils || {}; (function(namespace) { 'use strict'; // Property to hold public variables and functions let exposed = namespace; /** * FUNCTION: makeChange * USE: Makes change out of a given dollar amount. * @param amount: The dollar amount of the change to make. * @return: An array of Denomination info that makes up the given amount. */ exposed.makeChange = (amount) => { let denominations = exposed.Denomination.get(); let remainingCents = exposed.Denomination.convert(amount); let change = []; for (const denomination of denominations) { let quantity = Math.floor(remainingCents / denomination.cents()); remainingCents %= denomination.cents(); if (quantity > 0) { change.push({ quantity: quantity, denomination: denomination, }); } } return change } exposed.Denomination = class { constructor(dollars, description) { this.dollars = dollars; this.description = description; } cents() { return exposed.Denomination.convert(this.dollars); } static convert(dollars) { return dollars * 100; } static get() { let denominations = [ new exposed.Denomination(100, 'Hundred-Dollar Bill'), new exposed.Denomination(50, 'Fifty-Dollar Bill'), new exposed.Denomination(20, 'Twenty-Dollar Bill'), new exposed.Denomination(10, 'Ten-Dollar Bill'), new exposed.Denomination(5, 'Five-Dollar Bill'), new exposed.Denomination(1, 'One-Dollar Bill'), new exposed.Denomination(.25, 'Quarter'), new exposed.Denomination(.10, 'Dime'), new exposed.Denomination(.05, 'Nickel'), new exposed.Denomination(.01, 'Penny'), ]; return denominations; } } (function (factory) { if (typeof define === 'function' && define.amd) { define([], factory); } else if (typeof exports === 'object') { module.exports = factory(); } }(function() { return namespace; })); }(Utils)); // http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Leave a Reply