• Home
  • Question Cash Register Solution – JavaScript

Question Cash Register Solution – JavaScript

To solve a cash register problem in JavaScript, you can follow these steps:

  1. Define the variables you will need for the cash register program. These may include the amount due, the amount paid, and the amount of change to be given.
  2. Calculate the change due by subtracting the amount due from the amount paid.
  3. Determine the denominations of bills and coins that should be given as change, based on the amount of change due.
  4. Use a loop to iterate through the different denominations, starting with the highest value, and subtracting the value of each denomination from the change due until it is reduced to zero.
  5. Print out the results of the program, including the amount of each denomination to be given as change.

Here is an example of what the code might look like:

const amountDue = 25.00;
const amountPaid = 50.00;
let changeDue = amountPaid - amountDue;

const denominations = [100, 50, 20, 10, 5, 1, 0.25, 0.10, 0.05, 0.01];
const numDenominations = denominations.length;

let changeGiven = {};

for (let i = 0; i < numDenominations; i++) {
const denomination = denominations[i];
let numNotes = Math.floor(changeDue / denomination);
if (numNotes > 0) {
changeGiven[denomination] = numNotes;
changeDue -= numNotes * denomination;


This code will calculate the change due and determine the number of each denomination to be given as change. The results will be printed in an object, with the keys representing the denominations and the values representing the number of each denomination to be given.