Object is not being returned

hi guys…
I have been trying to finish this script and even if I am getting all the logic in a good way, the script is not returning the object it should…
The exercise is this one: https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/

And here is a codepen with my code: https://codepen.io/gpedro34/pen/JZEXyo

If you open the console in the codepen page, you will see that only the last object with the status “CLOSED” is being returned, all the others are returning undefined in the console, even with the correct object created… If someone can help me understand what I did wrong it would be amazing… Thanks in advance…

First your code is a little too long!!

I see you have 8 if-else which they could be turned into a simple loop very easy. this is so bad, fix it. use a for loop. (tip, for keeping the ratios, use an array too)

The problem about the undefined you say this is because when you call the checkCashRegister it returns when total is equal to amount (check line 19), otherwise you will never return anything!

I also realized you placed the functions of calcIt and manage inside the checkCashRegister, this so weird(as a back-end developer) for me, but I think when calcIt return something(or generally any method returns), it backs to the place where called. so in your case, it will return to where it was called (e.g. lines 31 and 28).

I simply tried to return the change once the main function calls the caltIt and gets returned. after line 33, I added following
return change;//will return to callee where you log the result.

again, fix the too much- if-else with a for loop.

keep going on great work, happy programming.

1 Like

Oh I see now… It was being returned to the other function and not to the end of the checkCashRegister… Yeah I was thinking in refactoring a few things but I wasn’t really getting how it could be not getting anything from the return… Thanks for the help and will do, in regarding to the if’s testament xD