Hi everyone! Welcome back to another blog where I document the things I learned in web development. I do this because it helps retain the information and concepts as it is some sort of an active recall.
On days 42-47, I built a cash register app to complete part 4 of the data structures & algorithms certification project in freecodecamp.
In this app, you will need to input the cash from the customer and the program will output the total change in the form of each available denomination.
For example, the declared price of the object is $140, and the customer’s cash is $455.41, the program will output a total change of $315.41 as well as the customer’s change in the form of each available denomination. In this case, it’s: ONE-HUNDRED: $100.00, TWENTY: $60.00, TEN: $20.00, FIVE: $55.00, ONE: $80.00, QUARTER: $0.25, DIME: $0.10, NICKEL: $0.05, PENNY: $0.01
While building this project, I got stuck in a certain problem for a few days.
These are the reasons why:
In Javascript, floating-point numbers have limited precision due to the way they are represented internally. This can lead to unexpected results when performing arithmetic operations, especially with decimals (e.g., 0.01 might be read as 0.009999998321).
I did not take into account that denominations cannot be physically divided into cents (e.g., a $1 bill cannot be physically divided, nor can a $100 bill or any other bills). Therefore, I need to create a function that will loop through every denomination and check if their respective values can be used as a change.
At this point, my entire code was a complete mess. My if-else statements were redundant, and some of them were not even making sense. Additionally, the conditions of my if-else statements were not properly specified, leading to unexpected results. Because of this, it took me a while to figure out the solution to my problem, as I needed to deal with other issues first since I was entangled in a series of them.
After some time, I finally finished the program and was able to solve the problems mentioned earlier.
Here’s what I did:
First, I realized that to solve my main problem, which was to satisfy the conditions of the specified user stories to complete the project, I needed to clean up my code. I started by reworking the entire logic from top to bottom and reorganized everything because it was in complete chaos. Next, I rearranged the logic of my functions and loops and removed unnecessary if-else statements. After that, I properly specified the conditions of the remaining if-else statements.
Second, I debugged my code to identify which variables were returning unexpected results (e.g., what was meant to be 0.01 is returned as 0.009999998321). This was crucial since it disrupted the flow of calculations and prevented the conditions of my if-else statements from being satisfied.
Lastly, I created a function that will loop through every denomination and check if their respective values can be used as a change.
Overall, this was my first time, and definitely not the last, of being stuck in a project for days.
I realized that we learn much more in this phase compared to smoothly completing a project.
As I continue on my path to web development, the projects I take on are gradually becoming more difficult for me. Therefore, I must prepare myself to debug the problems that I will potentially encounter in order to gain experience and be okay with the fact that it’s normal to be stuck on a project for a long time. What matters most is the continuous search for answers until the problem is eventually solved. After all, it’s what most job descriptions require: the attitude to persistently seek answers to your questions while knowing the right questions to ask.
Anyways, that’s all for now, more updates in my next blog! See you there!
Top comments (0)