Javascript Calculator problems

Javascript Calculator problems


Hi, could you guys give me some feedback? Actually, I’m failing on some tests from the script FCC gives us and I would also like some help on my code. I had a rough time making the logic behind the input and output calculator. :sweat:
Here is my code: javascript calc code


Do you have a specific question? It passes all the tests, from what I’m seeing. It’s not PRETTY code, but it does the job.


Yes, when I made this topic it wasn’t passing the test. I made it do it today. I would like suggestions on my code. Like you said it’s not pretty code.


Well, the beauty of frameworks like React is the modularity. Currently, your calculator is doing everything. It isn’t really a separation of concerns.

Instead, how about you create a Display Component, and a Button Component? Even a ButtonsManager might make sense.

The Button components are only aware of their own value, and they notify the ButtonsManager when they are clicked.

The ButtonsManager can handle displaying the Button components, and perhaps decorate the response if needed, before passing a call to the Calculator component.

The Calculator Component takes notifications from the ButtonsManager, performs needed operations (for example, pushing another operation onto a stack, or updating the Display Component).

The Display component isn’t actually doing much of anything, simply being passed display data and updating itself.

I’m mostly just thinking out loud, but by separating concerns, you create FAR more maintainable code and more robust, reusable components (for example, Button Components and Display Components might be something reusable for, say, a Pomodoro Timer…)


What snowmonkey said. Try later to divide your code into components to making a habit. In future will save whole lot a headache when you need to debug it …