๐ช 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");
const input = document.querySelector(".input");
const previous = document.querySelector(".previous");
const current = document.querySelector(".current");
const btnClick = () => {
for (let i = 0; i < numBtn.length; i++) {
numBtn[i].addEventListener("click", function (e) {
current.value = (current.value += e.target.value).toLocaleString();
});
}
for (let i = 0; i < operator.length; i++) {
operator[i].addEventListener("click", function (e) {
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();
});
const reset = document.querySelector(".clear");
reset.addEventListener("click", (e) => {
previous.value = "";
current.value = "";
});
const del = document.querySelector(".del");
del.addEventListener("click", (e) => {
current.value = current.value.slice(handleNumber.length, -1);
});