๐Ÿช„ ๊ฐ„๋‹จํ•œ ๊ธฐ๋Šฅ ๊ตฌํ˜„. Calculator

[meษช]ยท2022๋…„ 1์›” 7์ผ
0

5. Mini Toy Projects

๋ชฉ๋ก ๋ณด๊ธฐ
12/17

[์ƒˆ ์ฑ… ๊ฐ•์ขŒ]์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ•์ขŒ 4์žฅ - ๊ณ„์‚ฐ๊ธฐ(ES2021)

๐Ÿ’Ž "์ˆœ์„œ๋„๋ฅผ ๊ทธ๋ฆฌ๋Š” ๊ฒŒ ์šฐ์„ ์ด๋‹ค." ์ฒ˜์Œ ๋“ฃ๋Š”๋‹น... 
   (์ฒจ์–ธ : ์ˆœ์„œ๋„ ๋˜ํ•œ ๊ฐœ๋ฐœ์— ์„ ํ–‰ํ•œ ๊ณ„ํš์˜ ์ข…๋ฅ˜ ์ค‘ ํ•˜๋‚˜์ด๊ณ , ๋ณธ๊ฒฉ ๊ฐœ๋ฐœ ์ „์— ์„ค๊ณ„ ๋ฌธ์„œ๋ฅผ ์ž‘์„ฑํ•ด์„œ ๋™๋ฃŒ์™€ ๋ฆฌ๋ทฐ๋ฅผ ํ•˜๋ฉด ์ข‹๋‹ค.
          '์ฝ”๋”ฉ์€ ๊ทธ๋ƒฅ ์งœ๋ฉด ๋˜๋‹ˆ๊นŒ ๊ฐœ๋ฐœ๋ณด๋‹ค ์„ค๊ณ„๊ฐ€ ๋” ์ค‘์š”ํ•˜๋‹ค'๊ณ  ํ•˜๊ธฐ๋„ ํ•˜๋Š”๋ฐ ๋‚œ... ์ฝ”๋”ฉ๋„ ์ž˜ ๋ชป ์งœ๋Š”๋ฐ์—...)
   ์‚ฌ๋žŒ์˜ ํ–‰๋™์„ ๊ณ ๋ คํ•˜์—ฌ ์ž‘์„ฑํ•œ ์ˆœ์„œ๋„๋ฅผ 'JS์— ๋งž๊ฒŒ' ์ˆ˜์ •ํ•˜๊ณ  ์ชผ๊ฐœ์•ผ ํ•œ๋‹ค. 

โœ”๏ธ (๊ณ„์‚ฐ๊ธฐ ์ˆซ์ž ๋ถ€๋ถ„) ์ค‘๋ณต๋œ ํ•จ์ˆ˜๋ฅผ ์ค„์ด๋Š” ๊ณผ์ • feat. ๊ณ ์ฐจํ•จ์ˆ˜

๊ณ„์‚ฐ๊ธฐ๋Š” ๐ŸŸฅ + โญ•๏ธ + ๐Ÿ”ด (์ฒซ ๋ฒˆ์งธ๋กœ ์ž…๋ ฅ๋œ ์ˆซ์ž + ์—ฐ์‚ฐ์ž + ๋‘ ๋ฒˆ์งธ๋กœ ์ž…๋ ฅ๋œ ์ˆซ์ž) ๋กœ ์ด๋ฃจ์–ด์ง€๋‹ˆ๊นŒ

// โ†“
let firstNum = '';
let operator = '';
let secondNum = '';

const $operator = document.querySelector('#operator');
const $result = document.querySelector('#result');
// โ†‘ ๊ณ ์ •

// โ†“ STEP 1
document.querySelector('#num-0').addEventListener('click', () => {
	if (operator) { // ์—ฐ์‚ฐ์ž๊ฐ€ ์žˆ๋‹ค๋ฉด
    secondNum += '0'; // ๋‘ ๋ฒˆ์งธ ์ˆซ์ž์— 0์„ ๋„ฃ๊ณ  ~
    } else {
    firstNum += '0';
    }
    $result.value += '0'}); // JS์—์„œ ๋ณ€์ˆ˜๋กœ ๋ถˆ๋Ÿฌ์™”์œผ๋ฉด HTML์— ๊ทธ๋ ค๋‚ด๋Š” ๊ฒƒ ์žŠ์ง€ ๋ง๊ธฐ
// โ†‘ ์ด๋ ‡๊ฒŒ ์ˆซ์ž๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ์ ์œผ๋ฉด 0๋ถ€ํ„ฐ 9๊นŒ์ง€ (#num-0๋ถ€ํ„ฐ #num0=-9๊นŒ์ง€)10๋ฒˆ์„ ์ ์–ด์ค˜์•ผ ํ•˜๋Š”๋ฐ, ์ด๋ ‡๊ฒŒ ํ•จ์ˆ˜์—์„œ ์ค‘๋ณต๋˜๋Š” ๋ถ€๋ถ„์„ ํ•˜๋‚˜๋กœ ๋งŒ๋“ค ๋•Œ ๋‹ค๋ฅธ ๋ถ€๋ถ„์ด ์žˆ๋‹ค๋ฉด ๊ทธ๊ฑด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋นผ์ค€๋‹ค. ์ด ๊ฒฝ์šฐ๋Š” 'number'
// ----------------------------------------------------------------
// โ†“ STEP 2
document.querySelector('#num-0').addEventListener('click', (number) => {
  if (operator) {
    secondNum += number;
  } else {
    firstNum += number;
  }
  $result.value += number});
// โ†‘ ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด number ๋•๋ถ„์— ๋™์ผํ•œ ์ฝ”๋“œ๊ฐ€ ๋ฐ˜๋ณต๋˜๋ฏ€๋กœ ๋ฐ˜๋ณต๋˜๋Š” ๋ถ€๋ถ„์„ ๋ณ€์ˆ˜๋กœ ์ €์žฅํ•œ๋‹ค. ์ด ๊ฒฝ์šฐ๋Š” const onClickNumber
// ----------------------------------------------------------------
// โ†“ STEP 3
const onClickNumber = (number) => {
  if (operator) {
    secondNum += number;
  } else {
    firstNum += number;
  }
  $result.value += number};

document.querySelector('#num-0').addEventListener('click', onClickNumber('0'));
// โ†‘ ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด const์˜ ๊ฒฐ๊ณผ ๊ฐ’์ด undefined์—ฌ์„œ ์•„๋ž˜ onClickNumber ์ž๋ฆฌ์— undefined๊ฐ€ ๋“ค์–ด๊ฐ€๋ฏ€๋กœ return๋ฌธ ์ถ”๊ฐ€ํ•œ๋‹ค.
// ----------------------------------------------------------------
// โ†“ STEP 4
const onClickNumber = (number) => {
  if (operator) {
    secondNum += number;
  } else {
    firstNum += number;
  }
  $result.value += number
	return () => {
    };
};

document.querySelector('#num-0').addEventListener('click', onClickNumber('0'));
// โ†‘ ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด return๋ฌธ ๋•๋ถ„์— undefined๋Š” ๋ฉดํ–ˆ์ง€๋งŒ ์—ฌ์ „ํžˆ ๋นˆ ๊ฐ’์ด onClickNumber ์ž๋ฆฌ์— ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜๋ฏ€๋กœ ๊ฐ’์ด ํ•„์š”ํ•œ if๋ฌธ์„ return๋ฌธ ์•ˆ์œผ๋กœ ๋„ฃ๋Š”๋‹ค.
// ----------------------------------------------------------------
// โ†“ STEP 5
const onClickNumber = (number) => {
  return () => {
    if (operator) {
    secondNum += number;
	  } else {
    firstNum += number;
  	}
  	$result.value += number
  };
};

document.querySelector('#num-0').addEventListener('click', onClickNumber('0'));
// โ†‘ ์ด๋•Œ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์—์„œ {}์™€ return์ด ํ•จ๊ป˜ ์žˆ์œผ๋ฉด ์ƒ๋žต์ด ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ
// ----------------------------------------------------------------
// โ†“ STEP 6
const onClickNumber = (number) => () => {
    if (operator) {
    secondNum += number;
	  } else {
    firstNum += number;
  	}
  	$result.value += number
};

document.querySelector('#num-0').addEventListener('click', onClickNumber('0'));
// โ†‘ ํ•จ์ˆ˜๊ฐ€ ํ•จ์ˆ˜๋ฅผ returnํ•˜๋Š” ๊ณ ์ฐจํ•จ์ˆ˜ (high order function) ์™„์„ฑ
// ----------------------------------------------------------------
// โ†“ STEP 7
const onClickNumber = (number) => (event) => {
    if (operator) {
    secondNum += number;
	  } else {
    firstNum += number;
  	}
  	$result.value += number
};

document.querySelector('#num-0').addEventListener('click', onClickNumber('0'));
// โ†‘ onClickNumber๊ฐ™์€ event๋ฅผ ์“ฐ๋ฉด ๋ธŒ๋ผ์šฐ์ €๊ฐ€ event๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ event๋ผ๋Š” ์ธ์ˆ˜๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•œ๋‹ค. ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ž๋™์œผ๋กœ ์••๋ ฅํ•˜๋ฏ€๋กœ STEP 6์—์„œ ๋ฉˆ์ถ”์–ด๋„ ๋œ๋‹ค.
// ===============================================================
// ๐Ÿ‘‡ ๊ณ ์ฐจํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•œ ํ•จ์ˆ˜ ์ค‘๋ณต ์ œ๊ฑฐ
let firstNum = '';
let operator = '';
let secondNum = '';

const $operator = document.querySelector('#operator');
const $result = document.querySelector('#result');
const onClickNumber = (number) => (event) => {
    if (operator) {
    secondNum += number;
	  } else {
    firstNum += number;
  	}
  	$result.value += number
};

document.querySelector('#num-0').addEventListener('click', onClickNumber('0'));
document.querySelector('#num-0').addEventListener('click', onClickNumber('1'));
document.querySelector('#num-0').addEventListener('click', onClickNumber('2'));
document.querySelector('#num-0').addEventListener('click', onClickNumber('3'));
document.querySelector('#num-0').addEventListener('click', onClickNumber('4'));
document.querySelector('#num-0').addEventListener('click', onClickNumber('5'));
document.querySelector('#num-0').addEventListener('click', onClickNumber('6'));
document.querySelector('#num-0').addEventListener('click', onClickNumber('7'));
document.querySelector('#num-0').addEventListener('click', onClickNumber('8'));
document.querySelector('#num-0').addEventListener('click', onClickNumber('9'));
// ๐Ÿ‘†

โœ”๏ธ (๊ณ„์‚ฐ๊ธฐ ์ˆซ์ž ๋ถ€๋ถ„) ์ค‘๋ณต๋œ ํ•จ์ˆ˜๋ฅผ ์—†์• ๋Š” ๊ณผ์ • feat. ๊ณ ์ฐจํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ 

// ๐Ÿ‘‡
let firstNum = '';
let operator = '';
let secondNum = '';

const $operator = document.querySelector('#operator');
const $result = document.querySelector('#result');
const onClickNumber = (event) => {
    if (operator) {
    secondNum += event.target.textContent;
	  } else {
    firstNum += event.target.textContent;
  	}
  	$result.value += event.target.textContent;
};

document.querySelector('#num-0').addEventListener('click', onClickNumber);
document.querySelector('#num-0').addEventListener('click', onClickNumber);
document.querySelector('#num-0').addEventListener('click', onClickNumber);
document.querySelector('#num-0').addEventListener('click', onClickNumber);
document.querySelector('#num-0').addEventListener('click', onClickNumber);
document.querySelector('#num-0').addEventListener('click', onClickNumber);
document.querySelector('#num-0').addEventListener('click', onClickNumber);
document.querySelector('#num-0').addEventListener('click', onClickNumber);
document.querySelector('#num-0').addEventListener('click', onClickNumber);
document.querySelector('#num-0').addEventListener('click', onClickNumber);
// ๐Ÿ‘†

โœ”๏ธ (๊ณ„์‚ฐ๊ธฐ ์ˆซ์ž ๋ถ€๋ถ„) secondNum์ด fistNum์— ์—ฐ์†ํ•ด์„œ ์ฐํžˆ๋Š” ์ƒํ™ฉ ๋ฐฉ์ง€ํ•˜๊ธฐ

1+2๋ฅผ ๋ˆ„๋ฅด๋ฉด 12๊ฐ€ ๋˜๋Š” ์ƒํ™ฉ์„ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด onClickNumber์— if๋ฌธ ์ถ”๊ฐ€
"์—ฐ์‚ฐ์ž๋Š” ์žˆ๋Š”๋ฐ ๋‘ ๋ฒˆ์งธ ์ˆซ์ž๊ฐ€ ์—†์œผ๋ฉด ๊ฒฐ๊ณผ์ฐฝ ํ™”๋ฉด์„ ๋น„์›Œ๋ผ. (์™œ๋ƒํ•˜๋ฉด ๋‘ ๋ฒˆ์งธ ์ˆซ์ž ์ง€๊ธˆ ์ž…๋ ฅํ•  ๊ฑฐ๋‹ˆ๊นŒ.)"

const onClickNumber = (event) => {
    if (operator) {
      if (!second) {
        $result.value = '';
      }
    secondNum += event.target.textContent;
	  } else {
    firstNum += event.target.textContent;
  	}
  	$result.value += event.target.textContent;
};

โœ”๏ธ (๊ณ„์‚ฐ๊ธฐ ์ˆซ์ž ๋ถ€๋ถ„) ์ค‘์ฒฉ๋˜๋Š” if๋ฌธ ์—†์• ๊ธฐ

// โ†“ STEP 1. if๋ฌธ ๋‹ค์Œ์— ๋‚˜์˜ค๋Š” ๊ณตํ†ต๋œ ์ ˆ์ฐจ๋ฅผ ๊ฐ ๋ถ„๊ธฐ์  ๋‚ด๋ถ€์— ๋„ฃ๋Š”๋‹ค.
const onClickNumber (event) => {
  if (operator) {
    if (!second) {
      $result.value = '';
    }
    secondNum += event.target.textContent;
    $result.value += event.target.textContent;
  } else {
    fistNum += event.target.textContent;
    $result.value += event.target.textContent;
  }
};
// โ†‘
// ----------------------------------------------------------------
// โ†“ STEP 2. ๋ถ„๊ธฐ์ ์—์„œ ์งง์€ ์ ˆ์ฐจ๋ถ€ํ„ฐ ์‹คํ–‰ํ•˜๋„๋ก if๋ฌธ์„ ์ž‘์„ฑํ•œ๋‹ค. ์ฆ‰ ๋” ์งง์€ else ๋’ค code๊ฐ€ ์œ„๋กœ ๊ฐ€๊ฒŒ, ๊ธด code๊ฐ€ ์•„๋ž˜๋กœ ๊ฐ€๊ฒŒ ๋ณ€๊ฒฝํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ณ€๊ฒฝํ•  ๋• ์ธ์ž์— ๋Š๋‚Œํ‘œ๋ฅผ ๋ถ™์ด๋ฉด ๋œ๋‹ค. (์ด ๊ฒฝ์šฐ๋Š” operator์— ๋Š๋‚Œํ‘œ๋ฅผ ๋ถ™์ธ๋‹ค.) 
const onClickNumber (event) => {
  if (!operator) {
    fistNum += event.target.textContent;
    $result.value += event.target.textContent;
  } else {
    if (!second) {
      $result.value += '';
    }
    secondNum += event.target.textContent;
    $result.value += event.target.textContent;
  }
};
// โ†‘
// ----------------------------------------------------------------
// โ†“ STEP 3. ์งง์€ ์ ˆ์ฐจ๊ฐ€ ๋๋‚œ ๊ตฌ๊ฐ„์— return (ํ•จ์ˆ˜ ๋‚ด๋ถ€)๋‚˜ break (for๋ฌธ ๋‚ด๋ถ€)๋กœ ์ค‘๋‹จํ•œ๋‹ค.
const onClickNumber (event) => {
  if (!operator) {
    fistNum += event.target.textContent;
    $result.value += event.target.textContent;
    return;
  } else {
    if (!second) {
      $result.value += '';
    }
    secondNum += event.target.textContent;
    $result.value += event.target.textContent;
  }
};
//
// ----------------------------------------------------------------
// โ†“ STEP 4. if๋ฅผ return์œผ๋กœ ์ค‘๋‹จํ•˜๋ฉด else๊ฐ€ ํ•„์š” ์—†๋‹ค.
const onClickNumber (event) => {
  if (!operator) {
    fistNum += event.target.textContent;
    $result.value += event.target.textContent;
    return;
  }
  if (!second) {
    $result.value += '';
  }
    secondNum += event.target.textContent;
    $result.value += event.target.textContent;
  }
};
// โ†‘
// ===============================================================
// ๐Ÿ‘‡ ์ตœ์ข… code
let firstNum = '';
let operator = '';
let secondNum = '';

const $operator = document.querySelector('#operator');
const $result = document.querySelector('#result');

const onClickNumber = (event) => {
    if (!operator) {
        firstNum += event.target.textContent;
        $result.value += event.target.textContent;
        return;
    }
    if (!secondNum) {
        $result.value = '';
    }
    secondNum += event.target.textContent;
    $result.value += event.target.textContent;
};

