RESPONSIVE CALCULATOR USING PURE HTML, CSS AND JAVASCRIPT

Solution retrospective
This is my solution to this Calculator App, help me review it and check its responsiveness.
I used input[type=range]
for my theme slider and that made my code longer as I had to override the default browser styling for different browsers (Firefox and Edge not working as expected).
My approach was to change the body class whenever the range value changes. I've commented some block of code that is meant to get the theme from the local storage and set the theme to the corresponding value. The code is currently not working, the theme returns to default on refreshing the page.
I also have an issue with the functionality, I want to prevent the keydown of "." twice in a single operand and also change the inner text of the answer screen to 'Syntax Error' when the user enters operators in an illogical order. If you view my calculator.js file, you're going to see the regex I commented out, some of the test cases failed. How do I fix this? Regex?
I hope to get help with this. Thanks :)
Please log in to post a comment
Log in with GitHubCommunity feedback
- @elaineleung
Hi Nwali, good work here! I cloned your repo to see if I can help with troubleshooting, and anyway, will try to answer some questions here.
About the theme, I suggest you check out Adam Argyle's article on building out a theme switcher component, which was what helped me with my themes and using it with
localStorage
for loading. I won't attempt to explain the mechanics of it all, but basically he uses a separate script for the theme functions, and the key is to have the script run before the page loads so that the browser can retrieve the saved theme value and display it. Do have a read and see if that helps you!About the decimals, I think for user experience, it's better to just use
return
to stop the undesirable behavior instead of showing aSyntax Error
message. Anyway, I wrote some if statements, and I think I got this to work where I checked whether thescreen.value
has a decimal usingincludes()
. This is easy to do if it's only the first operand, but if there's the second one and that one needs a decimal (e.g., 1321.12 * 87.5), then I need more than justincludes()
. Anyway, try this out and see if it works for you too:function getBtnValue(event){ let btnValue = event.target.dataset.val; if (btnValue === ".") { if (screen.value.match(/[-+*\/]/)) { const operandArr = screen.value.split(/[-+*\/]/) if (operandArr[1].includes(".")) return // this checks if second operand has decimal } else { if (screen.value.includes(".")) return // this checks if first operand has decimal } } screen.value = (screen.value + btnValue).replace(/,/g, ""); }
Hopefully if it works you can refactor and make it look less clunky with all those if statements 😅
One thing I noticed is, I'm able to input 0 and another number (e.g., "0321"). While things can still evaluate, I think in terms of the display it would be better to disallow it, which is what I've done in my calculator.
Hope this helps you out, and good luck!
Marked as helpful
Join 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