๊ด€๋ฆฌ ๋ฉ”๋‰ด

Daily Front_Minhhk

[CSS, JS] ๊ณ„์‚ฐ๊ธฐ์— ๊ธฐ๋Šฅ ์ถ”๊ฐ€ํ•˜๊ธฐ! ๋ณธ๋ฌธ

Code๊ฐœ๋ฐœ์ผ์ง€

[CSS, JS] ๊ณ„์‚ฐ๊ธฐ์— ๊ธฐ๋Šฅ ์ถ”๊ฐ€ํ•˜๊ธฐ!

Minhhk 2022. 11. 1. 15:28
๐Ÿ”ฅ ์šฐ์„  ์ตœ์ข…์  ์™„์„ฑ๋ณธ์ด๋‹ค. ๋‚˜์ดํŠธ๋ฉ”์–ด๋Š” ๋ชปํ•˜๊ฒ ๋‹ค~~~ใ…œ ์ง€๊ธˆ๋„ ์ตœ์„ ์„ ๋‹คํ–ˆ๋‹ค?!
๐Ÿ—ฃ ๊ทธ๋ƒฅ ๋ง‰ ์‚ฌ์šฉํ•˜๋Š” ๊ณ„์‚ฐ๊ธฐ์˜ ๋กœ์ง์ด ์ด๋ ‡๊ฒŒ ๋ณต์žกํ•  ์ค„์€ ๋ชฐ๋ž๋‹คใ…Žใ…Ž ์—ญ์‹œ ์‰ฌ์šด๊ฑด ์—†๋‹ค~
๐Ÿ˜ต‍๐Ÿ’ซ  ์˜ค๋Š˜์€ ์ด๋ ‡๊ฒŒ ์ผ๋‹จ๋ฝ์ด ๋˜์—ˆ์ง€๋งŒ ,,, ๋‹ค์‹œ ๋‚ด์ผ์„ ์œ„ํ•ด ๋‹ฌ๋ ค๋ณด์ž๊ณ ! ๊นƒ๊ณผ ๋…ธ๋“œ์— ๋ง›๋ณด๊ธฐ ์ผ ๋“ฏ? ํ•˜๋‹ค

html์€ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ๊ฐ€๊ณ ,

css์—์„œ ๋ฒ„ํŠผ์˜ border-bottom ์ถ”๊ฐ€์™€?? ์•„๋ž˜ active ์ถ”๊ฐ€๋ฅผ ํ•ด์ฃผ์—ˆ๋‹ค.

ํด๋ฆญ์‹œ ํšจ๊ณผ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค!

 

CSS,,

.clear__and__enter > button:active {
  box-shadow: 1px 1px 0 rgba(77, 234, 137, 0.5);
  position: relative;
  top:2px;
}

button:active {
  color: #ffffff;
  font-size: 20px;
  box-shadow: 1px 1px 0 rgba(77, 234, 137, 0.5);
  position: relative;
  top:2px;
  border-bottom: none;
}

.operator:active {
  color:#440d77
}

๊ทธ๋ฆฌ๊ณ ๊ทธ๋ฆฌ๊ณ !!

 

JS,,

const calculator = document.querySelector(".calculator");
const buttons = calculator.querySelector(".calculator__buttons");
const operator = document.querySelector(".calculator__operator");
const display = document.querySelector(".calculator__display--for-advanced");

let firstNum, operatorForAdvanced, previousKey, previousNum;

// n1๊ณผ n2 -> operator
let calculate = (n1, operator, n2) => {
  let result = 0;

  if (operator === "+") {
    result = parseInt(n1) + parseInt(n2); // '+'๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ
  } else if (operator === "-") {
    result = parseInt(n1) - parseInt(n2); // '-'๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ
  } else if (operator === "*") {
    result = parseInt(n1) * parseInt(n2); // '*'๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ
  } else if (operator === "/") {
    result = parseInt(n1) / parseInt(n2); // '/'๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ
  }
  return String(result);
};

// ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ์ž‘๋™ํ•˜๋Š” ํ•จ์ˆ˜
buttons.addEventListener("click", (event) => {
  const target = event.target; // ํด๋ฆญ๋œ HTML ์—˜๋ฆฌ๋จผํŠธ์˜ ์ •๋ณด

  const action = target.classList[0]; // ํด๋ฆญ๋œ HTML ์—˜๋ฆฌ๋จผํŠธ์— ํด๋ ˆ์Šค ์ •๋ณด
  const buttonContent = target.textContent; // ํด๋ฆญ๋œ HTML ์—˜๋ฆฌ๋จผํŠธ์˜ ํ…์ŠคํŠธ ์ •๋ณด

  if (target.matches("button")) {
    if (action === "number") {
      //  0์„ ๋‚˜ํƒ€๋‚ด๊ฑฐ๋‚˜? previousKey์— ์—ฐ์‚ฐ์ž๊ฐ€ ์žˆ๋‹ค๋ฉด? ๊ทธ๊ฒƒํ•˜๋‚˜ ๋งŒ ํ‘œ์‹œ
      if (display.textContent === "0" || previousKey === "operator") {
        // console.log('์ˆซ์ž ' + buttonContent + ' ๋ฒ„ํŠผ');
        display.textContent = buttonContent;
      } else {
        display.textContent += buttonContent;
      }
      // key์— number๋ฅผ ํ• ๋‹น,,, ์ด๊ฑธ ์•ˆํ•ด์„œ ๋‘๋ฒˆ์งธ ์ˆซ์ž์— ์Œ“์ด์ง€ ์•Š์•˜๋‹คใ… ใ… 
      previousKey = "number";
    }

    if (action === "operator") {
      // firstNum์— ์ฒ˜์Œ ๊ฐ’์„ ์ €์žฅ
      firstNum = display.textContent;
      // display.textContent = Number(0);
      // ๋ณ€์ˆ˜ operatorForAdvanced์— ๋ฒ„ํŠผ์˜ ์ปจํ…์ธ (์—ฐ์‚ฐ์ž ๊ธฐํ˜ธ)๋ฅผ ํ• ๋‹น ํ›„ (๋‚˜์ค‘ calculate์—์„œ ์“ฐ์ผ ๊บผ์ž„ ์—ฐ์‚ฐ์ž)
      operatorForAdvanced = buttonContent;
      // key์— ์—ฐ์‚ฐ์ž ํ• ๋‹น
      previousKey = "operator";
    }

    if (action === "clear") {
      display.textContent = "0"; // ํ™”๋ฉด์€ 0 ํ• ๋‹น
      // ๋ณ€์ˆ˜์— ํŠน์ • ๊ฐ’ ํ• ๋‹นํ•˜๋Š” ์ดˆ๊ธฐํ™” ๋ฏธ ์ง„ํ–‰์‹œ, undefined๋กœ ์ดˆ๊ธฐํ™” ๊ฐ€๋Šฅ
      firstNum = undefined;
      operatorForAdvanced = undefined;
      previousKey = undefined;
      previousNum = undefined;
    }

    if (action === "calculate") {
      console.log("๊ณ„์‚ฐ ๋ฒ„ํŠผ");
      // ๊ณ„์‚ฐ๊ธฐ์˜ ํ™”๋ฉด์— calculate ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
      previousNum = display.textContent;
      display.textContent = calculate(
        firstNum,
        operatorForAdvanced,
        previousNum
      );
      previousKey = "calculate";
    }
  }
});

test.script๋Š” ํ†ต๊ณผํ–ˆ๋‹ค!

ํ•˜์ง€๋งŒ,, ๋งˆ์Œ์— ์•ˆ๋“œ๋Š”๊ฒŒ

  if (target.matches('button')) {
    if (action === 'number') {
      if (display.textContent === '0' || previousKey === 'operator') {
        display.textContent = buttonContent;
      } 
      else {
        display.textContent += buttonContent;
      }
    }

์—ฌ๊ธฐ์„œ

if (display.textContent === '0' || previousKey === 'operator') {
        display.textContent = buttonContent;
} 

์ด๋ ‡๊ฒŒ ๋˜๋ฉด 0 ํ‘œ์‹œ๊ฐ€ ๋  ๋•Œ๋Š” 0์„ ๋ˆ„๋ฅผ๋•Œ ๋ณ€ํ•˜์ง€ ์•Š๊ฒŒ 0์„ ํ• ๋‹น ํ•œ๋‹ค,

ํ•˜์ง€๋งŒ ์—ฐ์‚ฐ์ž๋ฅผ ๋ˆ„๋ฅธ ํ›„ ๋‘๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ๋ˆ„๋ฅผ๋•Œ

previousKey์— ์—ฐ์‚ฐ์ž๊ฐ€ ํ• ๋‹น์ด ๋˜์—ˆ๊ธฐ์—,?

๋‘๋ฒˆ์งธ ์ˆซ์ž ๋„ฃ๋Š”์นธ์—๋Š” ํ•˜๋‚˜์”ฉ ๋ฐ–์— ํ• ๋‹น๋˜์ง€ ์•Š๋Š”๋‹ค… ใ…œ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•ด์•ผํ• ๊นŒ….?

 

 ๐Ÿ“Œ ์ง„์งœ,, ๋‹ค ๋„ฃ์–ด ๋†“๊ณ  ๋„˜๋ฒ„๋งŒ ์™œ ์ด์ „ํ‚ค์— ํ• ๋‹น์„ ์•ˆํ–ˆ๊นŒ?!,,, ๋ฐ”๋ณด ๊ฐ™๋‹ค ์œ„ ์Šคํฌ๋ฆฝํŠธ์—๋Š” ์ˆ˜์ • ์™„๋ฃŒ!!
// key์— number๋ฅผ ํ• ๋‹น,,, ์ด๊ฑธ ์•ˆํ•ด์„œ ๋‘๋ฒˆ์งธ ์ˆซ์ž์— ์Œ“์ด์ง€ ์•Š์•˜๋‹คใ… ใ… 
      previousKey = 'number'

 

 

๊ณผ์ œ ์ œ์ถœ๐Ÿ‘๐Ÿป

์ œ์‹œ๋œ git ์—์„œ fork ํ•ด์˜จ๋‹ค. (๋‚ด repository ์— ๋ณต์ œ)

ํŒŒ์ผ ์ˆ˜์ • ํ›„

์ˆ˜์ •ํ•œ ํŒŒ์ผ์„ add file ๋กœ ์—…๋กœ๋“œ ํ•œ๋‹ค

๊ทธ๋ฆฌ๊ณ  pull request ํ•œ๋‹ค (๋‚˜์ค‘ ์—…๋ฌด ๋ณผ ๋•Œ ์—ฌ๋Ÿฟ์‚ฌ๋žŒ์ด ๊ฐ™์ด ํ™œ์šฉํ•˜๊ธฐ์—, ํ•ญ์ƒ ์ž˜ ๊ตฌ๋ถ„์ด ๊ฐ€๋„๋ก ๋ช…์นญ์“ฐ์ž!)