Tell us what’s happening:
The last test :
" 1. When price is 19.5, the value in the #cash element is 20, cid is [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]], and the #purchase-btn element is clicked, the value in the #change-due element should be Status: CLOSED QUARTER: $0 DIME: $0 NICKEL: $0 PENNY: $0.5."
Q1 : Why does the request only include “QUARTER” to “PENNY” rather than “ONE HUNDRED” to “PENNY” or only “PENNY” ? It is kind of illogical.
Q2: I can not pass the test. Even if I copied the text “Status: CLOSED QUARTER: $0 DIME: $0 NICKEL: $0 PENNY: $0.5” for test, I failed. I can not find out why it does not work.
Your code so far
https://codepen.io/TzuHsien-Fan/pen/jOJWyXG
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Challenge Information:
Build a Cash Register Project - Build a Cash Register
1 Like
sanity
January 4, 2024, 5:27am
2
There’s bug in the last test, for details see:
opened 08:40PM - 21 Dec 23 UTC
help wanted
scope: curriculum
type: showstopper
new javascript course
### Describe the Issue
It looks like the expected array is wrong in the last … test.
Also, I'm not sure if it should only account for what is the same. Just because the output _does_ contain the expected doesn't mean it can't contain _more_ than the expected.
Tests: https://github.com/freeCodeCamp/freeCodeCamp/blob/main/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/build-a-cash-register-project/build-a-cash-register.md
### Affected Page
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures-v8/build-a-cash-register-project/build-a-cash-register
### Your code
```
const expected = ['Status: OPEN', 'QUARTER: $0', 'DIME: $0', 'NICKEL: $0', 'PENNY: $0.5'];
let text = 'Status: OPEN ONE HUNDRED: $0 TWENTY: $0 TEN: $0 FIVE: $0 DOLLAR: $0 ONE: $0 QUARTER: $0 DIME: $0 NICKEL: $0 PENNY: $0.5'
let result = expected.every(str => text.trim().toLowerCase().includes(str.toLowerCase()))
console.log(result); // true
```
### Expected behavior
Test not to pass with the wrong code.
### Screenshots
_No response_
### System
- Device: [e.g. iPhone 6, Laptop]
- OS: [e.g. iOS 14, Windows 10, Ubuntu 20.04]
- Browser: [e.g. Chrome, Safari]
- Version: [e.g. 22]
### Additional context
Forum: https://forum.freecodecamp.org/t/cash-register-bug/657207
2 Likes
having similar issue, tried many things, what’s the solution?
We don’t just give solutions here, so don’t expect anyone to paste the exact solution in here for you. Instead, we try to help you figure it out for yourself.
If you are still having problems with this step then please use the Help button in the step to create a new post just for your issue.
Thank you, but there’s literally an open github issue on it, did I misread something?
Ahh, you’re right, I was thinking of something else and got it mixed up with this one. For this issue, there is no solution at the moment. You can’t pass the last test until the test itself is fixed.
It seems like this link’s script.js code is the solution, still studying why this works rather than some other code
---
id: 657bdcc3a322aae1eac38392
title: Build a Cash Register
challengeType: 14
forumTopicId: 16012
dashedName: build-a-cash-register
---
# --description--
Here you'll build a cash register app that will return change to the customer based on the price of the item, the amount of cash provided by the customer, and the amount of cash in the cash drawer. You'll also need to show different messages to the user in different scenarios, such as when the customer provides too little cash or when the cash drawer doesn't have enough to issue the correct change.
There are a few variables you'll need to use in your code:
- `price`: the price of the item as a floating point number.
- `cash`: the amount of cash provided by the customer for the item, which is provided via an `input` element on the page.
- `cid`: cash-in-drawer, a 2D array listing available currency in the cash drawer.
If you'd like to test your application with different values for `price` and `cid`, make sure to declare them with the `let` keyword so they can be reassigned by our tests.
This file has been truncated. show original
const purchaseButton = document.getElementById('purchase-btn');
const cashInput = document.getElementById('cash');
const changeDueDiv = document.getElementById('change-due');
let price = 19.5; // let allows fcc tests to reassign
let cid = [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]; // Example cash in drawer
const checkCashRegister = () => {
if (Number(cashInput.value) < price) {
alert('Customer does not have enough money to purchase the item');
cashInput.value = '';
return;
}
if (Number(cashInput.value) === price) {
changeDueDiv.innerHTML = 'No change due - customer paid with exact cash';
cashInput.value = '';
return;
}
let changeDue = Number(cashInput.value) - price;
let change = calculateChange(changeDue, cid);
if (change.status === "INSUFFICIENT_FUNDS") {
changeDueDiv.innerHTML = 'Status: INSUFFICIENT_FUNDS';
} else {
changeDueDiv.innerHTML = `Status: ${change.status} ${change.change.map(c => `${c[0]}: $${c[1].toFixed(2)}`).join(' ')}`;
}
cashInput.value = '';
}
purchaseButton.addEventListener('click', checkCashRegister);
const calculateChange = (changeDue, cid) => {
const currencyUnit = {
"ONE HUNDRED": 100,
"TWENTY": 20,
"TEN": 10,
"FIVE": 5,
"ONE": 1,
"QUARTER": 0.25,
"DIME": 0.1,
"NICKEL": 0.05,
"PENNY": 0.01
};
let totalCID = cid.reduce((sum, elem) => sum + elem[1], 0);
totalCID = parseFloat(totalCID.toFixed(2));
if (totalCID < changeDue) {
return { status: "INSUFFICIENT_FUNDS", change: [] };
}
let changeArr = [];
let reversedCid = [...cid].reverse();
for (let [denom, amount] of reversedCid) {
let value = currencyUnit[denom];
let amountOfDenom = 0;
while (changeDue >= value && amount > 0) {
amount -= value;
changeDue -= value;
changeDue = parseFloat(changeDue.toFixed(2));
amountOfDenom += value;
}
if (amountOfDenom > 0) {
changeArr.push([denom, amountOfDenom]);
}
}
if (changeDue > 0) {
return { status: "INSUFFICIENT_FUNDS", change: [] };
}
let changeTotal = changeArr.reduce((sum, elem) => sum + elem[1], 0);
if (changeTotal < totalCID) {
return { status: "OPEN", change: changeArr };
}
// Specific format for 'CLOSED' status
return {
status: "CLOSED",
change: "CLOSED " + Object.keys(currencyUnit)
.map(denom => {
let amount = changeArr.find(elem => elem[0] === denom)?.[1] || 0;
return `${denom}: $${amount.toFixed(2)}`;
}).join(' ')
};
};
Yea, I encounter this too and I can’t pass the test. It says it was fixed ( fix(challenge): fix the last test of the Cash Register Project by weilirs · Pull Request #52929 · freeCodeCamp/freeCodeCamp · GitHub )
and it has been already merged into :main ,but I still have bugged instruction on this test.
When changes on github code take effect on site ?
The fixed was just merged yesterday. As I understand it, there is no set timeline as to when it will actually be pushed to production. But I would be surprised if it took more than a week.
Well, it took over a week. It’s still not fixed.
It has been fixed already. The last test request change to Status: CLOSED PENNY: $0.5 .
system
Closed
July 31, 2024, 1:21pm
12
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.