Skip to content

Exact Change  #12640

@Joe2718

Description

@Joe2718

argh

This is the third time looking at this and coming to the same dead end. The only difference I'm seeing between my answer and the answer to pass it is empty decimals. When I go to add them via precision or tofixed; I end up with it popping into a string type. No one in help knew how to convert 0.5 to 0.50 without making it to a string so I'm thinking that what I've been focused on solving with this challenge isn't even relevant to the intent of the challenge. My code has a ton of frustration written into it, Type jumping and floating point.. I'm not exactly proud of this code, I'm just in desperate need to figure out why this isn't passing? I have no clue.

Challenge Exact Change has an issue.
User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36.
Please describe how to reproduce this issue, and include links to screenshots if possible.

My code:

var values=[["PENNY", 0.01], ["NICKEL", 0.05], ["DIME", 0.10], ["QUARTER", 0.25], ["ONE", 1.00], ["FIVE", 5.00], ["TEN", 10.00], ["TWENTY", 20.00], ["ONE HUNDRED", 100.00]];
function checkCashRegister(price, cash, cid) {
	// console.log(cid);
  	var changeNum = (cash - price).toFixed(2);  // Alright, finally something small and works even though I'm using a string.  
  	// console.log(changeNum + " = cash - price      "+ cash + "<----cash   price---->"+ price);
 	var myCid = cid.reverse(); 
 	values.reverse();
 	var coins;
 	var change=[];
 	myCid.forEach(function (bill, index) {
 		// console.log(bill[0]+ "   "+ bill[1]);
 		// starting to go thru drawer
 		if (changeNum >= values[index][1]){
 			// going to try to give customer this denomination
 			// how many of these might I need?
 			coins = coinNum=Math.floor(changeNum/values[index][1]);
 			//console.log('I need ' +coins + " "+ bill[0]);
 			if(bill[1] >= coins * values[index][1]){
 				// I can do max here.
 				// console.log([bill[0],(coins * values[index][1]).toFixed(2)]);
 				var jspita = (coins * values[index][1]).toFixed(2);
 				change.push([bill[0],jspita]); // adding to bill report
 				changeNum =  (changeNum -(coins * values[index][1])).toFixed(2); // paying customer
 				bill[1]= (bill[1] -  (coins * values[index][1])).toFixed(2); // removing from drawing
 			} else {
 				// empty the drawer  in this denomination
 				if (bill[1] !== 0){ // if drawer doesn't have any of these, can't pay customer
 					change.push([bill[0],bill[1]]); // adding to bill report
	 				changeNum -= bill[1]; // paying customer
	 				bill[1] = 0; //  removing from drawer
 				}
 			}
 		}
 	// console.log(changeNum, bill[0],bill[1],values[index][1]);
	});
	cash = 0;  // need to total money left in drawer
	myCid.forEach(function (bill, index) { // clid = cash left in drawer
		if (typeof (bill[1]) == 'number' ){ // not sure how number with numbers end up with strings.
			cash += Number(bill[1].toFixed(2));
		} else {  
			cash += Number(Number(bill[1]).toFixed(2)); // This is freaking horrible!   toFixed changes crap to a string.. 

		}
	});

	// console.log(changeNum, cash);
	if (changeNum == 0 && cash == 0){
		change = "Closed";
	}
	// console.log(changeNum + " " + cash);
 	if (changeNum > 0){
		change="Insufficient Funds";
	}

	if (Array.isArray(change)){  // alright this is a crappy solution but I'm just tired of this challenge and fighting 'type'
		change.forEach(function (bill, index) {  
			// console.log(bill[1]);
			let t=  (Number(bill[1])*100).toFixed(0);
			let l = t.length; // precision is this plus 2 for the decimal places.
			// console.log(t);
			t = Number((Number(bill[1]).toPrecision(l)));
			// console.log(t);
	            // or 
	            bill[1]= Number(bill[1]);
		});
	}
  // Here is your change, ma'am.
  return change;
}

var blah =checkCashRegister(3.26, 100.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]);
console.log(blah);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions