Tell us what’s happening:
Hi everyone before I start please be patient with me, I am new here.
For some reason I cannot pass the test (in the last condition) despite the fact that if I set manually manually the Array and input as the condition I get the result is correct.
Am I missing something?
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Cash Register Project</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<input id="cash" type="number"></input>
<div id="change-due"></div>
<button id="purchase-btn">Purchase</button>
<div id="cash-in-drawer"></div>
<script src="./script.js"></script>
</body>
</html>
/* file: styles.css */
/* file: script.js */
let price = 19.5;
let cid = [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];
const cValues = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
let changeAmount = 0;
let changeString = "";
let changeInDrawerTotal = 0;
const purchaseButton = document.getElementById('purchase-btn');
const cashInput = document.getElementById('cash');
const changeDiv = document.getElementById('change-due');
const cidDiv = document.getElementById(`cash-in-drawer`);
let cidTotal = 0;
const getStatus = () => {
changeAmount = cashInput.value - price;
for (let i = 0; i < cid.length - 1; i++){
changeInDrawerTotal += cid[i][1];
}
if(price > cashInput.value){
alert("Customer does not have enough money to purchase the item");
}else if(changeAmount === 0){
changeDiv.innerHTML = `<p>No change due - customer paid with exact cash</p>`
}else if(changeAmount > changeInDrawerTotal){
changeDiv.innerHTML = `<p>Status: INSUFFICIENT_FUNDS</p>`
}else if (changeAmount === changeInDrawerTotal){
changeDiv.innerHTML = `<p>Status: CLOSED</p>`
getChange(changeAmount);
changeDiv.innerHTML += `${changeString}`;
}else{
changeDiv.innerHTML = `<p>Status: OPEN</p>`
getChange(changeAmount);
changeDiv.innerHTML += `${changeString}`;
}
}
const getChange = (num) => {
let currentVal = 0;
let originalNum = Number(num.toFixed(2));
let totalVal = 0;
for(let i = cid.length - 1; i >= 0; i--){
let currencyValue = Number(cValues[i].toFixed(2));
while(num >= currencyValue && cid[i][1] > 0){
num -= currencyValue;
num = Number(num.toFixed(2));
cid[i][1] -= currencyValue;
currentVal += currencyValue;
currentVal = Number(currentVal.toFixed(2));
totalVal += currencyValue;
totalVal = Number(totalVal.toFixed(2));
}
if (currentVal > 0){
changeString += cid[i][0] + ": $" + currentVal.toString();
}
currentVal = 0;
}
if (totalVal != originalNum){
changeDiv.innerHTML = `<p>Status: INSUFFICIENT_FUNDS</p>`
changeString = "";
}
}
purchaseButton.addEventListener("click", getStatus);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Challenge Information:
Build a Cash Register Project - Build a Cash Register