Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

Cash Register project, - part of the tests fail.
Console returns [ReferenceError: _randomNumber is not defined],

same tests (like “customer does not have enough money”) - both passed and failed.

Your code so far

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<input id="cash"> Cash
<div id="change-due"></div>
<button id="purchase-btn" onclick="countChange()">Purchase</button>






</body>
</html>
<script src="./script.js"></script>


/* file: styles.css */

// INITIAL DATA
let price = 1.86
// let cash = 100
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 manipulateCash = (num1,num2,operator) => {
	switch(operator){
		case "-":
			return (num1 - num2).toFixed(2)
		case "+":
			return (parseFloat(num1) + parseFloat(num2)).toFixed(2)
		default:
			console.log("Invalid operator")
			break;
	}
}

const iterateThroughDrawer = (drawer,change) =>{
	// console.log("DRAWER",drawer)
	let {name, amount, step} = drawer;
	// console.log("name,amount,step",name,amount,step);
	let currentChange = change;
	let clientsBag = [name,0];
	while (amount > 0 && (currentChange - step) >= 0 ){
		clientsBag[1] = manipulateCash(clientsBag[1],step,"+");
		amount = manipulateCash(amount,step,"-");
		currentChange = manipulateCash(currentChange,step,"-");
	}
	return [clientsBag, currentChange]
}


const countChange = () => {
	
	let cash = Number(document.getElementById("cash").value)
	let change = manipulateCash(cash, price,"-")
	let transactionDetailsField = document.getElementById("change-due")
	let moneyForClient = []
	
	
	let cashInDrawer = cid.toReversed().map((cashRegister)=>{
		let stepValueFor = {
			PENNY: 0.01,
			NICKEL: 0.05,
			DIME: 0.1,
			QUARTER: 0.25,
			ONE: 1,
			FIVE: 5,
			TEN: 10,
			TWENTY: 20,
			"ONE HUNDRED": 100
		}
		const name = cashRegister[0],
			amount = parseFloat(cashRegister[1]),
			step= parseFloat(stepValueFor[name])
		
		return {name,amount,step}
	})
	let countTotalCash = () =>{
		let total = 0
		cashInDrawer.forEach((cashRegister)=>{
			total = manipulateCash(total,(cashRegister.amount), "+")
		})
		return total
	}
	
	
	const state = {
		// CLIENT GAVE EXACT AMOUNT
		isNoChange: Number(manipulateCash(cash, price, "-")) === 0,
		// CLIENT HAS NOT ENOUGH MONEY
		isClientHasNoMoney: Number(manipulateCash(cash, price, "-")) < 0,
		// REGISTER IS CLOSED, NO MONEY LEFT
		isClosed: countTotalCash() === change,
		// NOT ENOUGH OF THE CORRECT NOTES || TOTAL AMOUNT OF MONEY IN THE REGISTER IS NOT ENOUGH
		isNotEnoughProperCash: () =>{
			let clientsCash = []
			let total = 0
			let changeLeft = change
			// console.log("total",total, "changeLeft",changeLeft)
			cashInDrawer.forEach((cashRegister)=>{
				let drawer = iterateThroughDrawer(cashRegister, changeLeft)
				
				// console.log("drawer",drawer)
				changeLeft = drawer[1];
				if (drawer[0][1] > 0){
					clientsCash.push(`${drawer[0][0]}: $${Number(drawer[0][1])}`)
					total = manipulateCash(total, drawer[0][1], "+")
					// console.log("clientsCash",clientsCash)
				}
				
			})
			// console.log("clientsCash",clientsCash)
			moneyForClient = clientsCash.join(", ")
			// console.log("AFTER WHILE:\n total\n",total, "\nchangeLeft\n",changeLeft, "\n moneyForClient\n", moneyForClient)
			
			return total < change || changeLeft > 0
		}
		
	}
	let status = ""



// console.log("cashInDrawer",cashInDrawer)
// APP CODE
	console.log(
		`
		state
		isNoChange: ${state.isNoChange} ,
		isNotEnoughCash: ${state.isClientHasNoMoney} ,
		isClosed: ${state.isClosed} ,
		isNotEnoughProperCash: ${state.isNotEnoughProperCash()},
		
		`
	)
	console.log("moneyForClient",moneyForClient)
	
	if (state.isClientHasNoMoney){
		alert("Customer does not have enough money to purchase the item")
	}
	else if (state.isNoChange){
		status = "No change due - customer paid with exact cash"
	}
	else if (state.isNotEnoughProperCash()){
		status = "Status: INSUFFICIENT_FUNDS"
	} else if (state.isClosed){
		status = "Status: CLOSED"
	} else {
		status = "Status: OPEN"
	}
	console.log("status",status)
	console.log("moneyForClient", moneyForClient)
	if (status !== "Status: INSUFFICIENT_FUNDS"){
		status += ` ${moneyForClient}`
	}
	console.log("status",status)
	transactionDetailsField.innerText = status
	
}








Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 YaBrowser/24.4.0.0 Safari/537.36

Challenge Information:

Build a Cash Register Project - Build a Cash Register

Hello,
All I did is I opened the project page copied your HTML and JS code then hit submit and everything worked just fine, you might want to reset the lesson close the tab then reopen it then copy your code over and try to submit again

2 Likes

Hi!
After your comment I’ve tried to do the same in a different browser et voila.
The silliness of the mistakes / bugs sometimes drive me mad.

Thanks! :grin:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.