๐ŸŽจ Calculator

kirin.logยท2021๋…„ 3์›” 15์ผ
0
post-custom-banner

๐Ÿช logic

  • html ๋‚ด input์„ ํ†ตํ•ด ๋ฒ„ํŠผ์„ ์ƒ์„ฑํ•ด์ค€๋‹ค
  • ์ˆซ์ž+์—ฐ์‚ฐ์ž๋ฅผ ๋ˆ„๋ฅด๋ฉด input์— value๋“ค์–ด๊ฐ€๋Š” ํ•จ์ˆ˜ ์ƒ์„ฑ
  • ์ˆซ์ž+์—ฐ์‚ฐ์ž๋ฅผ ์ž…๋ ฅ ํ›„ =๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์—ฐ์‚ฐ๋˜๋Š” ๊ธฐ๋Šฅ(eval)
  • AC๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋ฆฌ์…‹๊ธฐ๋Šฅ ํ•จ์ˆ˜ ์ƒ์„ฑ
  • del๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์˜ค๋ฅธ์ชฝ๋ถ€ํ„ฐ ์‚ญ์ œ ๊ธฐ๋Šฅ ํ•จ์ˆ˜ ์ƒ์„ฑ
๐Ÿš€ html

<!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" />
        <link href="index.css" rel="stylesheet" />
        <script defer src="index.js"></script>
        <title>Document</title>
    </head>
    <body>
        <div class="container">
            <div class="calFrame">
                <div class="inputFrame">
                    <input class="input previous" type="text" readonly />
                    <input class="input current" type="text" readonly />
                </div>

                <div class="btnFrame">
                    <input type="button" value="AC" class="spanTwo clear" />
                    <input type="button" value="DEL" class="del" />
                    <input type="button" value="+" class="operator" />
                    <input type="button" value="7" class="number" />
                    <input type="button" value="8" class="number" />
                    <input type="button" value="9" class="number" />
                    <input type="button" value="-" class="operator" />
                    <input type="button" value="4" class="number" />
                    <input type="button" value="5" class="number" />
                    <input type="button" value="6" class="number" />
                    <input type="button" value="*" class="operator" />
                    <input type="button" value="1" class="number" />
                    <input type="button" value="2" class="number" />
                    <input type="button" value="3" class="number" />
                    <input type="button" value="/" class="operator" />
                    <input type="button" value="0" class="number" />
                    <input type="button" value="." class="number" />
                    <input type="button" value="=" class="spanTwo result" />
                </div>
            </div>
        </div>
    </body>
</html>
๐Ÿš€ css
>
* {
    background: linear-gradient(to right, rgb(252, 6, 121), rgb(240, 117, 154));
    box-sizing: border-box;
    outline: none;
}
>
.calFrame {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    margin: 100px auto;
    padding: 10px;
    max-width: 400px;
    width: 30vw;
    height: 55vh;
    background: thistle;
    border: 1px solid black;
    border-radius: 5px;
}
>
.btnFrame {
    display: grid;
    grid-template-columns: repeat(4, 75px);
    grid-template-rows: repeat(5, 75px);
    background: thistle;
}
>
.btnFrame input {
    margin: 3px;
    color: whitesmoke;
    font-weight: 900;
    font-size: x-large;
    cursor: pointer;
}
>
.inputFrame {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 65px;
    border: 1px solid black;
}
>
.previous,
.current {
    padding: 0 30px;
    height: 50px;
    border: none;
    color: rgb(39, 30, 30);
    background: rgb(250, 202, 226);
    text-align: right;
}
>
.previous {
    font-size: 20px;
}
>
.current {
    font-size: 30px;
}
>
.spanTwo {
    grid-column: span 2;
}
๐Ÿš€ js

'use strict';

const operator = document.querySelectorAll(".operator"); // ์—ฐ์‚ฐ์ž
const numBtn = document.querySelectorAll(".number"); // ์ˆซ์ž(0~9)
const input = document.querySelector(".input"); // ์ž…๋ ฅ๋ž€

const previous = document.querySelector(".previous");
const current = document.querySelector(".current");


// ์ˆซ์ž ๋ฐ ์—ฐ์‚ฐ์ž๋Š” "๋ฐฐ์—ด"๋กœ ๋ฐ›์•„์ง„๋‹ค(querySelectorAll)

// ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ, ์ž…๋ ฅ์ฐฝ์— ์ž…๋ ฅ ๊ธฐ๋Šฅ
const btnClick = () => {
    for (let i = 0; i < numBtn.length; i++) {
        numBtn[i].addEventListener("click", function (e) {
            //console.log(e.target);
            current.value = (current.value += e.target.value).toLocaleString();
        });
    }

    for (let i = 0; i < operator.length; i++) {
        operator[i].addEventListener("click", function (e) {
            //console.log(e.target);
            current.value += e.target.value;
        });
    }
};
btnClick();

// ์—ฐ์‚ฐ ๊ธฐ๋Šฅ
const resultBtn = document.querySelector(".result");

resultBtn.addEventListener("click", (e) => {
    previous.value = current.value;
    current.value = eval(current.value).toLocaleString();
});

// ์ดˆ๊ธฐํ™”(AC) ๊ธฐ๋Šฅ
const reset = document.querySelector(".clear");

reset.addEventListener("click", (e) => {
    previous.value = "";
    current.value = "";
});

// delete ๊ธฐ๋Šฅ
const del = document.querySelector(".del");

del.addEventListener("click", (e) => {
    current.value = current.value.slice(handleNumber.length, -1);
});
profile
boma91@gmail.com
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€