Tell us what’s happening:
I keep getting a no pass even after I have entered the cash and price inputs and getting the requested outputs. I checked all my outputs in playcode and VS code. My html is poo poo but i’m trying to get the javascript before i get the rest. I’m not getting any errors in the console so I’m not sure where else to go form here.
I did copy and paste the code provided from the example into the tutorial and it didnt even pass all the requirements.
Your code so far
<!-- file: index.html -->
/* file: script.js */
const cash = document.getElementById("cash")
const changeDue = document.getElementById("change-due");
const purchaseBtn = document.getElementById("purchase-btn");
const priceOfItem = document.getElementById("price");
let price = 3.26;
//Cash in Drawer
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]
];
let denomValues = [
['PENNY', .01],
['NICKEL', .05],
['DIME', 0.1],
['QUARTER', .25],
['ONE', 1],
['FIVE', 5],
['TEN', 10],
['TWENTY', 20],
['ONE-HUNDRED', 100]
];
//Listen for Purchase Click Event Listener//
purchaseBtn.addEventListener("click", () => {
const cashRecieved = cash.value
const change =( Math.round(cashRecieved * 100) - Math.round(price*100))/100;
console.log("YOU CLICKED THE PURCHASE BUTTON");
//CALL FUNCTION to Determine if Calculation is Necessary
processTransaction(cashRecieved, change);
});///End of Event Listener Button Function
//FUNCTION to Determine if Valid Tranasaction can Occur
const processTransaction = (cashRecieved, change) => {
//If Cash Recieved is Less than Price
if(cashRecieved < price) {
alert("Customer does not have enough money to purchase the item");
resetForNextTransaction();
return;
}
console.log(cashRecieved - price)
if(cashRecieved - price === 0) {
cashRegisterDisplay()
resetForNextTransaction();
return;
}
//If Cash Recieved is Greater than Price
console.log("Cash Recieved is Greater than Price");
if(drawerTotal(cid)===0) {
insufficientFundsDisplay();
return;
}
//If Cash Recieved is Greater than Price
//Call Function to Reformat Variables
const [cidDenomMult, changeMult] = reformatVariables(cid, denomValues, change);
//Call Function to Calculate Change
const [modOfChange, denomAmts, currDrawer] = drawerCalc(cidDenomMult, changeMult);
console.log(currDrawer);
//BOOKMARK///////////////////////////
//Check if Change can be Made With Denominations in Drawer
cashRegisterDisplay(modOfChange, denomAmts, currDrawer);
};
//////////////End of nonCalcTransaction Function
//FUNCTION reformatVariables to Reformat cid Array for Easier Appication of Functions
const reformatVariables = (cid, denomValues, change) => {
//Create Array from cid that Includes Unit Values;
let addDenomValues = cid.map((item) => {
const [denomination, totalAmount] = item;
const update = denomValues.find(([updateDenom]) => updateDenom === denomination);
if (update) {
const [,unitValue] = update; // Destructure the unit value from denomValues
return [denomination, Math.round(totalAmount*100), Math.round(unitValue*100)]; // Add the new value
}
}).reverse()//Put array In descending Order
//Multiply Change by 100 for Easier Function Applications
let tempChange = change*100;
return [addDenomValues, tempChange];
};
//End of reformatCid Function
//// FUNCTION calcChange to Calculate Bills and Coins to Withdraw for Change
const drawerCalc = (tempCidDenomValues, tempChange) => {
let amountDenom = 0;
let denomAmtsArr = [];
let workingChange = tempChange
let counter = 0;
//Determine If you have the Denoms To Make Change Tender Needed to make Change
const requiredDrawer = tempCidDenomValues.map(([denomination, totalAmount, unitValue], index) => {
if(unitValue < workingChange && totalAmount >= unitValue){
while(workingChange >= unitValue && totalAmount >= unitValue) {
counter++
workingChange -= unitValue;
totalAmount -= unitValue;
}
return[denomination, totalAmount, ]
}
return[denomination, totalAmount]
}) ///End of Map method Produced an array with updated drawer amounts after change
//Return Check if change can be made with denominations in drawer if so update DrawerCount
workingChange = Math.round(workingChange*100)/100;//workingChange goes to zero if drawer has required denominations
///////Determine Amount in Change for Each Denom
tempCidDenomValues.forEach(([denomination, totalAmount, unitValue]) => {
let counter = 0;
if(unitValue < tempChange && totalAmount >= unitValue){
while(tempChange >= unitValue && totalAmount >= unitValue) {
counter++
tempChange -= unitValue;
totalAmount -= unitValue;
}
amountDenom = counter*unitValue;
denomAmtsArr.push([denomination, amountDenom])
}
})
///End of forEach method Produced an array with amount for each denom in change
console.log(workingChange)
return [workingChange, denomAmtsArr, requiredDrawer] ;
};
//////End of Calc Function//////////////////////
//FUNCTION for Display Change to Register
const cashRegisterDisplay = (requireDenomMet=0, denomOfChange=0 , currentDrawer=1) => {
if(requireDenomMet > 0) {
insufficientFundsDisplay();
return;
}
if(denomOfChange === 0) {
changeDue.textContent = "No change due - customer paid with exact cash";
}
const drawerOp = drawerTotal(currentDrawer) > 0 ? "OPEN" : "CLOSED"
console.log(denomOfChange)
//For Each Method to Display the Change Due
changeDue.textContent = `Status: ${drawerOp} `
denomOfChange.forEach(([denom, amount], index) => {
changeDue.textContent += `${denom}: $${Math.round(amount)/100} `;
})
};
///End of cashRegisterDisplay Function//////////
//FUNCTION for Reset
const resetForNextTransaction = (cid = []) =>{
};
//FUNCTION to Call Insufficient Funds
const insufficientFundsDisplay = () => {
changeDue.textContent = "Status: INSUFFICIENT_FUNDS";
resetForNextTransaction();
}
///FUNCTION to determine total in Drawer
const drawerTotal = (workingDrawer) => {
return workingDrawer.reduce((acc, item) => {
return acc + item[1];
}, 0)
};
/* file: styles.css */
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Challenge Information:
Build a Cash Register Project - Build a Cash Register