Help : Encountered issue while working on Exact Change JS

here is my complete code:

var newObj = [];
var denomination = [0.01,0.05,0.10,0.25,1.00,5.00,10.00,20.00,100.00];

function getChange(change, cid){
var denom = 0;
var index = 0;
var multiplier = 0;
var availableCash = 0;
var remainingChange = 0;

	for (var i = denomination.length; i >= 0; i--){
	    if (change/denomination[i] >=1){
    	denom = denomination[i];
    	index = i;
        multiplier = Math.round(change/denomination[i]);
    	availableCash = cid[i][1];
	console.log("denom " + denom);
	console.log("multiplier " + multiplier);
	console.log("available Cash " + availableCash);
	if (availableCash > change){
		**remainingChange = parseFloat( (change - denom*multiplier).toFixed(2));**
	} else if (availableCash < change){
		remainingChange = change - availableCash
	} else if (availableCash == change && change - availableCash === 0){
		return "Closed"

	**if (remainingChange === 0 || isNaN(remainingChange)){**
**	console.log("here I am");**
**	 return "Great Job, Exact Change Available"**
**	} else {**
**	getChange(remainingChange,cid);**
**	}**


function checkCashRegister(price, cash, cid){
var change = parseFloat((cash - price).toFixed(2));
var result = getChange(change,cid);

checkCashRegister(19.50, 21.52, [["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]]);

Please refer to the blocked lines (marked with )
My question/issue is when I do console.log(result) I get undefined. Even though its prints “Here I am”. However if i change
**remainingChange = parseFloat( (change - denom
to remainingChange = 0; then console.log(result); prints “Great Job, Exact Change Available”

I am totally lost here… can someone please help me understand what is happening here and how to fix it. Any help is greatly appreciated Thanks.

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

Thanks Ariel :). It was my first time posting on the forum.

A possible work- around to this issue I am having is declare a global variable say text and store strings like “Insufficient fund” & “Closed” to that variable and print/return from the main function checkCashRegister.
Still not sure why the getChange function returns undefined tho…

Hey why did you delete your post. It worked you know; Once i declared the variable flag at the beginning of the function getChange(). Thank you very much for that.

Just like setting a global variable and storing that text in there, your solution works as well.

FYI I still quite dont understand why that IF loop would set a variable or even print what’s typed inside of it but fail to return anything other than undefined. I guess I just need to get get a deeper understanding of the function calls and get better.

Thanks again!!

It is returning “undefined” because of recursion.When you returned return “Great Job, Exact Change Available”, it is going to the last call made, while all the previous calls have returned "undefined ", so the call made from outside the function get undefined.

There was a problem with that code.It worked here but there was a flaw in the logic.So forget all about that which I wrote earlier

i think I understand what you are saying but the weird thing is if do

remainingChange = 0; Instead of remainingChange = parseFloat((change - denom * multiplier).toFixed(2));

then that If loop returns the string just fine, instead of undefined… this has confused me even more…

It’s because it then never goes to else part and so else { getChange(remainingChange,cid);}, never get called second time .so whatever you are returning will go to the call site from where the function gets called.So here the function is get called from var result = getChange(change,cid); this line,so whatever is getting returned is coming here and stored in result.
Just remember for each function call you have to remember the line from where it is called and then whatever is getting returned from that function(undefined is returned if nothing is returned) will come to the line from where the function is getting called.I think you should give some time to learning how recursion works…go through some videos, and solve a couple of problems based on recursion then you will understand very clearly what’s going on here.

Thanks a bunch for taking the time to explain this, its much clear now. you are absolutely right I do need to learn function calls & recursion…will do :slight_smile:

Yes…clear the concept of recursion right now here :slight_smile: , no matter how much time it takes!

Link to videos, if any … please.



This might help -

1 Like

Perfecto! Thx for the tip! … Faizahmadfaiz

No probs :slight_smile: