DEV Community

Cover image for Simple Calculator with Dark Mode.

Simple Calculator with Dark Mode.

Mohammed Farmaan. on July 08, 2020

Here's a simple calculator built using HTML, CSS and obviously, JavaScript. This calculator also has dark mode which looks really good. And here's...
Collapse
 
kretaceous profile image
Abhijit Hota •

Great web-app with great UI! I surely learned something about dark-mode implementation.

Although, may I suggest you to add the readonly attribute to the text input for result, since you're using the notorious eval() function? I'm aware that it's a 'simple' calculator but I'm just suggesting.

Great project, though!šŸ˜„

Collapse
 
zxcodes profile image
Mohammed Farmaan. •

Yeah sure. I was more focussed on the UI so I didn't put that little extra effort to look into that. Thanks for the suggestion tho. Glad you learned something!😁

Collapse
 
powerc9000 profile image
Clay Murray •

Here you go so you don't have to use eval.

// Turns a string equation into a value eg '2*2 + 4' will return '8'
function calcString(input) {
    const values = input.split("");
    const out = [];
    const ops = [];
    const allOps = ["*", "/", "+", "-", ")", "("];

    let num = "";
    for(let index = 0; index < values.length; index++){
    const v = values[index];
        if(v === " ") {
          continue;
        }
      if(!allOps.includes(v)){
        while(!allOps.includes(values[index]) && values[index] !== " " && index < values.length){
          num += values[index];
                 index ++;
        }
        index--;
      out.push(num);
      num = "";
        continue;
      }          

            if (v === ")"){
                let top = ops.pop();

                while(top !== "(" && ops.length) {
                    out.push(top);
                    top = ops.pop();

                }
                if(top !== "("){
                    return "Mismatched parens"
                }
              continue;
            }
            if(ops.length && v !== "(") {
                let top = ops[ops.length - 1];
                let topP = getPrecedence(top);
                const currP = getPrecedence(v);

              while(currP >= topP && top !== "(" && ops.length) {

                out.push(top)
                ops.pop();
                top = ops[ops.length - 1];
                topP = getPrecedence(top);

              }

            } 
            ops.push(v);


    }
    while(ops.length) {
            out.push(ops.pop());
    }
    const stack = [];
    out.forEach((o)=>{
      if(!allOps.includes(o)){
        stack.push(parseFloat(o, 10));
      } else {
        const r = stack.pop();
        const l = stack.pop();
        switch(o) {
          case "+":
            stack.push(l+r);
            break;
          case "-":
            stack.push(l-r);
            break;
          case "*":
            stack.push(l*r);
            break;
          case "/":
            stack.push(l/r);
            break;
        }
      }
    })

  return ""+stack[0]

}
function getPrecedence(value) {
    const precedence = [["("], ["*", "/"], ["+", "-"]];
    return precedence.findIndex((p)=> {
        return p.includes(value);
    })
}

Enter fullscreen mode Exit fullscreen mode
Collapse
 
zxcodes profile image
Mohammed Farmaan. •

Great. Thanks for the explanation!šŸ˜„

Collapse
 
powerc9000 profile image
Clay Murray • • Edited

Totally forgot to mention! Looks really great the design is super nice.

Thread Thread
 
zxcodes profile image
Mohammed Farmaan. •

Thank you!šŸ˜‡

Collapse
 
mathurahravi profile image
Mathurah Ravigulan •

It would be cool if you could add something with local storage so it saves the dark mode/light mode toggle the user has, so it doesn't automatically change light mode when the screen is reloaded! I came across this blog, thought it could be helpful 😊 joshwcomeau.com/gatsby/dark-mode/

Collapse
 
madza profile image
Madza •

Year 2020: If there is not dark mode, it doesn't count xdd

Collapse
 
zxcodes profile image
Mohammed Farmaan. •

Yess it doesn't count!šŸ˜‚

Collapse
 
tadeubdev profile image
Tadeu Barbosa •

Congrats! It's a good design and project!

Collapse
 
zxcodes profile image
Mohammed Farmaan. •

Thank you!😊

Collapse
 
jlohani profile image
Jayant Lohani •

This looks amazing. Great work.
I made a calculator a while ago but its design was not good and had some flaws. Will try to create another one like yours which supports dark mode.

Collapse
 
zxcodes profile image
Mohammed Farmaan. •

Thank you!šŸ˜„ Yeah sure. Feel free to check how I implemented things and don't forget to add your own design to it. Good luck.

Collapse
 
jlohani profile image
Jayant Lohani •

All I have to do is change the GitHub link at the end.šŸ˜
Just kidding.šŸ˜‚šŸ˜‚ Will just look at the implementation and make my own.

Thread Thread
 
zxcodes profile image
Mohammed Farmaan. •

Hahah sure. You can do that too xD.😜

Collapse
 
raounek profile image
touibeg mohamed •

good job mohammad ...

Collapse
 
zxcodes profile image
Mohammed Farmaan. •

Thank you!šŸ˜‡

Collapse
 
akdeberg profile image
Anik Khan •

The UI looks astonishing šŸ˜
And as per the code, I have the same suggestion as @Abhijit.
Thanks a lot for sharing. #Kudos

Collapse
 
zxcodes profile image
Mohammed Farmaan. •

Thank you!šŸ˜„ Yeah I've considered that suggestion.

Collapse
 
willemodendaal profile image
Willem Odendaal •

I like that this is just plain Javascript. Good job :)

Collapse
 
zxcodes profile image
Mohammed Farmaan. •

Thank you!šŸ˜„

Collapse
 
yedveshubham profile image
Shubham_Yedve •

Impressive Ui/design I loved it. Especially the transition between the light and the dark theme.

Collapse
 
zxcodes profile image
Mohammed Farmaan. •

Thank you. I'm glad you liked it!šŸ˜„

Collapse
 
moyohussein_92 profile image
Hussein AbdulQohar •

Thumbs up for the design. Did you notice that evaluating 0.2 multiplied by 6 or 0.2 multiplied by 3 is giving a funny answer, or this just happens to be one of the disadvantages of the eval function.