Tell us what’s happening:
If I call the functions one by one every test passed, but when trying to call all of them so I can pass the tests some of them fail.
How can I pass the tests? What is the intended way of doing this?
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input id="cash"/>
<span id="change-due"></span>
<button id="purchase-btn">Submit</button>
<script src="./script.js"></script>
</body>
</html>
/* file: script.js */
let price;
let cid;
const moneyValues = [
["ONE HUNDRED", 100],
["TWENTY", 20.00],
["TEN", 10],
["FIVE", 5],
["ONE", 1],
["QUARTER", 0.25],
["DIME", 0.1],
["NICKEL", 0.05],
["PENNY", 0.01]
];
const cash = document.getElementById("cash")
const button = document.getElementById("purchase-btn");
const change = document.getElementById("change-due");
const moneyValuesObject = convertArrayToObject(moneyValues);
function sortArray(array){
return array.sort(function(a, b) {
return b[1] - a[1];
});
}
function convertArrayToObject(array){
return sortArray(array).reduce((acc, [name, value]) => {
acc[name] = value;
return acc;
}, {});
}
function calculateCidTotal(cidObject){
let total = 0;
Object.entries(cidObject).forEach((element) => {
total += element[1];
})
return Number(total).toFixed(2);
}
function displayMessage(array, status){
let message = `Status: ${status}`
if(status === "INSUFFICIENT_FUNDS"){
return message;
}
array.forEach(element => {
message += ` ${element[0]}: $${moneyValuesObject[element[0]] * element[1]}`
})
return message;
}
function finalCheck(price, cash, cid){
const cidObject = convertArrayToObject(cid);
const sortedCid = sortArray(moneyValues);
let changeArray = [];
if(cash < price) alert("Customer does not have enough money to purchase the item")
if(cash == price) {
change.innerHTML = "No change due - customer paid with exact cash"
return
}
let moneyAfterPayment = Number((cash - price).toFixed(2))
let status = "OPEN";
sortedCid.forEach((element) => {
let billCount = 0;
const changeValue = moneyValuesObject[element[0]];
if(moneyAfterPayment >= changeValue && cidObject[element[0]] >= changeValue){
while (moneyAfterPayment >= changeValue && cidObject[element[0]] >= changeValue) {
moneyAfterPayment = Number((moneyAfterPayment - changeValue).toFixed(2));
cidObject[element[0]] = Number(cidObject[element[0]] - changeValue).toFixed(2);
billCount++
}
changeArray.push([element[0], billCount])
}
})
const cidTotal = calculateCidTotal(cidObject);
if(moneyAfterPayment > 0 || cidTotal < 0){
status = "INSUFFICIENT_FUNDS"
}else if(cidTotal == 0.00){
status = "CLOSED"
}
let message = displayMessage(changeArray, status)
change.innerText = message;
change.value = message;
}
button.addEventListener("click", () => {
const cashValue = Number(cash.value);
finalCheck(20, cashValue, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
finalCheck(100, cashValue, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
finalCheck(11.95, cashValue, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
finalCheck(25, 25, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
finalCheck(19.5, cashValue, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);
// finalCheck(19.5, cashValue, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);
})
/* 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/126.0.0.0 Safari/537.36
Challenge Information:
Build a Cash Register Project - Build a Cash Register