
vanilla JS ๋ฅผ ํตํด ๊ณ์ฐ๊ธฐ๋ฅผ ๊ตฌํํ๋ผ
clear button (C) ๋ฅผ ๊ตฌํํ๋ผ
Basic operator ( + , - , * , / ) ๋ฅผ ๊ตฌํํ๋ผ
์ฐ์์ ์ธ ๊ณ์ฐ (ex.2 x 2 x 2 x 2 x 2) ์ ๊ตฌํํ๋ผ
calculator๋ผ๋ class๋ฅผ ๊ฐ์ง div์ ๋ด๋ถ์, status class์ div์ ๊ฐ๊ฐ์ ์ซ์๋ฅผ innerText๋ก ๊ฐ์ง number class์ ๊ฐ๊ฐ์ ๊ธฐํธ๋ฅผ innerText๋ก ๊ฐ์ง operator๋ฅผ button์ผ๋ก ์ ์ธํ๋ค. equal๊ณผ clear๋ฅผ class๋ก ๊ฐ๋ button๋ ์์ฑํด์ฃผ์๋ค.
๊ฐ์ฅ ๋จผ์ .calculator์ ์์ grid์์ฑ์ ์ง์ ํ์ฌ ๋ด๋ถ์ ๋ฒํผ์ด ์ ๋ ฌ๋๋๋ก ํ์๋ค.
.calculator {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-gap: 10px;
grid-auto-rows: minmax(100px, auto);
background-color: black;
}
์ดํ ๊ฐ๊ฐ์ ์์ฑ์ ๋ฐ๋ผ ํน์ฑ์ ์ง์ ํด์ฃผ์๋๋ฐ, ๊ทธ์ค ์ฃผ๋ชฉํ ๋งํ ๊ฒ์ .zero์ด๋ค. ์ฌํ ๋ฒํผ๊ณผ๋ ๋ค๋ฅด๊ฒ, ํด๋น ๋ฒํผ์ ๊ฐ๋ก๋ก ๋์นธ์ ์ฐจ์งํ๋๋ฐ, ํด๋น ํน์ฑ์ grid-column: auto / span 2;์ ํตํด ์ง์ ํด์ฃผ์๋ค. ๋ํ, ๋ชจ๋ ๋ฒํผ์ ๋ํด์ ํด๋ฆญ์์ ํ์ด๋ผ์ดํธ ๋๋ ํ์์ ์์ ๊ธฐ ์ํด์
*:focus {
outline: none;
}
์ ์ง์ ํ์ฌ ์ฃผ์๊ณ , ๊ฐ ๋ฒํผ์ด ํด๋ฆญ ๋์์๋, ์๋๋ก ์ด์ง ๋๋ฆฌ๋ ๋๋๊ณผ ์์์ ๋ณํ๋ฅผ ์ฃผ๊ธฐ ์ํ์ฌ ๊ฐ๊ฐ์ ํด๋์ค:active๋ฅผ ์ง์ ํ์ฌ, transform: translateY(1px);์ background-color:๋ฅผ ๊ฐ๊ฐ์ ํด๋์ค์ ์ด์ธ๋ฆฌ๋ ์์์ผ๋ก ๋ฐ๊พธ์ด ์ฃผ์๋ค.
์ด์ ๋ณธ๊ฒฉ์ ์ธ ์๊ตฌ์กฐ๊ฑด์ ์ถฉ์กฑ์ํค๋ ๊ณ์ฐ๊ธฐ๋ฅผ ๋ง๋ค์ด๋ณด๊ฒ ๋ค. ๊ฐ์ฅ ๋จผ์ ,
const status = document.querySelector(".status");
const clear = document.querySelector(".clear");
const number = document.querySelectorAll(".number");
const operator = document.querySelectorAll(".operator");
const equal = document.querySelector(".equal");
๋ฅผ ํตํด ํ์ํ ์์๋ค์ ๋ฏธ๋ฆฌ ์ ์ธํด์ฃผ๊ณ , ์ฌ์ฉํ ๋ณ์๋ ์ ์ธํด์ฃผ์๋ค.
let num1 = 0;
let num2 = 0;
let num = 0;
let wasOp = 0;
let op1 = "";
let op = "";
let opCount = 0;
let result = 0;
ํ์ด์ง์ ๋ก๋์ ํจ๊ป ์คํ๋๋ ํจ์ init()์์๋ ๊ฐ๊ฐ์ ์ซ์๋ฅผ innerText๋ก ๊ฐ๊ณ ์๋ number์ ์ฐ์ฐ์๋ฅผ innerText๋ก ๊ฐ๊ณ ์๋ operator์ ๋ํด์,
number.forEach(function(Item) {
Item.addEventListener("click",handleNumber);
});
operator.forEach(function(Item) {
Item.addEventListener("click", handleOperator);
});
๋ฅผ forEach()๋ฅผ ํตํด ์คํํ์ฌ ๊ฐ๊ฐ ํด๋ฆญ์ ๋ํด ๋์ํ๋ ํจ์๋ฅผ ์ ์ธํด์ฃผ๊ณ , clear ๋ฒํผ๊ณผ equal ๋ฒํผ์ ๋ํด์๋ ํด๋ฆญ์ ๋ํด ๋์ํ๋ ํจ์๋ฅผ ์ ์ธํด์ฃผ์๋ค.
clear.addEventListener("click", handleClear);
equal.addEventListener("click",handleEqual);
๊ฐ๊ฐ์ number์ ๋ํ ํด๋ฆญ์ ๋ฐ์ํ๋ handleNumber()์๋ event.target.innerText๋ฅผ ํตํด ํด๋น ๋ฒํผ์ ์ซ์๋ฅผ value๋ก์ ์ ์ฅํด์ฃผ๊ณ , ํด๋น value๋ฅผ ๋์คํ๋ ์ด์ ์ํฉ์ ๋ฐ๋ผ ๋ณํํ๋๋ก status.innerText๋ฅผ now๋ก ์ ์ฅํด์ฃผ์๋ค. ์ดํ์ 0์ด ์
๋ ฅ๋์๋๋ฐ ์ซ์๋ฅผ ์
๋ ฅ์ 03๊ณผ ๊ฐ์ด ์
๋ ฅ๋๋ ๊ฒ์ ๋ฐฉ์งํ๊ธฐ ์ํด now !== 0์ธ ์กฐ๊ฑด๋ฌธ์ ๋ํด else์๋ status.innerText๋ฅผ value๋ก ๋ฐ๋๋๋ก ํ๊ณ , ์กฐ๊ฑด๋ฌธ์ ํด๋น๋๋ ๊ฒฝ์ฐ์๋ ์ง์ ์ ์
๋ ฅ์ด ์ฐ์ฐ์์๋์ง ์ฌ๋ถ๋ฅผ 1๋ก ํํํ๋ wasOp๋ฅผ ์กฐ๊ฑด๋ฌธ์ผ๋ก ํ์ฌ ๋ง์ฝ ์ด์ ์ ์
๋ ฅ์ด ์กฐ๊ฑด๋ฌธ์ด์๋ ๊ฒฝ์ฐ status.innerText๋ฅผ ๋ฐ๋ก value๋ก ๋ฐ๊พธ์ด์ฃผ๊ณ , wasOp๊ฐ์ 0์ผ๋ก ๋ฐ๊ฟ์ฃผ์๋ค. ์๋ ๊ฒฝ์ฐ, status.innerText = status.innerText + value๋ฅผ ํตํด ์ด์ ์ ๊ฐ์ ๋ค์ ๊ฐ์ผ๋ก ์ด์ด์ง๋๋ก ํ์๋ค.
clear ๊ธฐ๋ฅ์ ํ๋ clear ๋ฒํผ์ ํด๋ฆญ์ ๋ฐ์ํ๋ handleClearํจ์๋ result,wasOp,opCount๋ฅผ ๋ชจ๋ 0์ผ๋ก ๋ฐ๊ฟ์ค ํ์, status.innerText ๋ํ 0์ผ๋ก ๋ฐ๊พธ์ด ์ถ๋ ฅํด์ฃผ์๋ค.
operator๋ฅผ ํด๋ฆญํ ๊ฒ์ ๋ํ ๋ฐ์ํ๋ handleOperatorํจ์๋ ๊ฐ์ฅ ๋จผ์ ์ด์ ์
๋ ฅ์ด ์ฐ์ฐ์์์์ ์๋ฏธํ๋ wasOp๋ฅผ 1๋ก ์ง์ ํ๊ณ , clear๊ฐ ๋๋ฆฌ์ง ์๊ณ ๋ช๋ฒ์ ์ฐ์ฐ์๊ฐ ์
๋ ฅ๋์๋์ง ์ฌ๋ถ๋ฅผ ์ ์ฅํ๋ opCount์ ๊ฐ์ ๋ฐ๋ผ ๋ค๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋๋ก ํ์๋ค. ๋จผ์ ์ฒ์์ผ๋ก ์
๋ ฅ๋ ์ฐ์ฐ์์ธ ๊ฒฝ์ฐ, num1์ parseInt(status.innerText)๋ฅผ ์ ์ฅํ๊ณ , op1์๋ event.target.innerText๋ฅผ ์ ์ฅํด์ค ํ์ ๋ค์ ์
๋ ฅ์ ๊ธฐ๋ค๋ฆฌ๋๋ก ํ์๋ค. opCount๊ฐ 1์ธ ๊ฒฝ์ฐ์๋, num2๋ฅผ parseInt(status.innerText)๋ก ์ ์ฅํ๊ณ , op๋ฅผ event.target.innerText๋ก ์ ์ฅํด์ค ํ์, op1์ ๊ฐ์ด +,-,*,/ ์ค ์ด๋ค ๊ฒ์ด๋์ ๋ฐ๋ผ result๋ฅผ ํด๋น ์ฐ์ฐ์๋ก num1๊ณผ num2๋ก ๊ณ์ฐํ ๊ฒฐ๊ณผ๊ฐ์ ์ ์ฅํ๋๋ก ํ์๋ค. opCount๊ฐ 2 ์ด์์ธ ๊ฒฝ์ฐ์๋ ๊ณตํต์ ์ผ๋ก num์ status.innerText๋ฅผ ์ ์ฅํด์ฃผ๊ณ , result์ num์ op์ ๋ฐ๋ผ ๊ณ์ฐํ ๊ฒฐ๊ณผ๋ฅผ result๋ก ์ต์ ํ ์์ผ์ฃผ๊ณ , ๊ทธ ํ์ event.target.innerText๋ฅผ op๋ก ์ต์ ํ ์์ผ์ฃผ์๋ค. opCount์ ๋ฐ๋ฅธ ์กฐ๊ฑด๋ฌธ์ ๊ฑฐ์น ์ดํ์๋ ๊ณตํต์ ์ผ๋ก opCount = opCount + 1์ ํด์ฃผ์ด ๊ฐ์ ์กฐ๊ฑด์ ์ค๋ณต์ผ๋ก ํต๊ณผํ์ง ์๋๋ก ์ฒ๋ฆฌํ์๋ค.
equal (=) ๋ฒํผ์ ํด๋ฆญํ ์ด๋ฒคํธ์ ๋ํ ๋ฐ์์ผ๋ก ๋์ํ๋ handleEqual ํจ์๋ ๊ธฐ๋ณธ์ ์ผ๋ก handleOperator์ ๋์๊ณผ ๊ฐ์ ๋ฉ์ปค๋์ฆ์ผ๋ก ๋์ํ๋ค. ์ฐจ์ด์ ์ด ์๋ค๋ฉด, opCount๊ฐ 0์ธ ๊ฒฝ์ฐ์๋ ํน์ ์ฐ์ฐ์ ์งํํ ์ ์๊ธฐ ๋๋ฌธ์ handleClear ํจ์๊ฐ ์๋ํ๊ฒ ํ๊ณ , ์ด์ธ์ ์ฐ์ ์์๋ op๋ฅผ ์ ์ฅํ๋ ๊ฒ์ ์ ์ธํ๊ณ ๋ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์๋ํ๊ฒ ํ์๋ค.
.gif)