[help] FCC beta Challenge Cash register

Hello, i need help with this exercice:
https://beta.freecodecamp.org/en/challenges/javascript-algorithms-and-data-structures-projects/cash-register

I have a specific question:
I calcul the range like this:
let change = (cash - price).toFixed(2);

I want to play with my numbers after ‘,’ numbers. Here, if i do this:
let aDecimal = parseInt( change.toString().split(".")[1] ); //50
I have 50. Now,i do this:
aDecimal = parseFloat(“0.”+aDecimal);
So i have this 0.5
But if i have 0.5.toString()[2] != 0 && 0.5.toString()[3] == undefined, i want this to have an extra “0” but i want at the end a number type like this 0.50 (number type, not a string).

If some one could help me on this. I’m having seisur.
I tried to do the exercice with full number as decimal like this “50” for 50 cents. But it should be easier to calcul some thing else the other way.

1 Like

Could you please share your code? If you use repl.it, you can paste the link here and it will run right here in the forum.

Hi, ok i’m doing to past a part of my code.

edit:

If you can teach me a way to basicaly have this:
50 => 0.5 => 0.50
5 => 0.05

Hello everybody, everybody hello

0.50 and 0.5 are equal in value. If I have the following code:

var cents = 0.50;
console.log(cents); // displays 0.5

The only way to display 0.50 that I am aware of is to use something like toFixed as:

var cents = 0.50;
var converted = cents.toFixed(2)
console.log(converted); // displays 0.50

Of course in the above, converted is a string and not a number.

If you are just wanting it to display the extra 0 at the end, why do you care if it is a string or a number?

Hi,
because i’m doing operation one it later on.
Now, i’m doing calcul with the cent like this:
i have 0.50 but i use 50.
Then for 0.25, i use 25, for 0.10 i use 10…

Why not just multiply/divide it by 100 then?

If i 50/100 i have 0.5 float. But it is not a problem to do calcul if it doesn’t show 0.50 ?

If I remember correctly, this challenge expects numbers rather than strings. 0.5 is a number. If I am wrong and you need a string with "0.50" then you can still use the number 0.5 throughout your code and then do a parseInt() at the end to convert it to "0.50".

Hi back.
Ok, i finished the exercice:

It’s maybe not the right way, it’s maybe too long and could be optimized but at least i used what i thought and make it happen:)

How:
-Retrieve Integer/fractional change and Cash register integer/factional values
-Check for fractional and integer value with the cash registers values if you have enough funds
-if yes, work on the integer and fractional. If no, leave it no fund
-If at the end, your array is empty => closed / not empty => you still have money in the cash machin

/*
Link: https://beta.freecodecamp.org/en/challenges/javascript-algorithms-and-data-structures-projects/cash-register
*/