document.querySelector('#num-0').addEventListener('click', onClickNumber);
document.querySelector('#num-1').addEventListener('click', onClickNumber);
document.querySelector('#num-2').addEventListener('click', onClickNumber);
document.querySelector('#num-3').addEventListener('click', onClickNumber);
document.querySelector('#num-4').addEventListener('click', onClickNumber);
document.querySelector('#num-5').addEventListener('click', onClickNumber);
document.querySelector('#num-6').addEventListener('click', onClickNumber);
document.querySelector('#num-7').addEventListener('click', onClickNumber);
document.querySelector('#num-8').addEventListener('click', onClickNumber);
document.querySelector('#num-9').addEventListener('click', onClickNumber);
// ๐Ÿ‘†

โœ”๏ธ (๊ณ„์‚ฐ๊ธฐ ์—ฐ์‚ฐ์ž ๋ถ€๋ถ„) ์ค‘๋ณต๋œ ํ•จ์ˆ˜๋ฅผ ์ค„์ด๋Š” ๊ณผ์ • (๊ฑฐ์˜ ๋™์ผ)

// STEP 1.
document.querySelector('#plus').addEventListener('click', () => {
    if (firstNum) {
        operator = '+'
        $operator.value = '+';
    } else {
        alert('์ˆซ์ž๋ฅผ ๋จผ์ € ์ž…๋ ฅํ•˜์„ธ์š” :>');
    }
});
// ----------------------------------------------------------------
// STEP 2.
document.querySelector('#plus').addEventListener('click', (op) => {
    if (firstNum) {
        operator = op;
        $operator.value = op;
    } else {
        alert ('์ˆซ์ž๋ฅผ ๋จผ์ € ์ž…๋ ฅํ•˜์„ธ์š” :>');
    }
})
// ----------------------------------------------------------------
// STEP 3.
const onClickOperator = (op) => {
        if (firstNum) {
            operator = op;
            $operator.value = op;
        } else {
            alert ('์ˆซ์ž๋ฅผ ๋จผ์ € ์ž…๋ ฅํ•˜์„ธ์š” :>');
        }
    }
document.querySelector('#plus')addEventListener('click,' onClickOperator('+'));
// ----------------------------------------------------------------
// STEP 4.
const onClickOperator = (op) => {
    if (firstNum) {
        operator = op;
        $operator.value = op;
    } else {
        alert ('์ˆซ์ž๋ฅผ ๋จผ์ € ์ž…๋ ฅํ•˜์„ธ์š” :>');
    }
    return () => {
    };
}
document.querySelector('#plus').addEventListener('click',onClickOperator('+'));
// ----------------------------------------------------------------
// STEP 5.
const onClickOperator = (op) => {
    return () => {
        if (firstNum) {
            operator = op;
            $operator.value = op;
        } else {
            alert ('์ˆซ์ž๋ฅผ ๋จผ์ € ์ž…๋ ฅํ•˜์„ธ์š” :>');
        };
    };
};
document.querySelector('#plus').addEventListener('click',onClickOperator('+'));
// ----------------------------------------------------------------
// STEP 6.
const onClickOperator = (op) => () => {
    if (firstNum) {
        operator = op;
        $operator.value = op;
    } else {
        alert ('์ˆซ์ž๋ฅผ ๋จผ์ € ์ž…๋ ฅํ•˜์„ธ์š” :>');
    };
};
document.querySelector('#plus').addEventListener('click' onClickOperator('+'));
// ===============================================================
// ๐Ÿ‘‡
const onClickOperator = (op) => {
    return () => {
        if (firstNum) {
            operator = op;
            $operator.value = op;
        } else {
            alert ('์ˆซ์ž๋ฅผ ๋จผ์ € ์ž…๋ ฅํ•˜์„ธ์š” :>');
        };
    };
};
document.querySelector('#plus').addEventListener('click',onClickOperator('+'));
document.querySelector('#minus').addEventListener('click', onClickOperator('-'));
document.querySelector('#divide').addEventListener('click', onClickOperator('/'));
document.querySelector('#multiply').addEventListener('click', onClickOperator('*'));
// ๐Ÿ‘†

๐Ÿ™‡ the source of this content

ZeroCho TV

profile
๋Š๋ ค๋„ ํ•  ๊ฑฐ์•ผ.......

0๊ฐœ์˜ ๋Œ“๊ธ€