Design comparison
Solution retrospective
In this project, my main objective was to make the code more accessible and conform to semantic standards. I initially considered using the "eval" function, but realized that it could be dangerous and is generally not recommended. As a result, I had to come up with a new approach for implementing the calculator's functionality, which I found quite enjoyable. Additionally, I was able to apply my recently acquired skills in regular expressions to the project.
Community feedback
- @ChamuMutezvaPosted almost 2 years ago
Hi. So far so good with your application. Here are some considerations to look at:
- Set a maximum number that can be entered in the display. I can currently enter any number by continually pressing a button.
- Do the following calculation
0.1 + 0.2
- I can write such as
90....768.8
, what I mean here is create a condition that does not allow to enter more than 1 decimal point. - This can also be avoided
9+-/*123
- Do a calculation that gives you infinity eg
100/0
, then press the delete button to remove some characters from infinity and perform a calculation. How best can those scenario be handled
Happy coding
Marked as helpful1@frank1003APosted almost 2 years ago@ChamuMutezva Interesting, working on it now! Thank you for the feedback.
0@frank1003APosted almost 2 years ago@ChamuMutezva As per your observations everything is resolved except the
90....768.8
and9+-/*123
issues. I am to resume later, a bit distracted now. Any ideas you have on how that can be accounted for will be appreciated.0@ChamuMutezvaPosted almost 2 years ago@frank1003A For the decimal point:
- check if a decimal point has been pressed
- If a decimal point has been pressed, check if the current value has a decimal point. If a decimal point exist then return the current value as is otherwise add a decimal point . Something like below:
if (value === ".") { console.log("decimal point clicked") if (output.indexOf(".") === -1) { console.log("decimal point does not exist, add a decimal point") } else { return output } }
where
value
is the representation of the digit pressed andoutput
is the currentstate
0@ChamuMutezvaPosted almost 2 years ago@frank1003A , an important way that can help in debugging is to have functions that are specific task. In mind I would have a function that is dedicated to digits only , that is when entering numbers
0 up to 9
. Then another function will deal with the decimal point and another will be responsible for operators+, /, *, -
. That way , makes it easy to fix a bug caused by decimal point or an other operationMarked as helpful0@frank1003APosted almost 2 years ago@ChamuMutezva You are absolutely right about functions and most of the calculator features were function based, but because I am updating the state using a state callback, the functions had to directly return values rather than just performing those functions, so I felt it made sense to write the conditions directly. You are right though, that is not ideal for easy debugging.
0@frank1003APosted almost 2 years ago@ChamuMutezva Solved both with good old regex
const handleDecimalLimits = (value: string, newOutput: string) => { let fm = ""; if (newOutput.endsWith(".") && value === ".") { // Split the newOutput string into an array of numbers. const numbers = newOutput.split("+"); // Replace the last number in the array with a new version that has at most one decimal point. numbers[numbers.length - 1] = numbers[numbers.length - 1].replace( /\.+/g, "." ); // Join the numbers array back into a string. fm = numbers.join("+"); } else { fm = newOutput; } return fm; }``` ```const formatOperator = (str: string) => str.replace(/([x/+-])+/g, "$1"); const handleArithmeticOperatorLimit = (newOutput: string): string => { return formatOperator(newOutput); };```
0@ChamuMutezvaPosted almost 2 years ago@frank1003A , that does not work perfectly
if (newOutput.endsWith(".") && value === ".")
. endsWith will just check ifnewOutput
has a decimal point at the end but what if I type my number as0.342.89.09.23.
. What you need to check is:- does a decimal point exist in
newOutput
- if
newOutput
has a decimal point, then do nothing else - add the decimal point
- you can do that as explained earlier using
indexOf
, as in the followingif (newOutput.indexOf(".") === -1)
, If a decimal point does not exist , that statement will be true , hence add the decimal point .
0@frank1003APosted almost 2 years ago@ChamuMutezva You are right about the function not accounting for the case
0.342.89.09.23
. Simply checkingnewOutput.indexOf(".") === -1
will check for all decimal points innewOutput
butnewOutput
can be a combination of operators and numbers and decimals. I still need to tie this index check to the numbers in an expression. I'll play around with it in a bit. Appreciate the detail ✌0 - @RubenSmnPosted almost 2 years ago
Hi Frank, just played around with your calculator.
When calculating something like
5+6
which result in11
after that I wanted to immediately calculate4*2
but instead of that the input did no clear and I got114*2
, not sure if this was something intended but I would like to mention it.From that great work!
Marked as helpful1@frank1003APosted almost 2 years ago@RubenSmn Thank you for this. I wanted it to be able to use the result to further calculate if needed by a user, but I think I need to update that to clear the result if the next input after the result is not an operator.
0@frank1003APosted almost 2 years ago@RubenSmn Thanks for spotting this. I have updated the calculator to tackle this issue. If a result is passed and a number is clicked, it performs a reset, but if you click on an operator button or a period, it updates the screen with that result.
0 - @abdullah43577Posted almost 2 years ago
Hello Frank,
Great job taking on this challenge, using the eval() method isn't entirely bad, you can make your use of eval() safe by using strict mode, this means that you can add a piece of code at the top of every js file you would be using eval() for
use strict
this avoids some of the most dangerous use of eval().The
eval()
function evaluates JavaScript code represented as a string and returns its completion value. The source is parsed as a script.But it's great seeing that you got the challenge completed already without using eval(). That's very nice. I used eval() in my project and it saved me tons of lines of code.
Marked as helpful1@frank1003APosted almost 2 years ago@abdullah43577 Thanks for that piece of information, which could have saved me some time, but I will use it more now.
0
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord