Bibi's DevLog ๐Ÿค“๐ŸŽ

[์ธํ”„๋Ÿฐ]Javascript์ž…๋ฌธ - ๊ณ„์‚ฐ๊ธฐ ๋งˆ์ง€๋ง‰ - ํ•œ ์ค„๋กœ ์ž…๋ ฅ๋ฐ›์•„ ์ฒ˜๋ฆฌ ๋ณธ๋ฌธ

ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Javascript ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ

[์ธํ”„๋Ÿฐ]Javascript์ž…๋ฌธ - ๊ณ„์‚ฐ๊ธฐ ๋งˆ์ง€๋ง‰ - ํ•œ ์ค„๋กœ ์ž…๋ ฅ๋ฐ›์•„ ์ฒ˜๋ฆฌ

๋น„๋น„ bibi 2020. 10. 28. 17:57
* ์ด ๊ธ€์€ ์ธํ”„๋Ÿฐ์—์„œ ์ œ๊ณตํ•˜๋Š” ํ˜ธ๋ˆ…์Šค๋‹˜์˜ ์œ ๋ฃŒ ๊ฐ•์˜ '์‰ฝ๊ณ  ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ฐฐ์›Œ๋ณด๋Š” Javascript ์ž…๋ฌธ - ์ฝ”๋“œ์Šค์ฟผ๋“œ ๋งˆ์Šคํ„ฐ์ฆˆ ์ฝ”์Šค ๋ ˆ๋ฒจ1'๋ฅผ ๋“ฃ๊ณ  ๊ณต๋ถ€ํ•˜๋ฉฐ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. ๊ฐ•์˜ ๋‚ด์šฉ์— ๋”ํ•ด, ์ œ๊ฐ€ ํ•„๊ธฐํ•˜๊ณ  ๊ตฌ๊ธ€๋งํ•œ ๋‚ด์šฉ์ด ์ •๋ฆฌ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ดˆ๋ณด์ธ ์ œ๊ฐ€ ์ดํ•ดํ•œ ๋Œ€๋กœ ์ •๋ฆฌํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋ถ€์ •ํ™•ํ•œ ๋‚ด์šฉ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Œ์„ ๊ฐ์•ˆํ•ด ์ฃผ์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ๋‚ด์šฉ์„ ์ •๋ฆฌํ•œ ๊ธ€์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ œ๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ธํ”„๋Ÿฐ ์ธก ๋‹ต๋ณ€์„ ๋ฐ›์€ ์ ์ด ์žˆ์œผ๋‚˜(https://bibi6666667.tistory.com/37), ์ €์ž‘๊ถŒ์ƒ ๋ฌธ์ œ๊ฐ€ ๋œ๋‹ค๋ฉด ๋ณดํ˜ธ ๋ชจ๋“œ(๋น„๊ณต๊ฐœ)๋กœ ์ „ํ™˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

ํ•™์Šต ๋ชฉํ‘œ

  • ๋ฌธ์ž์—ด์˜ ์กฐ์ž‘๊ณผ ๋ฆฌ์ŠคํŠธ์˜ ์‚ฌ์šฉ์„ ๊ฒฝํ—˜ํ•œ๋‹ค.

์š”๊ตฌ์‚ฌํ•ญ

  • ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋ฌธ์ž์—ด ๊ฐ’์— ๋”ฐ๋ผ ์‚ฌ์น™์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ณ„์‚ฐ๊ธฐ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค.
  • ๋ฌธ์ž์—ด ๊ณ„์‚ฐ๊ธฐ๋Š” ์‚ฌ์น™์—ฐ์‚ฐ์˜ ๊ณ„์‚ฐ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ์•„๋‹Œ ์ž…๋ ฅ ๊ฐ’์— ๋”ฐ๋ผ ๊ณ„์‚ฐ ์ˆœ์„œ๊ฐ€ ๊ฒฐ์ •๋œ๋‹ค. ์ฆ‰, ์ˆ˜ํ•™์—์„œ๋Š” ๊ณฑ์…ˆ, ๋‚˜๋ˆ—์…ˆ์ด ๋ง์…ˆ, ๋บ„์…ˆ ๋ณด๋‹ค ๋จผ์ € ๊ณ„์‚ฐํ•ด์•ผ ํ•˜์ง€๋งŒ ์ด๋ฅผ ๋ฌด์‹œํ•œ๋‹ค.
  • ์˜ˆ๋ฅผ ๋“ค์–ด "2 + 3 * 4 / 2"์™€ ๊ฐ™์€ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ 2 + 3 * 4 / 2 ์‹คํ–‰ ๊ฒฐ๊ณผ์ธ 10์„ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.
  • ์ž…๋ ฅ์€ ํ…์ŠคํŠธ๋กœ ํ•œ ์ค„๋กœ ๋ฐ›๊ณ  ์ˆซ์ž์™€ ์—ฐ์‚ฐ์ž ์‚ฌ์ด๋Š” ์ŠคํŽ˜์ด์Šค๋กœ ๋ถ„ํ• ํ•œ๋‹ค. 2 + 3 * 5 ์ฒ˜๋Ÿผ ์ž…๋ ฅ์„ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•œ๋‹ค.
  • ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์€ html์˜ input๊ณผ div ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

ํžŒํŠธ

  • ๋ฌธ์ž์—ด์˜ split(" ")์„ ์ด์šฉํ•˜๋ฉด ์š”๊ตฌ์‚ฌํ•ญ๋Œ€๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋„์ „๊ณผ์ œ

  • ๊ณต๋ฐฑ ์—†์ด ์ž…๋ ฅ์„ ๋ฐ›์•„๋ณธ๋‹ค.
  • ์ผ๋ฐ˜ ๊ณ„์‚ฐ๊ธฐ์ฒ˜๋Ÿผ ๋ฒ„ํŠผ UI๋ฅผ ์ด์šฉํ•ด ๊ตฌํ˜„ํ•œ๋‹ค.
  • CSS๋ฅผ ์˜ˆ์˜๊ฒŒ ์ž…ํ˜€๋ณธ๋‹ค.

๋‚ด๊ฐ€ ์ง  ์ฝ”๋“œ

- ์ฒซ ๋‚  ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ์—์„œ ๊ทธ๋™์•ˆ ๋ฐฐ์šด ๊ฒƒ๋“ค์„ ์‘์šฉํ•ด๋ดค๋‹ค. (๊ฐ์ฒด, ํ”„๋กœํผํ‹ฐ, ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ ๋“ฑ)

๐Ÿ‘พ ๋””๋ฒ„๊น…
โš  Uncaught TypeError: Cannot read property 'Value' of null
-> ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ html๋ณด๋‹ค ๋จผ์ € ์‹คํ–‰๋˜์–ด์„œ ๋‚˜ํƒ€๋‚˜๋Š” ์˜ค๋ฅ˜๋ผ๊ณ .
์ฝ”๋“œ์—์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ถ€๋ถ„์„ ๊ฐ€๋Šฅํ•œ ๋ฌธ์„œ ์•„๋ž˜ ๋ถ€๋ถ„์œผ๋กœ ์˜ฎ๊ธฐ๊ณ  ๋‹ค์‹œ ์‹คํ–‰ํ•˜๋‹ˆ, ๋” ์ด์ƒ ์œ„์™€ ๊ฐ™์€ ์—๋Ÿฌ๋Š” ๋œจ์ง€ ์•Š๋Š”๋‹ค.
(๊ธฐ์กด <head>ํƒœ๊ทธ ๋‚ด์— ์žˆ๋˜ <script></script> ๋ถ€๋ถ„์„ <body></body>๋‚ด๋กœ ์˜ฎ๊ฒผ๋‹ค.)
์ฐธ๊ณ  : sir.kr/cm_free/941891
 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์˜ค๋ฅ˜ ์งˆ๋ฌธ์ž…๋‹ˆ๋‹ค. > SIR

์•ˆ๋…•ํ•˜์„ธ์š”. ์•„๋ž˜ ์†Œ์Šค๊ฐ€ ์ต์Šค์—์„œ๋Š” ์ด์ƒ์—†๋Š”๋ฐ ํฌ๋กฌ์—์„œ Uncaught TypeError: Cannot read property 'Value' of null ์ด๋ผ๋Š” ์˜ค๋ฅ˜๊ฐ€ ์žˆ๋‹ค๊ณ  ๋‚˜์˜ค๋„ค์š”. ๊ทธ๋ฆฌ๊ณ  InitAmt();๋„ ํ˜น์‹œ ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š”์ง€์š”.

sir.kr

-๋‹ค์†Œ ๋ถˆ์นœ์ ˆํ•œ ํ”„๋กœ๊ทธ๋žจ์ด์ง€๋งŒ ์ž‘๋™์€ ํ•œ๋‹ค.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1> ๊ณ„์‚ฐ๊ธฐ ํ”„๋กœ๊ทธ๋žจ 1 </h1>
๊ณ„์‚ฐ์‹์„ ์ž…๋ ฅํ•˜์„ธ์š”.<br>
- ์ˆซ์ž์™€ ์—ฐ์‚ฐ์ž ์‚ฌ์ด์—๋Š” ๊ผญ "๋„์–ด์“ฐ๊ธฐ"๋ฅผ ํ•ด ์ฃผ์„ธ์š”.<br>
- ๋”ํ•˜๊ธฐ๋Š” +, ๋นผ๊ธฐ๋Š” -, ๊ณฑํ•˜๊ธฐ๋Š” *, ๋‚˜๋ˆ„๊ธฐ๋Š” /๋กœ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.<br>
- ๊ณ„์‚ฐ์‹์˜ ์•ž๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. <br>
- ๋‹ค์‹œ ๊ณ„์‚ฐํ•˜๋ ค๋ฉด ์ƒˆ๋กœ๊ณ ์นจ(F5)์„ ๋ˆ„๋ฅด์„ธ์š”.<br>
<input type="text" id="input">
<button onclick="main()">๊ณ„์‚ฐํ•˜๊ธฐ</button>
<div id="result"></div>

  <script>
        var input = {};
        var calculator = {};
        var output = {};

        var main = function () {
            input.putArr();
            calculator.classify();
            calculator.calculate();
            output.print();
        }

        input.Arr = [];
        input.putArr = function () {
            var input = document.getElementById("input").value;
            this.Arr = input.split(" ");
            console.log("arr : " + this.Arr);
            return this.Arr;
        }

        calculator.numArr = [];
        calculator.symArr = [];
        calculator.classify = function () {
            var arr = input.putArr();
            for (var i = 0; i < arr.length; i++) {
                if (isNaN(Number(arr[i])) === true) {
                    this.symArr.push(arr[i]);
                } else {
                    this.numArr.push(arr[i]);
                }
            }
            console.log(this.numArr);
            console.log(this.symArr);
        }

        calculator.result = 0;
        calculator.calculate = function () {
            result = this.numArr[0];
            for (var j = 0; j < this.numArr.length - 1; j++) {
                if (this.symArr[j] === "+") {
                    result = Number(result) + Number(this.numArr[j + 1]);
                }
                if (this.symArr[j] === "-") {
                    result = Number(result) - Number(this.numArr[j + 1]);
                }
                if (this.symArr[j] === "*") {
                    result = Number(result) * Number(this.numArr[j + 1]);
                }
                if (this.symArr[j] === "/") {
                    result = Number(result) / Number(this.numArr[j + 1]);
                }
            }
            console.log(j, result);
            return result;
        }

        output.out = document.getElementById("result"); // div๊ฐ์ฒด
        output.print = function () {
            var result = calculator.calculate();
            this.out.innerHTML = "๊ณ„์‚ฐ๊ฒฐ๊ณผ : " + result;
        }

    </script>
</body>
</html>

 


ํ•ด์„ค

HTML ) p ์™€ div์˜ ์ฐจ์ด
p๋Š” paragraph์˜ ์•ฝ์ž๋กœ ๋ฌธ๋‹จ์„ ๋‚˜๋ˆŒ ๋•Œ ์“ฐ์ธ๋‹ค. - ์ฃผ๋กœ ๋ฌธ์„œ์˜ ๋‚ด์šฉ ๊ธฐ์ค€.
div๋Š” division์˜ ์•ฝ์ž๋กœ ๋ฌธ์„œ์˜ ์˜์—ญ์„ ๋‚˜๋ˆŒ ๋•Œ - ์ฃผ๋กœ ๋…ผ๋ฆฌ์  ๋‹จ์œ„ ๊ธฐ์ค€.
์ฝ”๋”ฉ์„ ํ•˜๋ฉด์„œ ๋ถˆํ•„์š”ํ•œ ๋ถ€๋ถ„์€.. 
- ์ฃผ์„์ฒ˜๋ฆฌํ•˜๊ณ  ๋‚˜์ค‘์— ์ง€์šฐ๊ธฐ๋ณด๋‹ค๋Š”, ๊ทธ๋•Œ๊ทธ๋•Œ ์ง€์šฐ๋Š” ๊ฒŒ ์ข‹๋‹ค.
- ์–ด์ฐจํ”ผ ์ปค๋ฐ‹๋งŒ ํ–ˆ๋‹ค๋ฉด ๋ณต๊ตฌํ•  ์ˆ˜ ์žˆ๊ณ , ์ง€์šฐ์ง€ ์•Š๊ณ  ๋‚จ๊ฒจ๋†“์€ ์ฝ”๋“œ๊ฐ€ ์ƒˆ๋กœ ์ง  ์ฝ”๋“œ์™€ ์ถฉ๋Œ์„ ์ผ์œผํ‚ฌ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ.

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ eval();
: ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋‚ด์žฅํ•จ์ˆ˜๋กœ, ๋ฌธ์ž์—ด์„ ์ฝ”๋“œ๋กœ ์ธ์‹ํ•˜๊ฒŒ ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. 
eval( string ); 
์˜ˆ๋ฅผ ๋“ค์–ด.. eval("2+2"); -> "2+2"๊ฐ€ ๋ฌธ์ž์—ด์ž„์—๋„ ์ฝ”๋“œ๋กœ ์ธ์‹ํ•ด 4๋ผ๋Š” ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
์šฐ๋ฆฌ๋Š” ๋ฐฐ์šฐ๋Š” ๋‹จ๊ณ„๋‹ˆ๊นŒ ์ด ํ•จ์ˆ˜๋Š” ์“ฐ์ง€ ์•Š๋„๋ก ํ•œ๋‹ค!
์ฐธ๊ณ  : www.codingfactory.net/11024
 

JavaScript / ํ•จ์ˆ˜ / eval() / ๋ฌธ์ž์—ด์„ ์ฝ”๋“œ๋กœ ์ธ์‹ํ•˜๊ฒŒ ํ•˜๋Š” ํ•จ์ˆ˜

eval() eval()์€ ๋ฌธ์ž์—ด์„ ์ฝ”๋“œ๋กœ ์ธ์‹ํ•˜๊ฒŒ ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ๋ฌธ๋ฒ• eval( string ) ์˜ˆ๋ฅผ ๋“ค์–ด eval( '2+2' ) ๋Š” ๋ฌธ์ž์—ด 2+2๊ฐ€ ์•„๋‹ˆ๋ผ ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ณผ 4์ž…๋‹ˆ๋‹ค. ์˜ˆ์ œ

www.codingfactory.net

 

๐Ÿ‘‰ํ•ด์„ค์—์„œ๋Š” .push(), .pop() (๋งจ ๋’ค์— ๋„ฃ๊ธฐ / ๋นผ๊ธฐ) ๋ฐ .unshift(), shift() (๋งจ ์•ž์— ๋„ฃ๊ธฐ / ๋นผ๊ธฐ) ๋ฅผ ํ™œ์šฉํ•œ๋‹ค.
: input๊ฐ’์„ .split(" "); ์œผ๋กœ ๋ฐฐ์—ด๋กœ ๋งŒ๋“  ๋’ค,
.shift() ๋ฅผ ํ†ตํ•ด ๋ฐฐ์—ด์˜ ๋งจ ์•ž์—์„œ๋ถ€ํ„ฐ ๊ฐ’์„ ํ•˜๋‚˜์”ฉ ๋นผ์„œ ์—ฐ์‚ฐํ•จ.
.shift()๋ฅผ ํ•˜๋ฉด ๋ฐฐ์—ด ์•ˆ์˜ ๊ฐ’์„ ํ•˜๋‚˜์”ฉ ๋ฝ‘์•„ ์“ฐ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ ํ•˜๋‚˜์”ฉ ์ค„์–ด๋“ค๊ณ ,
๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•  ํ•„์š”๋„, ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”ํ•  ํ•„์š”๋„ ์—†์Œ.
-> ์ด ๋ฐฉ์‹์ด ์ฝ”๋“œ์˜ ๊ธธ์ด๋„ ์ค„์ด๊ณ , ๋ณต์žกํ•จ๋„ ๋œํ•ด์„œ ๋” ๋‚˜์€ ๊ฒƒ ๊ฐ™๋‹ค.

NaN์ด๋ž€? 
Not a Number์˜ ์•ฝ์ž๋กœ, ์ˆซ์ž๊ฐ€ ์•„๋‹ˆ๋ผ๋Š” ๋œป.
NaN์€ ์—ฐ์‚ฐ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด 10 * "a" ๋ฅผ ์—ฐ์‚ฐํ•˜๋ฉด ๊ฒฐ๊ณผ๋กœ NaN์ด ๋‚˜์˜จ๋‹ค.


โ€ป ์ˆ˜์—…์„ ๋“ค์œผ๋ฉด์„œ ๋˜‘๊ฐ™์ด ๋”ฐ๋ผ ์ณค๋Š”๋ฐ, ์ด์ƒํ•˜๊ฒŒ ๋™์ž‘์ด ์•ˆ ๋œ๋‹ค. ์ฝ˜์†”์ฐฝ์กฐ์ฐจ ์•ˆ ๋– ์„œ ๋””๋ฒ„๊น…๋„ ๋ชป ํ•˜๊ฒ ๋‹ค. ์™œ ๊ทธ๋Ÿด๊นŒ?ใ… ใ…  ๋ฌดํ•œ๋ฃจํ”„ ํ•จ์ˆ˜์— ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š” ๊ฒƒ ๊ฐ™๊ธด ํ•œ๋ฐ
ํ˜ธ๋ˆ…์Šค๋‹˜์˜ ์›๋ณธ ์ •๋‹ต์ฝ”๋“œ๋Š” ์—ฌ๊ธฐ์—..

