Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

My Cash Register Code is outputting the right answers but not passing the tests. Please help

Your code so far


`
const cashTenderedInput = document.getElementById(“cash”);
const showResults = document.getElementById(“change-due”);
const purchaseBtn=document.getElementById(“purchase-btn”);

function initialize(){//Function to initialize variables cash,price and cid and call the checkCashRegister function with those variables as parameters

//initializing the variables
let cid =[
[“PENNY”, 1.01],
[“NICKEL”, 2.05],
[“DIME”, 3.1],
[“QUARTER”, 4.25],
[“ONE”, 90],
[“FIVE”, 55],
[“TEN”, 20],
[“TWENTY”, 60],
[“ONE HUNDRED”, 100]
];
let price = 19.50;
let cash = cashTenderedInput.value;

return checkCashRegister(price, cash, cid);//calling the checkCashRegister

}

function checkCashRegister(price, cash, cid) {

let cidSum = 0;//Variable to hold cid total
let result=“”;//String to hold output messages
var change = cash - price; // calculate the amount of change due
var change1 = cash - price;//Change variable to be used for CLOSED status

for (let i = 0; i < cid.length; i++) {//Calculating cid total

cidSum += cid[i][1];

}

if(cash < price){

return alert(“Customer does not have enough money to purchase the item”);

};

if(cash == price ){

return showResults.innerHTML = “No change due - customer paid with exact cash”;

};

var currency = [ // Array of denominations and their values
[“PENNY”, 0.01],
[“NICKEL”, 0.05],
[“DIME”, 0.1],
[“QUARTER”, 0.25],
[“ONE”, 1],
[“FIVE”, 5],
[“TEN”, 10],
[“TWENTY”, 20],
[“ONE HUNDRED”, 100]
];

var cidTotal = 0; // Variable to hold the total amount of cash in the cash drawer to be used to subtract change

// looping through each denomination, in reverse starting with the highest
for (var i = currency.length - 1; i >= 0; i–) {
var denominationName = currency[i][0]; // get the name of the denomination
var denominationValue = currency[i][1]; // get the value of the denomination
var denominationAvailable = cid[i][1]; // get the amount of cash available in the cash drawer for this denomination
var denominationCount = 0; //Variable to hold the number of this denomination to give as change

// while there is still change due, and there is still this denomination available, and the value of this denomination is less than or equal to the change due
while (change >= denominationValue && denominationAvailable > 0) {
  change -= denominationValue; // subtract the value of this denomination from the change due
  change = Math.round(change * 100) / 100; // round the change due to two decimal places
  denominationAvailable -= denominationValue; // subtract the value of this denomination from the amount available in the cash drawer
  denominationCount++; // increase the number of this denomination to give as change
  cidTotal += denominationValue; // increase the total amount of cash in the cash drawer
}

// adding this denomination as change to the change string
if (denominationCount > 0) {

//Create change string
result +=${denominationName}: $${denominationCount*denominationValue} ;
};

};

//if cash-in-drawer is equal to the change due
if ( cidSum == change1) {

return showResults.innerHTML=Status : CLOSED ${result};

}

// if cash-in-drawer is less than the change due, or if you cannot return the exact change.
if (change > 0 || cidTotal < change) {

return showResults.innerHTML=Status: INSUFFICIENT_FUNDS;

}
// if cash-in-drawer is greater than the change due and you can return change, with the change due in coins and bills sorted in highest to lowest order.
return showResults.textContent= Status : OPEN ${result};

}


`

<!-- file: index.html -->

/* file: styles.css */

/* file: script.js */

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0

Challenge Information:

Build a Cash Register Project - Build a Cash Register

cid and price have to be global variables

I had the same problem, until I moved cid and price variables outside of the function.

Thank you. Its passing now.

Thank you its passing now . l appreciate .