Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted almost 3 years ago

RESPONSIVE CALCULATOR USING PURE HTML, CSS AND JAVASCRIPT

accessibility, solid-js
Nwali Joseph•180
@Source-Web
A solution to the Calculator app challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

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 :)

Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • Elaine•11,360
    @elaineleung
    Posted almost 3 years ago

    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 a Syntax Error message. Anyway, I wrote some if statements, and I think I got this to work where I checked whether the screen.value has a decimal using includes(). 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 just includes(). 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

Stay up to datewith new challenges, featured solutions, selected articles, and our latest news

Frontend Mentor

  • Unlock Pro
  • Contact us
  • FAQs
  • Become a partner

Explore

  • Learning paths
  • Challenges
  • Solutions
  • Articles

Community

  • Discord
  • Guidelines

For companies

  • Hire developers
  • Train developers
© Frontend Mentor 2019 - 2025
  • Terms
  • Cookie Policy
  • Privacy Policy
  • License

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

How does the accessibility report work?

When a solution is submitted, we use axe-core to run an automated audit of your code.

This picks out common accessibility issues like not using semantic HTML and not having proper heading hierarchies, among others.

This automated audit is fairly surface level, so we encourage to you review the project and code in more detail with accessibility best practices in mind.

How does the CSS report work?

When a solution is submitted, we use stylelint to run an automated check on the CSS code.

We've added some of our own linting rules based on recommended best practices. These rules are prefixed with frontend-mentor/ which you'll see at the top of each issue in the report.

The report will audit all CSS, SCSS and Less files in your repository.

How does the HTML validation report work?

When a solution is submitted, we use html-validate to run an automated check on the HTML code.

The report picks out common HTML issues such as not using headings within section elements and incorrect nesting of elements, among others.

Note that the report can pick up “invalid” attributes, which some frameworks automatically add to the HTML. These attributes are crucial for how the frameworks function, although they’re technically not valid HTML. As such, some projects can show up with many HTML validation errors, which are benign and are a necessary part of the framework.

How does the JavaScript validation report work?

When a solution is submitted, we use eslint to run an automated check on the JavaScript code.

The report picks out common JavaScript issues such as not using semicolons and using var instead of let or const, among others.

The report will audit all JS and JSX files in your repository. We currently do not support Typescript or other frontend frameworks.

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub