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
๋ฅผ ์ ์ฅํ๋ ๊ฒ์ ์ ์ธํ๊ณ ๋ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์๋ํ๊ฒ ํ์๋ค.