github.com/honux77/js-playground/blob/f983af9e961a709006fa3faeab1e2eed26892746/calculator/calculator7.html

 

honux77/js-playground

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ”Œ๋ ˆ์ด๊ทธ๋ผ์šด๋“œ ๊ณต๋ถ€์šฉ . Contribute to honux77/js-playground development by creating an account on GitHub.

github.com

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>๋ฌธ์ž์—ด ๊ณ„์‚ฐ๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ</h1>
<div>
    <input type="text" id="input">
    <button onclick="calc()">ํ™•์ธ</button>
</div>
<div id="output">์—ฌ๊ธฐ์— ๊ฒฐ๊ณผ๊ฐ€ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.</div>
<script>
    var input = {};

    input.init = function (str) {
        this.list = str.split(" ");
    }; // str์„ ๋„ฃ์œผ๋ฉด ์ชผ๊ฐœ์„œ ์ €์žฅ?

    input.empty = function(){
        return this.list.legnth === 0;
    };

    input.getValue = function () {
        var str = this.list.shift();
        var n = Number(str);
        return n;
    }; // getFirst, getSecond๊ฐ€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•˜๊ธฐ ๋–„๋ฌธ์— getValue๋กœ ํ†ตํ•ฉ.

    input.getOperator = function () {
        var op = this.list.shift();
        if (op === "+" || op === "-" || op === "*" || op === "/") {
            return op;
        } else { // ์‚ฌ์น™์—ฐ์‚ฐ ์™ธ์˜ ๊ฐ’์„ ๋„ฃ์œผ๋ฉด..
            return "$";
        }
    };

    var calculator = {};
    calculator.calculate = function (first, second, op) {
        var ret;
        switch (op) {
            case "+":
            ret = first + second;
            break;
            case "-":
            ret = first - second;
            break;
            case "*":
            ret = first * second;
            break;
            case "/":
            ret = first / second;
            break;
            default :// ์‚ฌ์น™์—ฐ์‚ฐ ๊ธฐํ˜ธ ์™ธ์˜ ๊ฐ’์„ ๋„ฃ์œผ๋ฉด ๊ณ„์‚ฐํ•˜์ง€ ์•Š๊ธฐ.
            return NaN;
        }
        return ret;
    };

    var output = {};
    output.out = document.getElementById("output"); // div๊ฐ์ฒด
    output.print = function (value) {
        this.out.innerHTML = "์ตœ์ข… ๊ฒฐ๊ณผ๊ฐ’์€ " + value + "์ž…๋‹ˆ๋‹ค.";
    };

    function calc() {
        var str = document.getElementById("input").value;
        input.init(str);
        var result = input.getValue();
        while (!input.empty()) { // input.empty๊ฐ€ ๋น„์–ด์žˆ์ง€ ์•Š์œผ๋ฉด.
            var op = input.getOperator();
            var second = input.getValue();
            result = calculator.calculate(result, second, op);
        }
        output.print(result);
    }
