Created
August 3, 2021 21:30
-
-
Save thusmiley/74735c7e99db0b0ad458444d455216f2 to your computer and use it in GitHub Desktop.
Build a Javascript Calculator FCC (solution)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!-- codepen link: https://codepen.io/thudang211/pen/wvdxZOR --> | |
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8" /> | |
| <meta http-equiv="X-UA-Compatible" content="IE=edge" /> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
| <script | |
| crossorigin | |
| src="https://unpkg.com/react@17/umd/react.development.js" | |
| ></script> | |
| <script | |
| crossorigin | |
| src="https://unpkg.com/react-dom@17/umd/react-dom.development.js" | |
| ></script> | |
| <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script> | |
| <link | |
| href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" | |
| rel="stylesheet" | |
| integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" | |
| crossorigin="anonymous" | |
| /> | |
| <link rel="stylesheet" href="./style.css" /> | |
| <title>Calculator</title> | |
| </head> | |
| <body> | |
| <div id="app"></div> | |
| <script type="text/babel" src="./index.js"></script> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function App() { | |
| const [expression, setExpression] = React.useState(""); | |
| const [answer, setAnswer] = React.useState(0); | |
| const display = (symbol) => { | |
| setExpression((prev) => prev + symbol); | |
| if (expression[expression.length - 1] === "=") { | |
| if (/[0-9.]/.test(symbol)) { | |
| setExpression(symbol); | |
| setAnswer("0"); | |
| } else { | |
| setExpression(answer + symbol); | |
| } | |
| } | |
| }; | |
| const calculate = () => { | |
| setAnswer(eval(expression)); | |
| setExpression((prev) => prev + "="); | |
| }; | |
| const allClear = () => { | |
| setExpression(""); | |
| setAnswer(0); | |
| }; | |
| const clear = () => { | |
| setExpression((prev) => | |
| prev | |
| .split("") | |
| .slice(0, prev.length - 1) | |
| .join("") | |
| ); | |
| setAnswer(0); | |
| }; | |
| return ( | |
| <div className="container"> | |
| <div className="grid"> | |
| <div id="display"> | |
| <input type="text" value={expression} disabled className="input" /> | |
| <div id="total">{answer}</div> | |
| </div> | |
| <div onClick={allClear} className="padButton" id="allclear"> | |
| AC | |
| </div> | |
| <div onClick={clear} className="padButton" id="clear"> | |
| C | |
| </div> | |
| <div onClick={() => display("/")} className="padButton" id="divide"> | |
| / | |
| </div> | |
| <div onClick={() => display("7")} className="padButton" id="seven"> | |
| 7 | |
| </div> | |
| <div onClick={() => display("8")} className="padButton" id="eight"> | |
| 8 | |
| </div> | |
| <div onClick={() => display("9")} className="padButton" id="nine"> | |
| 9 | |
| </div> | |
| <div onClick={() => display("*")} className="padButton" id="multiply"> | |
| x | |
| </div> | |
| <div onClick={() => display("4")} className="padButton" id="four"> | |
| 4 | |
| </div> | |
| <div onClick={() => display("5")} className="padButton" id="five"> | |
| 5 | |
| </div> | |
| <div onClick={() => display("6")} className="padButton" id="six"> | |
| 6 | |
| </div> | |
| <div onClick={() => display("-")} className="padButton" id="subtract"> | |
| - | |
| </div> | |
| <div onClick={() => display("1")} className="padButton" id="one"> | |
| 1 | |
| </div> | |
| <div onClick={() => display("2")} className="padButton" id="two"> | |
| 2 | |
| </div> | |
| <div onClick={() => display("3")} className="padButton" id="three"> | |
| 3 | |
| </div> | |
| <div onClick={() => display("+")} className="padButton" id="add"> | |
| + | |
| </div> | |
| <div onClick={() => display("0")} className="padButton" id="zero"> | |
| 0 | |
| </div> | |
| <div onClick={() => display(".")} className="padButton" id="decimal"> | |
| . | |
| </div> | |
| <div onClick={calculate} className="padButton" id="equals"> | |
| = | |
| </div> | |
| </div> | |
| </div> | |
| ); | |
| } | |
| ReactDOM.render(<App />, document.getElementById("app")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@100;300&display=swap') | |
| $func-color: #F2A23B | |
| $num-color: #8d8c8d | |
| $clear-color: #6D6C6C | |
| $display-color: #4F4E4F | |
| * | |
| margin: 0 | |
| padding: 0 | |
| box-sizing: border-box | |
| body | |
| height: 100vh | |
| background-image: linear-gradient(to right, #FFC1BE, #C6E6E1) | |
| .container | |
| font-family: 'Roboto', sans-serif | |
| font-size: 1.2em | |
| padding-top: 100px | |
| justify-content: center | |
| align-items: center | |
| display: flex | |
| .grid | |
| display: grid | |
| grid-template-columns: 1fr 1fr 1fr 1fr | |
| grid-template-rows: 1fr 1fr 1fr 1fr 1fr 1fr | |
| grid-template-areas: "display display display display" "allclear allclear clear divide" "seven eight nine multiply" "four five six subtract" "one two three add" "zero zero decimal equals" | |
| .padButton | |
| display: flex | |
| justify-content: center | |
| align-items: center | |
| padding: 0px 40px | |
| background-color: $num-color | |
| border: 1px solid $display-color | |
| cursor: pointer | |
| color: white | |
| #display | |
| background-color: $display-color | |
| grid-area: display | |
| #total | |
| text-align: right | |
| color: white | |
| padding-right: 15px | |
| font-size: 1.2em | |
| .input | |
| background-color: $display-color | |
| border: none | |
| color: white | |
| font-size: 0.8em | |
| padding-right: 15px | |
| text-align: right | |
| width: 100% | |
| #allclear | |
| grid-area: allclear | |
| #zero | |
| grid-area: zero | |
| .padButton:hover, .padButton:active, #allclear:hover, #clear:hover, #divide:hover, #multiply:hover, #add:hover, #subtract:hover, #equals:hover | |
| background-color: #C2C2C2 | |
| #divide, #multiply, #subtract, #add, #equals | |
| background-color: $func-color | |
| #allclear, #clear | |
| background-color: $clear-color | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment