I understand some people were coming across bugs with this project. I am failing these three:
- When
price
is less than the value in the#cash
element, total cash in drawercid
is greater than the change due, individual denomination amounts allows for returning change due, and the#purchase-btn
element is clicked, the value in the#change-due
element should be"Status: OPEN"
with required change due in coins and bills sorted in highest to lowest order.
- When
price
is19.5
, the value in the#cash
element is20
,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 PENNY: $0.5"
.Failed:19. When
price
is less than the value in the#cash
element, total cash in drawercid
is equal to change due, and the#purchase-btn
element is clicked, the value in the#change-due
element should be"Status: CLOSED"
with change due in coins and bills sorted in highest to lowest order.
When I manually input the test for 18, I get the same output:
Here is my Code:
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="styles.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="container-div">
<div class="price-div">
<p>Price: <span id="price"></span></p>
</div>
<div class="input-div">
<input id ="cash" type="number" />
<button id="purchase-btn">Calculate</button>
</div>
<div class="output-div">
<p id="change-due">Output</p> </div>
<div class="cid-div">
<p class="cid">Penny: <span class="cidSpan"></span></p>
<p class="cid">Nickel: <span class="cidSpan"></span></p>
<p class="cid">Dime: <span class="cidSpan"></span></p>
<p class="cid">Quarter: <span class="cidSpan"></span></p>
<p class="cid">One: <span class="cidSpan"></span></p>
<p class="cid">Five: <span class="cidSpan"></span></p>
<p class="cid">Ten: <span class="cidSpan"></span></p>
<p class="cid">Twenty: <span class="cidSpan"></span></p>
<p class="cid">Hundred: <span class="cidSpan"></span></p>
</div>
</div>
</body>
<script src="script.js"></script>
</html>
CSS
body{
background-color: rgb(68, 66, 66)
}
p, span{
text-align: center;
color: #B3B3B4;
}
.container-div{
}
.price-div p{
font-weight: bold;
font-size: 20px
}
.input-div{
display: flex;
flex-direction: column;
width:300px;
align-items: center;
margin: auto;
}
.cid-div p,.cid div span{
font-size: 20px;
margin: 5px
}
input{
width: 100%;
margin-bottom: 10px;
font-size: 18px;
text-align: center;
}
button{
font-size: 20px;
border-radius: 10px;
background-color:
}
.output-div{
height: 100px;
display:flex;
width: 300px;
text-align: center;
justify-content: center;
align-items: center;
margin: auto;
}
.output-div p{
text-align: center;
margin: auto;
font-weight: bold;
font-size: 20px
}
let price = 19.5;
let cid = [
['PENNY', 0.50],
['NICKEL', 0],
['DIME', 0],
['QUARTER', 0],
['ONE', 0],
['FIVE', 0],
['TEN', 0],
['TWENTY', 0],
['ONE HUNDRED', 0]
];
// 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 notes = {
PENNY: 0.01,
NICKEL: 0.05,
DIME: 0.10,
QUARTER: 0.25,
ONE: 1,
FIVE: 5,
TEN: 10,
TWENTY: 20,
'ONE HUNDRED': 100
};
let noteCounter = {
PENNY: 0,
NICKEL: 0,
DIME: 0,
QUARTER: 0,
ONE: 0,
FIVE: 0,
TEN: 0,
TWENTY: 0,
'ONE HUNDRED': 0
};
const cashInput= document.getElementById("cash");
const pricehtml = document.getElementById("price")
const changeOutput = document.getElementById("change-due")
const calcButton = document.getElementById("purchase-btn")
const cidOutSpan = document.querySelectorAll(".cidSpan")
pricehtml.textContent = price
// Loop which updates CID values in HTML
for(let i =0;i<=8; i++){
cidOutSpan[i].textContent = `$${cid[i][1]}`
}
// Loop which calculates number of notes and coins
function changeCalculator(cash){
if(price>cash){
alert("Customer does not have enough money to purchase the item")
return
}else if(price == cash){
return "No change due - customer paid with exact cash"
}
let change = cash-price
console.log(change)
for(let i = 8; i>=0; i--){
if(change >= notes[cid[i][0]] && cid[i][1]>=notes[cid[i][0]]){
for(let j=1; change >= notes[cid[i][0]] && cid[i][1] > 0; j++){
cid[i][1] = Math.round((cid[i][1] - notes[cid[i][0]])*100)/100
change = Math.round((change - notes[cid[i][0]])*100)/100
noteCounter[cid[i][0]] = j
}
}
if(i===0 && change > 0){
return "Status: INSUFFICIENT_FUNDS"
}
}
return noteCounter
}
// Converts number of notes to dollar value then string
function noteCountToString(noteCount){
let noteString = ""
for(let i = 8; i>=0; i--){
if(noteCount[cid[i][0]] > 0){
noteString += ` ${cid[i][0]}: $${noteCount[cid[i][0]]*notes[cid[i][0]]} `
console.log(noteString)
}
}
return noteString
}
// repeating above loop which updates CID values in HTML
function updateCID(){
for(let i =0;i<=8; i++){
cidOutSpan[i].textContent = `$${cid[i][1]}`
}
}
// changes status to closed if there is no cash in register
function status(){
let registerStatus = "OPEN"
if (cid.every((element)=>element[1]=== 0)){
registerStatus = "CLOSED"
}
return registerStatus
}
// main function
function main(){
let notesAndCoins = changeCalculator(cashInput.value)
updateCID()
if (notesAndCoins === "Status: INSUFFICIENT_FUNDS"){
changeOutput.textContent = notesAndCoins
return
}else if(notesAndCoins == "No change due - customer paid with exact cash"){
changeOutput.textContent = notesAndCoins
}else if (!notesAndCoins){
return
}else if(notesAndCoins)
changeOutput.textContent = `Status: ${status()} ${noteCountToString(notesAndCoins)}`
}
calcButton.addEventListener("click", main)