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

[์ธํ”„๋Ÿฐ]Javascript์ž…๋ฌธ - ๊ณ„์‚ฐ๊ธฐ - ํ•จ์ˆ˜๋กœ ์‚ฌ์น™์—ฐ์‚ฐ ๊ตฌํ˜„ ๋ณธ๋ฌธ

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

[์ธํ”„๋Ÿฐ]Javascript์ž…๋ฌธ - ๊ณ„์‚ฐ๊ธฐ - ํ•จ์ˆ˜๋กœ ์‚ฌ์น™์—ฐ์‚ฐ ๊ตฌํ˜„

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

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

  • ํ•จ์ˆ˜๋ฅผ ๊ฒฝํ—˜ํ•œ๋‹ค.
๋ฆฌํŒฉํ† ๋ง : ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์ฝ”๋“œ๋ฅผ ๊น”๋”ํ•˜๊ฒŒ ์ •๋ฆฌํ•˜๋Š” ๊ฒƒ?
(๊ตฌ๊ธ€๋ง)
๋ฆฌํŒฉํ† ๋ง์ด๋ž€
: ์™ธ๋ถ€๋™์ž‘์€ ๋ฐ”๊พธ์ง€ ์•Š์œผ๋ฉด์„œ ๋‚ด๋ถ€๊ตฌ์กฐ๋ฅผ ๊ฐœ์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•.
์†Œํ”„ํŠธ์›จ์–ด ์‹œ์Šคํ…œ์„ ๋ณด๋‹ค ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ, ์ˆ˜์ •ํ•˜๊ธฐ ์‰ฝ๊ฒŒ ๋ณ€๊ฒฝํ•˜๋Š” ํ”„๋กœ์„ธ์Šค.
๋ฆฌํŒฉํ† ๋ง์„ ํ†ตํ•ด ๊ฐ€๋…์„ฑ์„ ๋†’์ด๊ณ , ์œ ์ง€๋ณด์ˆ˜๋ฅผ ํŽธํ•˜๊ฒŒ ํ•œ๋‹ค.

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

  • ๋ฉ”์ธ ํ•จ์ˆ˜, ์‚ฌ์šฉ์ž์˜ ๊ฐ’์„ ์ž…๋ ฅํ•˜๋Š” ํ•จ์ˆ˜, ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜ ๋“ฑ ํ•จ์ˆ˜๋กœ ์ฝ”๋“œ๋ฅผ ๋‚˜๋ˆ„์–ด ๊ตฌํ˜„ํ•œ๋‹ค.
  • ์‚ฌ์šฉ์ž์˜ ๊ฐ’์„ ์ž…๋ ฅํ•˜๋Š” ๊ตฌํ˜„์€ ์ฒซ ๋ฒˆ์งธ ์ˆซ์ž ๊ฐ’, ์‚ฌ์น™์—ฐ์‚ฐ ๊ธฐํ˜ธ ๋ฐ quit, ๋‘ ๋ฒˆ์งธ ์ˆซ์ž ๊ฐ’ ์ž…๋ ฅ์œผ๋กœ ๋‚˜๋‰œ๋‹ค.

ํžŒํŠธ

  • ๊ฐ ์š”๊ตฌ์‚ฌํ•ญ์— ํ•ด๋‹นํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ํ•˜๋‚˜์”ฉ ์ƒ์„ฑํ•œ ํ›„ ์ง€๊ธˆ๊นŒ์ง€ main() ํ•จ์ˆ˜์—์„œ ๊ตฌํ˜„ํ•˜๋˜ ๋กœ์ง์„ ๊ฐ ํ•จ์ˆ˜๋กœ ์ด๋™ํ•œ๋‹ค.
  • ๋Œ€๋žต ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ•จ์ˆ˜๋“ค๋กœ ๋‚˜๋‰  ์ˆ˜ ์žˆ๋‹ค.
fucntion getFirstValue()
function getSecondValue() 
function getOperator()
function calculate() 
function print() 
function main()

 


๋‚ด๊ฐ€ ์ง  ํ•จ์ˆ˜

(๋ฐ˜๋ณต๋ฌธ ์ •๋‹ต ์ฝ”๋“œ ํ™œ์šฉ)

-getValue(), calculate(), print(), main()์œผ๋กœ ๋‚˜๋ˆ„๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ
getValue()์— prompt๋ถ€๋ถ„์„, calculate()์— while๋ฌธ ์•ˆ์ชฝ ๊ณ„์‚ฐ๋ถ€๋ถ„์„ ๋„ฃ์œผ๋ ค ํ•˜๋‹ˆ
'์ค‘๊ฐ„์˜ค๋ฅ˜'์ชฝ break;๋ฅผ ์ธ์‹ํ•˜์ง€ ๋ชปํ•ด์„œ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๋”๋ผ. ๊ทธ๋ž˜์„œ ๊ทธ๋ƒฅ ๋‹ค์‹œ ํ•ฉ์ณค๋‹ค.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>๋ฌธ์ž์—ด ๊ณ„์‚ฐ๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ</h1>
<div id="output">์—ฌ๊ธฐ์— ๊ฒฐ๊ณผ๊ฐ€ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.</div>
<script>
    function getValue() {
        var n1 = Number(prompt("์ฒซ ๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."));
        var op, n2;
        var result = n1;// ์ฒ˜์Œ ๊ฒฐ๊ณผ๊ฐ’์— ํ•œ ๋ฒˆ๋งŒ n1์„ ๋„ฃ์–ด while๋ฐ˜๋ณต๋ฌธ ํ™œ์šฉํ•˜๊ธฐ.
        var n = 2;
        while (true) { // ๋ฌดํ•œ๋ฃจํ”„
            op = prompt("์—ฐ์‚ฐ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”. q๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค.")
            if (op === 'q') {
                break;// break๋Š” ๋ฐ˜๋ณต๋ฌธ(while) ์ „์ฒด๋ฅผ ๋น ์ ธ๋‚˜๊ฐ„๋‹ค.
            }
            n2 = Number(prompt(n + "๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."));
            if (op === "+") {
                result += n2;
            } else if (op === "-") {
                result -= n2;
            } else if (op === "*") {
                result *= n2;
            } else if (op === "/") {
                result /= n2;
            } else {
                result = "์ค‘๊ฐ„์˜ค๋ฅ˜";
                break;
            }
            n++;
        }
        print(result);
    }


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

    function main() {
        getValue();
    }

    main();
</script>
</body>
</html>

 

ํ•ด์„ค

 

-'ํžŒํŠธ'์— ์žˆ๋Š” ๋ชจ๋“  ํ•จ์ˆ˜๋“ค๋กœ ์ฝ”๋“œ๋ฅผ ๋‚˜๋ˆˆ๋‹ค.

- ๊ฐ ํ•จ์ˆ˜์— return์„ ์‚ฌ์šฉํ•œ๋‹ค.

- ๋ฐ˜๋ณต๋ฌธ์— ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์‚ฌ์šฉํ•œ๋‹ค.

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

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

    var n = 2;

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

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

    function calculate(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;
    }

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

    function main() {
        var result = getFirstValue();
        while(true) {
            var op = getOperator();
            if (op === "q") {
                break;
            }
            var num = getSecondValue();
            result = calculate(result,num,op);
        }
        var output = document.getElementById("output");
    output.innerHTML = print(result);
    }
    main();

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