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

[์ธํ”„๋Ÿฐ]Javascript์ž…๋ฌธ - ๊ณ„์‚ฐ๊ธฐ - ๊ฐ์ฒด ํ™œ์šฉ ๋ณธ๋ฌธ

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

[์ธํ”„๋Ÿฐ]Javascript์ž…๋ฌธ - ๊ณ„์‚ฐ๊ธฐ - ๊ฐ์ฒด ํ™œ์šฉ

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

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

  • ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ๊ฐ์ฒด๋ฅผ ๊ฒฝํ—˜ํ•œ๋‹ค.

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

  • ์‚ฌ์šฉ์ž์˜ ๊ฐ’์„ ์ž…๋ ฅํ•˜๋Š” ๊ฐ์ฒด, ์‚ฌ์น™์—ฐ์‚ฐ์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ฐ์ฒด, ์ถœ๋ ฅํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด์„œ ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌํ˜„ํ•œ๋‹ค.
  • main ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • ์ „์—ญ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋‹ค๋ฉด ๋ชจ๋‘ ๊ฐ์ฒด ์•ˆ์œผ๋กœ ๋„ฃ๊ธฐ.

ํžŒํŠธ

input = {}
input.getFirstValue = function();
input.getSecondValue= function();
intput.getOperator = function(); 

output = {} 
output.print = function(); 

calculator = {} 
calculator.calculate = function();

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

- ๊ณ„์‚ฐ์€ ์ž˜ ๋˜๋Š”๋ฐ, "n๋ฒˆ์งธ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”" ์—์„œ n++ ๋ฅผ ํ•ด๋„ ์นด์šดํŒ…์ด ์•ˆ ๋œ๋‹ค. (๊ณ„์† 2๋ฒˆ์งธ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜๋ผ๊ณ  ๋‚˜์˜ด)
ํ˜ผ์ž ๋””๋ฒ„๊น… ํ•ด๋ณด๋ ค๋‹ค ๋„ˆ๋ฌด ์˜ค๋ž˜ ๊ฑธ๋ ค์„œ ๊ทธ๋ƒฅ ํ•ด์„ค์„ ๋ณด๊ธฐ๋กœ..

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>๋ฌธ์ž์—ด ๊ณ„์‚ฐ๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ</h1>
<div id="exit">์—ฌ๊ธฐ์— ๊ฒฐ๊ณผ๊ฐ€ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.</div>
<script>
    input = {} // getFirstValue, getSecondValue, getOperator
    output = {} // print
    calculator = {} // calculate

    input.getFirstValue = function() {
        var n1 = Number(prompt("์ฒซ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."));
        return n1;
    }

    input.getSecondValue = function (n) {
        var n2 = Number(prompt(n + "๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."));
        n++;
        return n2;
    }

    input.getOperator = function () {
        while (true) {
            var op = prompt("์—ฐ์‚ฐ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.(q๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ข…๋ฃŒ)");
            if (op === "+" || op === "-" || op === "*" || op === "/"
                || op === "q") {
                break;
            } else { // ์‚ฌ์น™์—ฐ์‚ฐ ์™ธ์˜ ๊ฐ’์„ ๋„ฃ์œผ๋ฉด..
                alert("์˜ฌ๋ฐ”๋ฅธ ์—ฐ์‚ฐ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.")
            }
        }
        return op;
    }

    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;
        }
        console.log("return : " + ret);
        return ret;
    }

    output.print = function (value) {
        return "์ตœ์ข… ๊ฒฐ๊ณผ๊ฐ’์€ " + value + "์ž…๋‹ˆ๋‹ค.";
    }

    function main() {
        var num1 = input.getFirstValue(); // ์ฒซ ๋ฒˆ์งธ ๊ฐ’
        console.log("num1 : " + num1);
        var result = num1;
        var n = 2;
        while(true) {
            var op = input.getOperator(); // ์—ฐ์‚ฐ์ž
            console.log("operator : " + op);
            if (op === "q") {
                break;
            }
            var num2 = input.getSecondValue(n); // ๋‘ ๋ฒˆ์งธ ๊ฐ’
            console.log("num2 : " + num2)
            result = calculator.calculate(result, num2, op);
        }
        var exit = document.getElementById("exit");
        exit.innerHTML = output.print(result);
    }

    main();

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

 


ํ•ด์„ค

this ์‚ฌ์šฉ.
-> ๋ฉ”์†Œ๋“œ(๊ฐ์ฒด ์•ˆ์˜ ํ•จ์ˆ˜) ๋‚ด์—์„œ ๊ฐ์ฒด์˜ ๋ฉค๋ฒ„๋ณ€์ˆ˜์— ์ ‘๊ทผํ•  ๋•Œ this๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
โ€ป ์†์„ฑ(๊ฐ์ฒด์˜ ๋ณ€์ˆ˜) = ๋ฉค๋ฒ„๋ณ€์ˆ˜
var n = 2; ๋Š” ์˜๋ฏธ๋ฅผ ์‚ด๋ ค count๋กœ ๋ณ€์ˆ˜๋ช…์„ ๋ฐ”๊พธ๊ณ , ๊ฐ์ฒด input์˜ ์†์„ฑ์œผ๋กœ ๋„ฃ์–ด ์ค€๋‹ค.
function foo() {
} // ์ด ๋•Œ๋Š” ;๊ฐ€ ํ•„์š”์—†์ง€๋งŒ,
var foo = function() {
}; // ์ด๋ ‡๊ฒŒ ํ•จ์ˆ˜ ์„ ์–ธ ํ•  ๋•Œ์™€
๊ฐ์ฒด.๋ฉ”์„œ๋“œ์ด๋ฆ„ = function() {
}; // ๋ฉ”์„œ๋“œ ์„ ์–ธ ํ•  ๋•Œ๋Š” ;๊ฐ€ ํ•„์š”ํ•˜๋‹ค (์ผ๋‹จ ์™ธ์šฐ๊ธฐ)
๊ฐ์ฒด = ๋ฌธ์žฅ์˜ ์ฃผ์–ด 
๋ฉ”์„œ๋“œ = ๋ฌธ์žฅ์˜ ๋™์‚ฌ
ํŒŒ๋ผ๋ฏธํ„ฐ(๋งค๊ฐœ๋ณ€์ˆ˜) = ๋ฌธ์žฅ์˜ ๋ชฉ์ ์–ด/๋ณด์–ด 
๋Š๋‚Œ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ฝ์œผ๋ฉด ๋œ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด..
input.getFirstValue(); -> input์•„, ์ฒซ ๋ฒˆ์งธ ๊ฐ’์„ ๊ฐ€์ ธ์™€ ์ค˜.
calculator.calculate(result,num,op) -> calculator์•ผ, (result, num, op)๋ฅผ ๊ฐ€์ง€๊ณ  calculateํ•ด ์ค˜.
โ— ์ถœ๋ ฅํ•˜๋Š” ๋ถ€๋ถ„ var output = document.getElementById("output")
-> ๊ฐ์ฒด output์˜ ์†์„ฑ output.out์œผ๋กœ ์ „ํ™˜.
๋Œ€์‹  output.print ๋ถ€๋ถ„์— this.out.innerHTML ๋กœ ํ™œ์šฉ.
์ด๋ ‡๊ฒŒ ํ•˜๋ฉด main()์˜ ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„์„ output.print(result);๋กœ ๊ฐ„๋‹จํžˆ ์ฝ”๋”ฉ ๊ฐ€๋Šฅ.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>๋ฌธ์ž์—ด ๊ณ„์‚ฐ๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ</h1>
<div id="output">์—ฌ๊ธฐ์— ๊ฒฐ๊ณผ๊ฐ€ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.</div>
<script>
    var input = {count : 2};// var n = 2; -> ์˜๋ฏธ๋ฅผ ์‚ด๋ ค count๋กœ ์ด๋ฆ„์„ ๋ฐ”๊พผ ํ›„, input์˜ ์†์„ฑ์œผ๋กœ ๋„ฃ๋Š”๋‹ค.

    input.getFirstValue = function() {
        var n = Number(prompt("์ฒซ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."));
        return n;
    };

    input.getSecondValue = function() {// input์ด๋ผ๋Š” ๊ฐ์ฒด์˜ ์†์„ฑ count์ด๋ฏ€๋กœ, this.count๋กœ ์‚ฌ์šฉ.
        var n2 = Number(prompt(this.count + "๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."));
        this.count++;
        return n2;
    };

    input.getOperator = function() {
        while (true) {
            var op = prompt("์—ฐ์‚ฐ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.");
            if (op === "+" || op === "-" || op === "*" || op === "/"
                || op === "q") {
                break;
            } else { // ์‚ฌ์น™์—ฐ์‚ฐ ์™ธ์˜ ๊ฐ’์„ ๋„ฃ์œผ๋ฉด..
                alert("์˜ฌ๋ฐ”๋ฅธ ์—ฐ์‚ฐ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.")
            }
        }
        return op;
    };

    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;
        }
        return ret;
    };

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

    function main() {
        var result = input.getFirstValue();
        while(true) {
            var op = input.getOperator();
            if (op === "q") {
                break;
            }
            var num = input.getSecondValue();
            result = calculator.calculate(result,num,op);
        }
        output.print(result);
    }
    main();
</script>
</body>
</html>