Tell us what’s happening:
My code passes all the tests except for the last one, when I do the last test manually, it works and my code outputs what it is supposed to…So I do not see why I cant pass the last test, as by hand it seems to work.
Your code so far
let totalValue = 0;
for(let elem of cid) {
totalValue += elem[1] * 100;
}
function priceHandle() {
let userMoneyValue = parseFloat(userMoney.value);
let changeRemaining = userMoneyValue * 100 - price * 100;
if(changeRemaining > totalValue) {
changeAmount.innerHTML = 'Status: INSUFFICIENT_FUNDS';
} else if (userMoneyValue < price) {
alert('Customer does not have enough money to purchase the item');
} else if(userMoneyValue === price){
changeAmount.innerHTML = 'No change due - customer paid with exact cash';
} else {
changeAmount.innerHTML = 'Status: OPEN';
let result = [];
cid = cid.reverse();
let currencyUnit = {
"ONE HUNDRED": 10000,
"TWENTY": 2000,
"TEN": 1000,
"FIVE": 500,
"ONE": 100,
"QUARTER": 25,
"DIME": 10,
"NICKEL": 5,
"PENNY": 1,
}
for(let elem of cid) {
let moneyHolder = [elem[0], 0];
elem[1] = elem[1] * 100;
while(changeRemaining >= currencyUnit[elem[0]] && elem[1] > 0) {
changeRemaining -= currencyUnit[elem[0]];
elem[1] -= currencyUnit[elem[0]];
totalValue -= currencyUnit[elem[0]];
moneyHolder[1] += currencyUnit[elem[0]] / 100;
}
result.push(moneyHolder);
console.log('hello', currencyUnit[elem[0]]);
}
if(changeRemaining > 0) {
changeAmount.innerHTML = 'Status: INSUFFICIENT_FUNDS';
} else if (changeRemaining === totalValue) {
displayClosedStatus(result);
} else if (changeRemaining === 0) {
displayChangeStatus(result);
}
cid = cid.reverse();
totalValue / 100;
for(let elem of cid) {
elem[1] = elem[1] / 100;
}
displayCid();
console.log(cid);
console.log(totalValue);
}
}
function formatValue(value) {
const parsedValue = parseFloat(value);
if (parsedValue % 1 === parseInt(parsedValue, 10)) {
return parsedValue.toString();
} else {
const formatted = parsedValue.toFixed(2);
return formatted.replace(/(\.0+|(?<=\.\d)0+)$/, '');
}
}
function displayChangeStatus(result) {
const list = document.createElement('ul');
result.forEach(([name, value]) => {
if(value > 0) {
const listItem = document.createElement('li');
const newValue = formatValue(value);
listItem.textContent = `${name}: ${'$' + newValue}`;
list.appendChild(listItem);
}
});
changeAmount.appendChild(list);
}
function displayClosedStatus(result) {
const list = document.createElement('ul');
result.forEach(([name, value]) => {
if(value > 0) {
const listItem = document.createElement('li');
const newValue = formatValue(value);
listItem.textContent = `${name}: ${'$' + newValue}`;
list.appendChild(listItem);
}
});
changeAmount.innerHTML = 'Status: CLOSED';
changeAmount.appendChild(list);
}
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 Edg/122.0.0.0
Challenge Information:
Build a Cash Register Project - Build a Cash Register