์ค์ ๊ณ์ฐ๊ธฐ๊ฐ ๊ตฌ๋๋๋ ๊ฒ์ฒ๋ผ ๋ง๋ค๊ธฐ!!
๊ณ์ฐ๊ธฐ,,, ๋ ์ด๋ ๊ฒ ์ด๋ ต๊ณ ํ๋ค๊ฒ ์ฌ๋ ์์ด์๊ตฌ๋,,
๋ ๋๋ฌด ์ฝ๊ฒ ์ฐ๊ณ ์์์ด,, ๋ฏธ์๐ฅบ๐ฅบ
๐ค ์์ ํ html ํ์ผ ํ๋จ์ script ์ถ๊ฐํ๊ธฐ
<script type="text/javascript" src="src/script.js"></script>
๐ค
.calculator
๋.calculator
์์.btn
์ ํํ๊ณ , current-display ๋ current-history ๋ ์ด์ ๊ณ์ฐ๋ด์ญ ์์ญ ์ ํํ๊ธฐ
const calculator = document.querySelector('.calculator');
const buttons = calculator.querySelector('.btn');
const display = document.querySelector(".current-display");
const current_display = document.querySelector(".current-history");
const old_display = document.querySelector(".history");
๐ค ๊ณ์ฐํ๋ ํจ์๋ง๋ค๊ธฐ
function calculate(n1, operator, n2) {
let result = 0;
if (operator === "+") {
result = Number(n1) + Number(n2);
} else if (operator === "โ") {
result = Number(n1) - Number(n2);
} else if (operator === "ร") {
result = Number(n1) * Number(n2);
} else if (operator === "รท") {
result = Number(n1) / Number(n2);
}
return String(result);
}
// ํ์ดํ ํจ์๋ก ๋ง๋ค๊ธฐ
const calculate = (n1, operator, n2) => {
let result = 0;
if (operator === '+') {
result = Number(n1) + Number(n2);
} else if (operator === 'โ') {
result = Number(n1) - Number(n2);
} else if (operator === 'ร') {
result = Number(n1) * Number(n2);
} else if (operator === 'รท') {
result = Number(n1) / Number(n2);
}
return String(result);
}
+, -, x, / ๋ฅผ ์๋ฌด๋ฆฌ ํด๋ ์๋๋ค.. ์ ์๋์ง? ํ๊ณ ์๊ฐํด๋ณด๋
๋ด๊ฐ ์ ์ด์ ๊ณ์ฐํ๋ ํจ์๋ฅผ ๋ง๋ค ๋ operator ๋ฅผ ์ด์ํ ํน์๋ฌธ์๋ก ์ง์ ํด๋๊ณ ํค๋ณด๋์ ์๋ x๋ / ์ด๋ฐ๊ฑธ ๋ฃ๊ณ ์์๋ค..
์ง์ง ๋๋ฌด ๋ฉ์ฒญํ ์ค์.. ํ์ง๋ง์
firstNum, previousKey, previousNum ๋ฑ ๋ณ์๋ฅผ ์ง์ ํ๊ณ
const display = document.querySelector(".current-display"); // calculator__display ์๋ฆฌ๋จผํธ์, ๊ทธ ์์ ์๋ฆฌ๋จผํธ์ ์ ๋ณด๋ฅผ ๋ชจ๋ ๋ด๊ณ ์์ต๋๋ค.
const current_display = document.querySelector(".current-history");
const old_display = document.querySelector(".history");
buttons.addEventListener('click', function(event) {
const target = event.target; // ํด๋ฆญ๋ html ์๋ฆฌ๋จผํธ์ ์ ๋ณด ์ ์ฅ
const action = target.classList[0] // ํด๋ฆญ๋ html ์๋ฆฌ๋จผํธ์ ํด๋์ค ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ
const buttonContent = target.textContent; // ํด๋ฆญ๋ html ์๋ฆฌ๋จผํธ์ ํ
์คํธ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ // ์๋ก ์
๋ ฅํ ํค ๊ธฐ์ตํ๊ธฐ
if (target.matches("button")) { // ์ฃผ์ด์ง ์ ํ์๋ ์์์ ์ผ์นํ๋์ง ์ฌ๋ถ ํ์ธํ๊ธฐ //target ์์๊ฐ <button> ์ธ์ง ํ์ธํด ์ผ์นํ๋ฉด true ๋ฐํ
if(action === "number") {
if(display.textContent === "0") { // display์ text ๊ฐ 0์ด๋ฉด?
// display.textContent = buttonContent; ๋ก ํ๊ณ
// current_display ์ ํด๋ฆญํ number ๋ฒํผ ๋ณด์ด๊ฒํ๊ณ
// currentHIstory ์ ํ์ฌ current_display๊ฐ ์ ์ฅํ๊ธฐ
} else if(previousKey === "operator") { // ์ด์ ์ ํค๊ฐ ์ฐ์ฐ์๋ฉด?
// display.textContent = buttonContent; ๋ก ํ๊ณ
// ๋ง์ฝ ์ด์ ํค๊ฐ operator๋ผ๋ฉด. current_display ์ ํด๋ฆญํ number ๊ฐ์ ์ถ๊ฐํ๊ณ
// currentHistory ์ ํ์ฌ current_display ๊ฐ ์ ์ฅ
}
previousKey = "number";
}
if(action === "operator") { // class ์ ๋ณด๊ฐ operator๋ฉด?
// number ๋ฉด ์ฒ๋ผ ๊ฒฝ์ฐ์ ์ ๊ณ์ฐํ๊ธฐ
}
if(action === "decimal") { // class ์ ๋ณด๊ฐ decimal(์์์ ) ์ด๋ฉด?
if (display.textContent.includes(".") === false) { // . ์ ํน์ ๋ฌธ์์ด(includes) ์ด ์๋ค๋ฉด
display.textContent = display.textContent + ".";
} // . ์ถ๊ฐํ๊ธฐ
}
if(action === "clear") { // class ์ ๋ณด๊ฐ clear ๋ฉด?
// display, current_display 0์ผ๋ก ๋ง๋ค์ด์ฃผ๊ณ
// previousKey ๋ clear ๋ก ๋ฐ๊ฟ์ฃผ๊ณ
// ๊ทธ ์ธ์ ํด์ผ๋๋ ๊ฒ๋ค ์ถ๊ฐํ๊ธฐ
}
if(action === "calculate") {
}
}
})
์ํฐ ์ฐ์์ผ๋ก ๋๋ฅด๋ฉด ๋จผ์ ๋๋ ๋ ์ซ์๊ฐ ๋ํด์ง๊ณ ,
= ์ด ๊ณ์ ์ถ๊ฐ๋๋ค.
์ด๊ฑฐ๋.. ์ถํ์ ์์ ํด๋ด์ผ๊ฒ ๋ค. ๋น์ฅ ์ง๋ ๋๋ฌด ํ๋ค์ด..ใ
ใ
กใ
์ด์ ์ git์ ์์ฑ ํ ์ฌ๋ฆฐ์ค ์์๋๋ฐ ์ฌ๋ฆฌ์ง ์์์๋ค;
์์ฑํ git์ ์ฌ๋ฆฌ๊ธฐ ์์
git init
git remote add origin https://๋ด ๊นํ URL
git add
git commit -m ":tada: Begin : ์ด์ ๊ณ์ฐ๊ธฐ"
๊ทธ๋ฌ๊ณ git push origin ํ๋๋ฐ ์๋๊ณ , git push main ํ๋๋ฐ ์๋จ โ fatal: The current branch main has no upstream branch.
ํ์ฌ ๋ธ๋์น(main)๊ฐ ์๊ฒฉ ์ ์ฅ์์ ์ฐ๊ฒฐ๋ ๋ธ๋์น ์์์ ๋ํ๋ด๋ ์ค๋ฅ๋ฅผ ๋ํ๋ด๋๋ฐ โ ์๊ฒฉ ์ ์ฅ์์ ํด๋น ๋ธ๋์น๊ฐ ์๊ฑฐ๋ ์๊ฒฉ ์ ์ฅ์์ ํด๋น ๋ธ๋์น๋ฅผ ํธ์ํ๊ธฐ ์ ์ ๋ก์ปฌ์์ ํด๋น ๋ธ๋์น๋ฅผ ๋จผ์ ์์ฑํ์ง ์์ ๊ฒฝ์ฐ ๋ฐ์ํ๋ค๊ณ ํ๋ค.
git push --set-upstream origin main
โ ํ์ฌ ๋ธ๋์น(main)์ ์๊ฒฉ ์ ์ฅ์(origin)์ upstream ๋ธ๋์น๋ก ํธ์ํ๋ค. โ ํธ์๋ฅผ ์ํํ๊ณ ๋์์ upstream ๋ธ๋์น๋ก ์ค์ ํ๋ค๋ ๋ปgit push origin main
์ผ๋ก ์๊ฒฉ ์ ์ฅ์์ ํธ์ ์ฑ๊ณต๊ณ์ฐ๊ธฐ์์ ๋ชปํ๋ ๊ฒ๋ค์ ์ถ๊ฐ๋ก ์์ ํ๋ ค๊ณ ํ๋๋ฐ
๋ณ์๋ช
๋ค์ด ๋ช
ํํ์ง ์์์ ๋ฏ์ด๋ณด๋๋ฐ ํ์ฐธ๊ฑธ๋ ธ๋ค..
๊ทธ๋์ ๋ณ์๋ช
๋ค ๋ช
ํํ๊ฒ ์์ ํ๊ธฐ
์ฐ์ html์ ์์ฑํ class ์ด๋ฆ๊ณผ ๋์ผํ๊ฒ ๋ง์ถ์
currentDisplay
: current-display
currentHistory
: current-history
operatorBtn
: operatorForAdvanced
๊ทธ๋ฆฌ๊ณ ๊ทธ ์ธ์ ๋ณ์๋ช
๋ค์ ์๊ธฐ ์ฝ๊ฒ ์์ ํ์
operateHistoryDiv
(๊ณ์ฐ๋ ๊ธฐ๋ก์ div ๋ฌถ์) : history class๋ช
์ ๊ฐ์ง div
clickHistory
: ์ด์ ์ ํด๋ฆญํ ๋ฒํผ(๊ธฐ์กด์ currentHistory)
historySpan
: history div ์์ ์์ฑ๋ span
historySpans
: history div ์์ ์์ฑ๋ span ๋ค!
๐ค ๋ด๊ฐ ๊ณ์ฐํ๋ ๋ด์ญ๋ค์ ๊ธฐ์ตํ๊ฒ ํด์ผ๋ผ!!!!
...
const historySpan = document.createElement("span");
historySpan.classList.add("old-history");
...
if(action === "calculate") {
...
// ๋ง์ฝ operatorBtn์ด ์๊ณ previousKey๊ฐ calculate๋ฉด?
if (operatorBtn && previousKey === "calculate") {
//historySpan์ ๋ด์ฉ์ clickHistory ์ ๊ณต๋ฐฑ ์ถ๊ฐํ๊ณ ๊ณ์ฐ ๊ฒฐ๊ณผ๊ฐ ์ถ๊ฐํ๊ธฐ
historySpan.textContent = clickHistory + " " + currentDisplay.textContent;
// operateHistory์ appendChild() ์ฌ์ฉํด์ historySpan ์ถ๊ฐ
operateHistory.appendChild(historySpan);
oldDisplay = operateHistory.textContent;
}
}
historySpan
์ currentDisplay
๋ณ์ ์๋์ ๋์ ๋ ๊ณ์ ํ๊ฐ๋ง ์์ฑ๋๋ค.
๋๋์ฒด ์?? ํ๋ฉด์ ์ฐพ์๋ณด๋
๋ด๊ฐ ๋ณ์์ ์์น๋ฅผ ์๋ชป ์ง์ ํ ๊ฒ!
ํ ๋ฒ๋ง ์์ฑ๋ ์ด์ ๋ ์ด๋ฒคํธ ๋ฆฌ์ค๋ ์ธ๋ถ์ ์์นํด ์๊ธฐ ๋๋ฌธ(ํ์ด์ง ๋ก๋ ์์๋ง ์คํ๋๊ณ , buttons.addEventListener ์ ์ฝ๋ฐฑ ํจ์๊ฐ ์คํ๋ ๋๋ง๋ค ์ฌ์คํ๋์ง ์์)
ํด๋ฆญ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ ๋๋ง๋ค span์ ๋ง๋ค๋ ค๋ฉด
ํด๋น ๋ณ์๋ค์ด ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋ ๋ด๋ถ๋ก ์ด๋ํด์ผ๋๋ค.
๐ค ๊ทผ๋ฐ ๊ทธ span์ ์์๊ฐ ๋ฐ๋๋ก์ฌ์ผ ux์ ์ผ๋ก ๋ง๋ ๊ฒ ๊ฐ์๋ฐ?
๊ธฐ์กด ์ฝ๋ operateHistory.appendChild(historySpan);
์ appendChild
๋ฅผ prepend
๋ก ๋ฐ๊ฟ์ฃผ๊ธฐ
prepend()
: ์ ํํ ์์์ ๋ด์ฉ์ ์์ ์ฝํ
ํธ๋ฅผ ์ถ๊ฐํ๋ ๋ฉ์๋// operateHistory.appendChild(historySpan);
operateHistoryDiv.prepend(historySpan);
๐ค clearํ ๋ history ์์ span๋ค ํ๋ฒ์ ์ ์ง์ฐ๊ณ ์ถ..์ด..
// operateHistoryDiv ์์ ์๋ span ๋ค์ ์ ์ฒด ์ ํํ๋ ๋ณ์ ๋ง๋ค๊ธฐ
const historySpans = operateHistoryDiv.querySelectorAll("span");
console.log(historySpans) // calculate ์ span.old-history ๊ฐ ์ถ๊ฐ๋ ๋ฐฐ์ด ๋ฆฌํด
...
if(action === "clear") {
...
// historySpans์ ์๋ ๋งํผ ๋๋๋ฐ, ๊ทธ๋๋ง๋ค operateHistoryDiv์ ์์ ๋
ธ๋ ์ ๊ฑฐ
historySpans.forEach((el) => {
operateHistoryDiv.removeChild(el);
})
}
๐ค currentDisplay์ ๋ฐ์๋๋ ์์์ history์ ์ ๋ฐ์์ด ์๋์ง..
โ action === "decimal" ์ currentHistory ์ถ๊ฐํ๊ธฐ
currentHistory.textContent = currentHistory.textContent + ".";
๐ค operator ํด๋ฆญํ์ ๋, ์ด๋ฏธ operator๊ฐ ์ ๋ ฅ๋์ด ์์ผ๋ฉด ์ด์ ์ ์ ๋ ฅํ๋ operator ์ญ์ ํ๊ณ ์๋ก ์ ๋ ฅํ operator๋ก ๋์ฒดํ๊ณ ๊ณ์ฐํ๊ธฐ
๐ค ๊ธฐ์กด์ if ๋ฌธ ๋ค์ else if๋ก ์ถ๊ฐํ๋๋ฐ ์ ์๊พธ ์๋๋๊ฑฐ์ผ???
โ ๊ธฐ์กด์ if๋ฌธ์ ์๋ (clickHistory || previousKey === "number") ์ด ์กฐ๊ฑด์ ํด๋น๋๋๊น ์ด๊ฒ ์คํ๋๊ณ ๊ทธ ๋ค์์ else if ๊ฐ ๋ฌด์๋๊ณ ์คํ๋์ง ์๋ ๊ฒ. ์์๋ฅผ ์ ํ์ธํ์!
if (action === "operator") {
...
if (clickHistory && previousKey === "operator") {
const operatorDel = currentHistory.textContent
.split(" ") // ๊ณต๋ฐฑ์ ๊ธฐ์ค์ผ๋ก ๋๋ ์ ๋ฐฐ์ด๋ก ๋ง๋ค๊ธฐ
.slice(0, -1) // ๋ง์ง๋ง operator ์๋ฅด๊ณ
.join(" "); // ์๋ฅธ๊ฑฐ ๋ค์ ๋ถ์ด๋๋ฐ ์ฌ์ด์ ๊ณต๋ฐฑ ์ถ๊ฐํ๊ธฐ
currentHistory.textContent = operatorDel + " " + buttonContent;
clickHistory = currentHistory.textContent;
} else if (clickHistory || previousKey === "number") {
// ๋ง์ฝ clickHistory ์๊ฑฐ๋ ์ด์ ํค๊ฐ number๋ฉด currentHistory ์ ํด๋ฆญํ operator ์ถ๊ฐ
currentHistory.textContent = clickHistory + " " + buttonContent;
// clickHistory ์ ํ์ฌ currentHistory ๊ฐ ์ ์ฅ
clickHistory = currentHistory.textContent;
}
previousKey = "operator";
}
๋์ค์ ์๊ฐ๋๋ฉด ์๋๊ฒ๋ค ๊ผญ ์ถ๊ฐํด๋ณด์๐ฅบ