Tell us what’s happening:
it says i dont have my html link to my javascript files linked correctly and that i dont have price and cid globaly declared but im sure i do
can anyone help me find out if i have a typo or sometthing or what i am doing nincorrectly
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width initial-scale=1.0'>
<link rel='stylesheet' href='styles.css' />
<script src="yourScript.js" defer></script>
<title>Cash Register</title>
</head>
<body>
<input id='cash' type='number' placeholder='Enter Cash Amount'>
<div id='change-due'></div>
<button id='purchase-btn'>sale</button>
</body>
</html>
/* file: script.js */
let price = 1.87;
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]
];
const cash = document.getElementById('cash');
const change = document.getElementById('change-due');
const sale = document.getElementById('purchase-btn');
let unitsOfCurrency = [
['PENNY', 0.01],
['NICKEL', 0.05],
['DIME', 0.10],
['QUARTER', 0.25],
['ONE', 1.00],
['FIVE', 5.00],
['TEN', 10.00],
['TWENTY', 20.00],
['ONE HUNDRED', 100.00]
];
sale.addEventListener('click', () => {
const cashValue = parseFloat(cash.value);
const changeDue = cashValue - price;
if (cashValue < price) {
alert('Customer does not have enough money to purchase the item');
} else if (cashValue === price) {
change.innerText = 'No change due - customer paid with exact cash';
} else {
const changeResult = getChange(changeDue, cid);
if(changeResult.status === 'INSUFFICIENT_FUNDS') {
change.innerText = 'Status: INSUFFICIENT_FUNDS';
} else if(changeResult.status === 'CLOSED') {
change.innerText = `Status: CLOSED ${changeResult.change.map(item => `${item[0]}: $${item[1].toFixed(2)}`).join(' ')
}`; } else {
change.innerText = `Status: OPEN ${changeResult.change.map(item => `${item[0]}: $${item[1].toFixed(2)}`).join(' ')}`; } }
});
const getChange = (changeDue, cid) => {
let cidTotal = cid.reduce((sum, [, amount]) => sum + amount, 0).toFixed(2); let changeArray = []; let remainingChange = changeDue; let temporaryCid = JSON.parse(JSON.stringify(cid));
for (let i = unitsOfCurrency.length - 1; i >= 0; i--) {
let [unit, unitValue] = unitsOfCurrency[i];
let [, unitInDrawer] = temporaryCid[i];
if (unitValue <= remainingChange && unitInDrawer > 0) {
let amountFromUnit = 0;
while (remainingChange >= unitValue && unitInDrawer >= unitValue) {
remainingChange = (remainingChange - unitValue).toFixed(2);
unitInDrawer = (unitInDrawer - unitValue).toFixed(2);
amountFromUnit += unitValue;
}
if (amountFromUnit > 0) {
changeArray.push([unit, amountFromUnit]);
temporaryCid[i][1] = unitInDrawer;
}
}
}
remainingChange = parseFloat(remainingChange);
if (remainingChange > 0) {
return { status: 'INSUFFICIENT_FUNDS', change: [] };
} else if (changeDue.toFixed(2) === cidTotal) {
return { status: 'CLOSED', change: cid };
} else {
return { status: 'OPEN', change: changeArray };
}
};
/* file: styles.css */
*,
*::before,
*::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:128.0) Gecko/20100101 Firefox/128.0
Challenge Information:
Build a Cash Register Project - Build a Cash Register