function checkCashRegister(price, cash, cid) {
    //find the change (use toFixed)
    let change = (cash - price).toFixed(2); //0.50 centimes
    //find the number before ',' (Integer part) (convert it to String to find it, then Integer)
    let bDecimal = parseInt( change.toString().split(".")[0] );
    //find the number after ',' (Fractional part) (convert it to String to find it, then Integer)
    let aDecimal = parseInt( change.toString().split(".")[1] ) / 100;
    //How many my cash register have in the range of values 100, 20, 10, 5 and 1
    let unitArray = [ cid[8][1], cid[7][1], cid[6][1], cid[5][1], cid[4][1] ];
    //How many my cash register have in the range of 0.25, 0.10, 0.02 and 0.01
    let unitArray2 = [ cid[3][1], cid[2][1], cid[1][1], cid[0][1] ];
    //The change of how many of each i have to give (only concern 100, 20, 10, 5, and 1)
    let unitArrayChange = [ ["ONE HUNDRED", 0], ["TWENTY", 0], ["TEN", 0], ["FIVE", 0], ["ONE", 0] ];
	//The change of how many of each i have to give (only concern 0.25, 0.10, 0.05 and 0.01)
	let unitArrayChange2 = [ ["QUARTER", 0], ["DIME", 0], ["NICKEL", 0], ["PENNY", 0] ];
	
	//Copies to do maths
	let unitArrayChangeNew1 = [ ["ONE HUNDRED", 0], ["TWENTY", 0], ["TEN", 0], ["FIVE", 0], ["ONE", 0] ];	
	let unitArrayChangeNew2 = [ ["QUARTER", 0], ["DIME", 0], ["NICKEL", 0], ["PENNY", 0] ];
	
    let changeFactor = 0;
	let unitIndex = 0;
	
	let calc1 = 0;
	let calc2 = 0;

	//In the cash register, i check how many "Integer money" i have
	for( let k = 0; k < unitArray.length; k++ ) {
		calc1 += unitArray[k];
		
		//In the cash register, i check how many "Fractional money" i have
		for( let k = 0; k < unitArray2.length; k++ ) {
			calc2 += unitArray2[k];
		}
	}
	
	//To start with, if i don't have enough Integer money OR Fractional Money
	if( ( bDecimal > calc1 )  || ( aDecimal > calc2 ) ) {
		return {status: "INSUFFICIENT_FUNDS", change: []};
	}
	
	/*
	WORK ON INTEGER VALUES
	While my change is not ZERO and my cash register machin is not EMPTY
	I continue to check for every values possibles
	I update my cash register and my change
	*/
    while( bDecimal > 0 ) {
		if( ( bDecimal >= 100 ) && ( unitArray[0] >= 100 ) ){
			changeFactor = 100;
			unitIndex = 0;
		} else if( ( bDecimal >= 20 ) && ( unitArray[1] >= 20 ) ) {
			changeFactor = 20;
			unitIndex = 1;
		} else if( ( bDecimal >= 10 )  && ( unitArray[2] >= 10 ) ){
			changeFactor = 10;
			unitIndex = 2;
		} else if( ( bDecimal >= 5 ) && ( unitArray[3] >= 5 ) ){
			changeFactor = 5;
			unitIndex = 3;
		} else if( ( bDecimal >= 1 ) && ( unitArray[4] >= 1 ) ){
			changeFactor = 1;
			unitIndex = 4;
		} else {
			// break;
		}
		
		// update my bDecimal (Integer change)
		bDecimal = bDecimal - changeFactor;
		// update my cash register machine
		unitArray[unitIndex] = unitArray[unitIndex] - changeFactor;
		// update how MANY of this money i used
		unitArrayChange[unitIndex][1] = unitArrayChange[unitIndex][1] + 1;
		//use the precedent value to calcul how MUCH of this money i used
		unitArrayChangeNew1[unitIndex][1] = unitArrayChange[unitIndex][1] * changeFactor;
    }

	//reset
	changeFactor = 0;
	unitIndex = 0;
	
	
	/*
	WORK ON FRACTIONAL VALUES
	While my change is not ZERO and my cash register machin is not EMPTY
	I continue to check for every values possibles
	I update my cash register and my change
	*/
	while( aDecimal > 0 ) {
		if( ( aDecimal >= 0.25 ) && ( unitArray2[0] >= 0.25 ) ){
			changeFactor = 0.25;
			unitIndex = 0;
		} else if( ( aDecimal >= 0.10 ) && ( unitArray2[1] >= 0.10 )  ) {
			changeFactor = 0.10;
			unitIndex = 1;
		} else if( ( aDecimal >= 0.05 ) && ( unitArray2[2] >= 0.05 )  ){
			changeFactor = 0.05;
			unitIndex = 2;
		} else if( ( aDecimal >= 0.01 ) && ( unitArray2[3] >= 0.01 )  ){
			changeFactor = 0.01;
			unitIndex = 3;
		} else {
			//break;
		}
		
		//update my bDecimal (Fractional change)
		aDecimal = aDecimal - changeFactor;
		
		//update my cash register machine (use parseFloat and toFixed to manage float Calcul)
		let valueFixed = parseFloat( unitArray2[unitIndex] - changeFactor ).toFixed( 2 );
		unitArray2[unitIndex] = parseFloat( valueFixed );
		
		// update how MANY of this money i used
		unitArrayChange2[unitIndex][1] = unitArrayChange2[unitIndex][1] + 1;
		
		//Find how MUCH of this money i used (use parseFloat and toFixed to manage float Calcul)
		let valueFixed2 = (unitArrayChange2[ unitIndex][1] * changeFactor ).toFixed( 2 );
		unitArrayChangeNew2[unitIndex][1] = parseFloat( valueFixed2 );
		
    }
	
	//concat my Integer and Fractional value from my cash register
	var unitArray3 = unitArray.concat( unitArray2 );
	
	//delet empty value to check if cash register machin is empty or not later on
	unitArray3 = unitArray3.filter( function( x ){
		return ( x !== 0 );
	} );
	
	//Concat my two array into one
	var unitArrayChangeNew3 = unitArrayChangeNew1.concat( unitArrayChangeNew2 );
	
	//Then EMPTY "0" values
	for( let k = 0; k < unitArrayChangeNew3.length; k++ ) {
		if( unitArrayChangeNew3[k][1] == 0 ) {
			unitArrayChangeNew3[k].splice( 0, 1 );
			delete unitArrayChangeNew3[k];
		}
	}
	//Then REMOVE "empty" values
	unitArrayChangeNew3 = unitArrayChangeNew3.filter( function( x ){
		return ( x !== ( undefined || null || '' ) );
	} );
	
	//Handle if my register machin is empty or not
	if( unitArray3.length == 0 ) {	
		return {status: "CLOSED", change: cid};
	}else {
		return {status: "OPEN", change: unitArrayChangeNew3};
	}
};

console.log( checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]));
console.log( checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]) );
console.log( checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) );
console.log( checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) );
console.log( checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) );

ps: thanks to “@Sweet Coding :)” for the tips

1 Like