Tell us what’s happening:
I cannot pass tests 11,12,13,18 and 19 even though I get the output the test is asking for. At the same time, my code passes through other similar tests which are similar to tests 11,12,13,18 and 19. As a result, I am stuck and cannot move forward. Please help.
Your code so far
let price = 19.5;
let cid = [
['PENNY', 1.01],
['NICKEL', 2.05],
['DIME', 3.1],
['QUARTER', 4.25],
['ONE', 90],
['FIVE', 55],
['TEN', 20],
['TWENTY',6 0],
['ONE HUNDRED', 100],
];
const displayScreen = document.getElementById("display-screen");
displayScreen.innerText = `$${price}`;
const cash = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");
const changeDueElement = document.getElementById("change-due");
const pennies = document.getElementById("pennies");
const nickel = document.getElementById("nickel");
const dime = document.getElementById("dime");
const quarter = document.getElementById("quarter");
const one = document.getElementById("one");
const five = document.getElementById("five");
const ten = document.getElementById("ten");
const twenty = document.getElementById("twenty");
const hundred = document.getElementById("hundred");
pennies.innerText = `Pennies: $${cid[0][1]}`;
nickel.innerText = `Nickels: $${cid[1][1]}`;
dime.innerText = `Dimes: $${cid[2][1]}`;
quarter.innerText = `Quarters: $${cid[3][1]}`;
one.innerText = `Ones: $${cid[4][1]}`;
five.innerText = `Fives: $${cid[5][1]}`;
ten.innerText = `Tens: $${cid[6][1]}`;
twenty.innerText = `Twentys: $${cid[7][1]}`;
hundred.innerText = `Hundreds: ${cid[8][1]}`;
let ref = [
['ONE HUNDRED', 10000, Math.round(cid[8][1] * 100)],
['TWENTY', 2000, Math.round(cid[7][1] * 100)],
['TEN', 1000, Math.round(cid[6][1] * 100)],
['FIVE', 500, Math.round(cid[5][1] * 100)],
['ONE', 100, Math.round(cid[4][1] * 100)],
['QUARTER', 25, Math.round(cid[3][1] * 100)],
['DIME', 10, Math.round(cid[2][1] * 100)],
['NICKEL', 5, Math.round(cid[1][1] * 100)],
['PENNY', 1, Math.round(cid[0][1] * 100)],
];
const changeDueCalculator = (num) => {
let sum = ref.reduce((accumulator,arr) => accumulator + arr[2], 0);
const countChangeUsed = {};
if (num < 0){
alert("Customer does not have enough money to purchase the item")
}
else if (num === 0){
changeDueElement.innerHTML = `<p>No change due - customer paid with exact cash</p>`;
changeDueElement.classList.remove("hidden");
}
else if (num > sum){
changeDueElement.innerHTML = `<p>STATUS: INSUFFICIENT_FUNDS</p>`;
changeDueElement.classList.remove("hidden");
}
else if (num === sum) {
ref.forEach((array) => {
while (num >= array[1] && array[2] > 0){
countChangeUsed[array[0]] = (countChangeUsed[array[0]] || 0) + array[1];
num = num - array[1];
array[2] = array[2] - array[1];
}
});
console.log(ref);
console.log(countChangeUsed);
const keys = Object.keys(countChangeUsed);
const values = Object.values(countChangeUsed);
console.log(keys);
console.log(values);
let output = "Status: CLOSED ";
for (let i = 0; i < keys.length; i++){
output += `${keys[i]}: $${values[i]/100} `
}
changeDueElement.innerHTML = `<p>${output}</p>`;
changeDueElement.classList.remove("hidden");
pennies.innerText = `Pennies: $${ref[8][2]/100}`;
nickel.innerText = `Nickels: $${ref[7][2]/100}`;
dime.innerText = `Dimes: $${ref[6][2]/100}`;
quarter.innerText = `Quarters: $${ref[5][2]/100}`;
one.innerText = `Ones: $${ref[4][2]/100}`;
five.innerText = `Fives: $${ref[3][2]/100}`;
ten.innerText = `Tens: $${ref[2][2]/100}`;
twenty.innerText = `Twentys: $${ref[1][2]/100}`;
hundred.innerText = `Hundreds: $${ref[0][2]/100}`;
}
else {
ref.forEach((array) => {
while (num >= array[1] && array[2] > 0){
countChangeUsed[array[0]] = (countChangeUsed[array[0]] || 0) + array[1];
num = num - array[1];
array[2] = array[2] - array[1];
}
});
console.log(num);
num = Math.round(Number(cash.value * 100)) - Math.round(price * 100);
console.log(num);
console.log(countChangeUsed);
const keys = Object.keys(countChangeUsed);
const values = Object.values(countChangeUsed);
console.log(keys);
console.log(values);
let sumOfObject = 0;
for(let j = 0; j < values.length; j++){
sumOfObject = sumOfObject + values[j];
}
console.log(sumOfObject);
if (sumOfObject === num) {
let output = "Status: OPEN ";
for (let i = 0; i < keys.length; i++){
output += `${keys[i]}: $${values[i]/100} `
}
changeDueElement.innerHTML = `<p>${output}</p>`;
changeDueElement.classList.remove("hidden");
pennies.innerText = `Pennies: $${ref[8][2]/100}`;
nickel.innerText = `Nickels: $${ref[7][2]/100}`;
dime.innerText = `Dimes: $${ref[6][2]/100}`;
quarter.innerText = `Quarters: $${ref[5][2]/100}`;
one.innerText = `Ones: $${ref[4][2]/100}`;
five.innerText = `Fives: $${ref[3][2]/100}`;
ten.innerText = `Tens: $${ref[2][2]/100}`;
twenty.innerText = `Twentys: $${ref[1][2]/100}`;
hundred.innerText = `Hundreds: $${ref[0][2]/100}`;
}
else {
changeDueElement.innerHTML = `<p>STATUS: INSUFFICIENT_FUNDS</p>`;
changeDueElement.classList.remove("hidden");
}
}
return ref;
}
purchaseBtn.addEventListener("click", () => {
let cashValue = cash.value;
changeDueCalculator(Math.round(Number(cashValue * 100)) - Math.round(price * 100));
})
cash.addEventListener("keydown", e => {
let cashValue = cash.value;
if (e.key === "Enter"){
changeDueCalculator(Math.round(Number(cashValue * 100)) - Math.round(price * 100));
}
})
<!-- file: index.html -->
/* file: script.js */
/* file: styles.css */
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Challenge Information:
Build a Cash Register Project - Build a Cash Register