JS, Cash Register, , what is wrong?

Hello everybody, I am doing the last JS project in the Curriculum, JavaScript Algorithms and Data Structures Projects: Cash Register.

And everything is ok, the program is almost working fine, except one thing.
My cash register in some cases gives a right change, and sometimes it gives a right change BUT instead of stopping performance, it continues giving MORE change.

I am trying to figure out where is the problem, but I don’t find anything.

The code is here…

Does somebody have any idea?

Here I am trying to explain better the problem. You can see,
The function changeMoney() is executed too many time, why?

and why in some cases it gives a wrong change and in other cases right one?

I’m not gonna lie, that is some difficult code to debug. I want to point out that when you use returns like you are you can end up with a call stack like this.

As far as I can tell, what is happening is when the function calls start to pop off the call stack the code ends up inside the change.forEach loop and it starts to execute the code again.

What I’m about to suggest will make the test pass but I do not really consider it to be the solution. If you add an if statement inside the change.forEach loop at the top that checks for status.status === 'OPEN' and returns if true it should work.

change.forEach( item => {
    if(status.status === 'OPEN') return; // this return
    change[change.length - 1][1] = parseFloat((change[change.length - 1][1] + element.value).toFixed(2));
    // console.log(change[change.length - 1][0], change[change.length - 1][1]);

To say thank You is not enough! it is really working now.
You are right, may be it is not a fine solution, but I spent already 2 days debuging this program and i was not able to find any solution to this bug. And I have no idea what is happening there…

I think I will leave it like this at the moment and later, if I find any better way to solve it, I will rewrite it.

Thank you once more!

Happy to help.

I do think it would require a bit of refactoring and it might not be something you want to do right now. I know once you have looked at the same code for long enough you just want to move on. I would suggest maybe coming back to it at some point and see if you can implement it using a class and methods on the class (that will be actual prototype methods).

Here are some videos if you ever get bored. Happy coding.

The JS Call Stack Explained In 9 Minutes

The Ultimate Guide to Execution Contexts, Hoisting, Scopes, and Closures in JavaScript

An Introduction to Functions, Execution Context and the Call Stack

1 Like

Wow! what a great content!
Anyway, I am happy that I almost finished the Project. It was not easy for me. Now, to keep moving forward and learn more!
I am so thankful for your help.

Actually, you were so right! i changed the code, because it was that loop. and now it is working just fantastic.

Great, refactoring can be very satisfying.

Most times the first solution to a problem is rarely the best one. But it’s hard to not just think, hey it works let’s move on.

1 Like