</script>
</body>
</html>

 

input.init = function(str) { // ๋ฐฐ์—ด ์ดˆ๊ธฐํ™” : input.list์— str์„ split(" ")ํ•œ ๋ฐฐ์—ด์„ ๋„ฃ๋Š”๋‹ค.
            this.list = str.split(" ");
        };

์ด๋ ‡๊ฒŒ ๋ฉ”์„œ๋“œ์™€ ์†์„ฑ(๋ฐฐ์—ด)์„ ๋™์‹œ์— ์ •์˜ํ•ด๋„ ์ž˜ ๋˜๋Š”๊ตฌ๋‚˜..

 

input.empty = function() {
	return this.list.length == 0;
};
        
//

function calc() {
    var str = document.getElementById('input').value;
    input.init(str);
    var result = input.getValue();
    while (!input.empty()) {
        var op = input.getOperator();
        var second = input.getValue();
        result = calculator.calculate(result, second, op);
    }
    output.print(result);
}

input.empty๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•˜๊ณ  ๋ฆฌํ„ด๊ฐ’์œผ๋กœ input.list.length == 0;์„ ๋„ฃ์—ˆ๋‹ค.
๊ทธ๋ž˜์„œ ์•„๋ž˜ calc()๋‚ด์˜ while๋ฌธ์—์„œ๋Š” input.empty()๊ฐ€ ์•„๋‹ ๋•Œ(!)
์ฆ‰ input.list.length๊ฐ€ 0์ด ์•„๋‹ ๋•Œ ๋กœ while ์กฐ๊ฑด๋ฌธ์„ ์ฃผ์—ˆ๋‹ค.
!input.empty(); -> true(input.list์— ์š”์†Œ๊ฐ€ ์žˆ์„ ๋•Œ) -> while๋ฌธ ๋ฌดํ•œ๋ฃจํ”„
!input.empty(); -> false(input.list์— ์š”์†Œ๊ฐ€ ์—†์„ ๋•Œ) -> while๋ฌธ ํƒˆ์ถœ

 

 

** GUI๊ณ„์‚ฐ๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ -> ๋‹ค์Œ ๊ฐ•์˜๋‚ด์šฉ์„ ๋“ฃ๋Š” ๊ฒŒ ์ง€๊ธˆ์€ ๋” ์ค‘์š”ํ•  ๊ฒƒ ๊ฐ™์•„์„œ ์šฐ์„  ์Šคํ‚ตํ•˜๊ธฐ๋กœ! **