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

[์ธํ”„๋Ÿฐ]Javascript์ž…๋ฌธ - ๋ฌธ์ž์—ด ๊ณ„์‚ฐ๊ธฐ - ์กฐ๊ฑด๋ฌธ, ๋ฐ˜๋ณต๋ฌธ ๋ณธ๋ฌธ

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

[์ธํ”„๋Ÿฐ]Javascript์ž…๋ฌธ - ๋ฌธ์ž์—ด ๊ณ„์‚ฐ๊ธฐ - ์กฐ๊ฑด๋ฌธ, ๋ฐ˜๋ณต๋ฌธ

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

 

์กฐ๊ฑด๋ฌธ

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

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

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

  • ์‚ฌ์šฉ์ž์—๊ฒŒ 2๊ฐœ์˜ ์ˆซ์ž ๊ฐ’๊ณผ ์‚ฌ์น™์—ฐ์‚ฐ ๊ธฐํ˜ธ(+, -, *, /) ์ค‘ ํ•˜๋‚˜๋ฅผ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค.
  • ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์‚ฌ์น™์—ฐ์‚ฐ ๊ธฐํ˜ธ์— ๋”ฐ๋ผ ์‚ฌ์น™์—ฐ์‚ฐ์„ ๊ณ„์‚ฐํ•œ ํ›„ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
  • ์˜ˆ๋ฅผ ๋“ค์–ด ์‚ฌ์šฉ์ž๋Š” ๊ฐ’ 2์™€ 4 ์ž…๋ ฅํ•˜๊ณ  ๊ณฑ์…ˆ(*) ๊ธฐํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด 2 * 4์˜ ๊ฒฐ๊ณผ์ธ 8์„ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

ํžŒํŠธ

  • ์‚ฌ์น™์—ฐ์‚ฐ ๊ธฐํ˜ธ๊ฐ€ ๋ฌด์—‡์ธ์ง€์— ๋”ฐ๋ผ ์‚ฌ์น™์—ฐ์‚ฐ์„ ์‹คํ–‰ํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
  • ์กฐ๊ฑด๋ฌธ(if/else if/else)์„ ์‚ฌ์šฉํ•ด ๊ตฌํ˜„ํ•œ๋‹ค.
  • ๋ฌธ์ž์—ด ๋น„๊ต๋ฅผ ์œ„ํ•ด === ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
if (op === '+') {
//code here 
} else if 
//...

์ถ”๊ฐ€ ํ•™์Šตํ•˜๊ธฐ

  • == ๊ณผ ===์˜ ์ฐจ์ด
  • switch ์™€ case๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ํ•ด ๋ณด์ž.

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

- if, else if, else ๋ฒ„์ „

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>๊ณ„์‚ฐ๊ธฐ - ์กฐ๊ฑด๋ฌธ ์‚ฌ์šฉ</h1>
- 2๊ฐœ์˜ ์ˆซ์ž์™€ ์‚ฌ์น™์—ฐ์‚ฐ ๊ธฐํ˜ธ(+ - * /)๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๊ณ„์‚ฐ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.<br>
์ˆซ์ž 1 : <input type="number" id="num1"> <br>
์ˆซ์ž 2 : <input type="number" id="num2"> <br>
์‚ฌ์น™์—ฐ์‚ฐ : <input type="text" id="symbol"> <br>
<button onclick="calc()">๊ณ„์‚ฐํ•˜๊ธฐ</button>
<div id="result"></div>

<script>
function calc() {
    var str1 = document.getElementById("num1").value;
    var str2 = document.getElementById("num2").value;
    var num1 = Number(str1);
    var num2 = Number(str2);
    var symbol = document.getElementById("symbol").value;
    var result = 0;
    if (symbol === "+") {
        result = num1 + num2;
    } else if (symbol === "-"){
        result = num1 - num2;
    } else if (symbol === "*") {
        result = num1 * num2;
    } else if (symbol === "/") {
        result = num1 / num2;
    } else {
        result = "+(๋”ํ•˜๊ธฐ), -(๋นผ๊ธฐ), *(๊ณฑํ•˜๊ธฐ), /(๋‚˜๋ˆ„๊ธฐ)์ค‘ ํ•˜๋‚˜๋ฅผ ์ž…๋ ฅํ•˜์‹ญ์‹œ์˜ค"
    }
    document.getElementById("result").innerHTML = "๊ณ„์‚ฐ๊ฒฐ๊ณผ : "+result;
}
</script>
</body>
</html>

 

- switch-case ๋ฒ„์ „

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>๊ณ„์‚ฐ๊ธฐ - ์กฐ๊ฑด๋ฌธ ์‚ฌ์šฉ</h1>
- 2๊ฐœ์˜ ์ˆซ์ž์™€ ์‚ฌ์น™์—ฐ์‚ฐ ๊ธฐํ˜ธ(+ - * /)๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๊ณ„์‚ฐ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.<br>
์ˆซ์ž 1 : <input type="number" id="num1"> <br>
์ˆซ์ž 2 : <input type="number" id="num2"> <br>
์‚ฌ์น™์—ฐ์‚ฐ : <input type="text" id="symbol"> <br>
<button onclick="calc()">๊ณ„์‚ฐํ•˜๊ธฐ</button>
<div id="result"></div>

<script>
function calc() {
    var str1 = document.getElementById("num1").value;
    var str2 = document.getElementById("num2").value;
    var num1 = Number(str1);
    var num2 = Number(str2);
    var symbol = document.getElementById("symbol").value;
    var result = 0;
    switch (symbol) {
        case "+" :
            result = num1 + num2;
            break;
        case "-" :
            result = num1 - num2;
            break;
        case "*" :
            result = num1 * num2;
            break;
        case "/" :
            result = num1 / num2;
            break;
        default :
            result = "์‚ฌ์น™์—ฐ์‚ฐ ์นธ์—๋Š” +(๋”ํ•˜๊ธฐ), -(๋นผ๊ธฐ), *(๊ณฑํ•˜๊ธฐ), /(๋‚˜๋ˆ„๊ธฐ)์ค‘ ํ•˜๋‚˜๋ฅผ ์ž…๋ ฅํ•˜์‹ญ์‹œ์˜ค";
    }
    document.getElementById("result").innerHTML = "๊ณ„์‚ฐ๊ฒฐ๊ณผ : "+result;
}
</script>
</body>
</html>

 


 

ํ•ด์„ค

* ์—ฐ์‚ฐ์ž (Operator) : ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ฌธ์ž.
(์ฐธ๊ณ  : jhnyang.tistory.com/185 ์—ฐ์‚ฐ์ž๋ž€?, ์—ฐ์‚ฐ์ž ์ข…๋ฅ˜ )
-> ๋ณ€์ˆ˜๋ช…์œผ๋กœ๋Š” op๋กœ ์ค„์—ฌ ๋ถ€๋ฅธ๋‹ค.

-if๋ฌธ ๋ฒ„์ „

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>๋ฌธ์ž์—ด ๊ณ„์‚ฐ๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ</h1>
<div>
    ์ฒซ ๋ฒˆ์งธ ์ˆ˜ : <input type="text" id="n1"><br>
    ์—ฐ์‚ฐ์ž : <input type="text" id="op"><br>
    ๋‘ ๋ฒˆ์งธ ์ˆ˜ : <input type="text" id="n2"><br>
    <button onclick="calc()">์‹คํ–‰</button>
</div>
<div id="output">์—ฌ๊ธฐ์— ๊ฒฐ๊ณผ๊ฐ€ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.</div>
<script>
    function calc() {
        var str1 = document.getElementById("n1").value;
        var str2 = document.getElementById("n2").value;
        var op = document.getElementById("op").value;
        var n1 = Number(str1);
        var n2 = Number(str2);

        var out = document.getElementById("output");
        var str = "";
        if (op === "+") {
            str += "๋”ํ•˜๊ธฐ : " + (n1 + n2) + "<br>";
        } else if (op === "-") {
            str += "๋นผ๊ธฐ : " + (n1 - n2) + "<br>";
        } else if (op === "*") {
            str += "๊ณฑํ•˜๊ธฐ : " + (n1 * n2) + "<br>";
        } else if (op === "/") {
            str += "๋‚˜๋ˆ„๊ธฐ : " + (n1 / n2) + "<br>";
        } else {
            str += "์—ฐ์‚ฐ์ž์—๋Š” ์‚ฌ์น™์—ฐ์‚ฐ์ž๋งŒ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."
        }
        out.innerHTML = str;
    }

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

 

-switch-case ๋ฒ„์ „

โ“ switch-case๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ?
if๋ฌธ์ด ๋„ˆ๋ฌด ๋งŽ์•„์„œ!
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>๋ฌธ์ž์—ด ๊ณ„์‚ฐ๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ</h1>
<div>
    ์ฒซ ๋ฒˆ์งธ ์ˆ˜ : <input type="text" id="n1"><br>
    ์—ฐ์‚ฐ์ž : <input type="text" id="op"><br>
    ๋‘ ๋ฒˆ์งธ ์ˆ˜ : <input type="text" id="n2"><br>
    <button onclick="calc()">์‹คํ–‰</button>
</div>
<div id="output">์—ฌ๊ธฐ์— ๊ฒฐ๊ณผ๊ฐ€ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.</div>
<script>
    function calc() {
        var str1 = document.getElementById("n1").value;
        var str2 = document.getElementById("n2").value;
        var op = document.getElementById("op").value;
        var n1 = Number(str1);
        var n2 = Number(str2);

        var out = document.getElementById("output");
        var str = "";
        switch (op) {
            case "+" :
                str += "๋”ํ•˜๊ธฐ: " + (n1 + n2) + "<br>";
                break;
            case "-" :
                str += "๋นผ๊ธฐ: " + (n1 - n2) + "<br>";
                break;
            case "*" :
                str += "๊ณฑํ•˜๊ธฐ: " + (n1 * n2) + "<br>";
                break;
            case "/" :
                str += "๋‚˜๋ˆ„๊ธฐ: " + (n1 / n2) + "<br>";
                break;
        }
        out.innerHTML = str;
    }

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

 

 

 



 

๋ฐ˜๋ณต๋ฌธ

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

  • ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ค‘ ๋ฐ˜๋ณต๋ฌธ์„ ๊ฒฝํ—˜ํ•œ๋‹ค.
  • ๋ฌดํ•œ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•ด ๋ณธ๋‹ค.

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

  • prompt()๋ฅผ ์ด์šฉํ•ด์„œ ๊ตฌํ˜„ํ•œ๋‹ค.
  • ํ•œ ๋ฒˆ์— 2๊ฐœ์˜ ์ˆซ์ž ๊ฐ’๋งŒ ์‚ฌ์น™์—ฐ์‚ฐ์„ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์‚ฌ์น™์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•ด์•ผ ํ•œ๋‹ค.
  • ์•ž ๋‹จ๊ณ„์—์„œ ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ณผ ๊ฐ’์€ ์ดํ›„ ์—ฐ์‚ฐ์˜ ์ฒซ๋ฒˆ์งธ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
    • ์˜ˆ๋ฅผ ๋“ค์–ด 4 + 2 * 3์˜ ์ˆœ์„œ๋กœ ๊ฐ’์„ ์ž…๋ ฅํ•˜๋ฉด ๋จผ์ € 4 + 2๊ฐ€ ๊ณ„์‚ฐ๋˜๊ณ  ๊ฒฐ๊ณผ ๊ฐ’ 6๊ณผ 3์˜ ๊ณฑ์…ˆ์„ ํ†ตํ•ด 18์˜ ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์™€์•ผ ํ•œ๋‹ค.
  • ์‚ฌ์น™์—ฐ์‚ฐ ๊ธฐํ˜ธ ๋Œ€์‹  "q"๋ผ๋Š” ๋ฌธ์ž๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ตœ์ข… ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•œ๋‹ค.

ํžŒํŠธ

  • ๊ฐ’์„ ์ž…๋ ฅ ๋ฐ›์„ ๋•Œ while ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•œ๋‹ค.
  • while(true)์™€ ๊ฐ™์ด ๊ตฌํ˜„ํ•˜๋ฉด ๋ฌดํ•œํžˆ ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.
  • while(true)๋กœ ์‹คํ–‰ํ•˜๊ณ  ์žˆ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•˜๋ ค๋ฉด break ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. ์ฆ‰, "quit"์ด๋ผ๋Š” ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜๋Š” ๊ฒฝ์šฐ break๋ฅผ ์‚ฌ์šฉํ•ด ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•  ์ˆ˜ ์žˆ๋‹ค.

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

- ๋ฏธ์™„์„ฑ (์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š์Œ)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>๊ณ„์‚ฐ๊ธฐ - ๋ฐ˜๋ณต๋ฌธ</h1>
<div id="result"></div>
<script>
    var numArr = [];
    var symArr = [];
    var result = 0;

    function calculate() {
        while (true) {
            var num = Number(prompt("๊ณ„์‚ฐ์‹์— ๋“ค์–ด๊ฐˆ ์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”."));
            numArr.push(num);
            var sym = prompt("๊ณ„์‚ฐ์‹์— ๋“ค์–ด๊ฐˆ ์—ฐ์‚ฐ์ž(+-*/)๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”. p๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค.");
            symArr.push(sym);
            document.getElementById("result").innerHTML = result;
            if (sym === "p") {
                break;
            }
        }
        console.log(numArr, symArr);


        if (symArr[0] === "+") {
            result += numArr[0] + numArr[0 + 1];
            console.log(numArr[0] + "๋”ํ•˜๊ธฐ" + numArr [0 + 1] + "=" + result);
        } else if (symArr[0] === "-") {
            result += numArr[0] - numArr[0 + 1];
            console.log(numArr[0] + "๋นผ๊ธฐ" + numArr [0 + 1] + "=" + result);
        } else if (symArr[0] === "*") {
            result += numArr[0] * numArr[0 + 1];
            console.log(numArr[0] + "๊ณฑํ•˜๊ธฐ" + numArr [0 + 1] + "=" + result);
        } else if (symArr[0] === "/") {
            result += numArr[0] / numArr[0 + 1];
            console.log(numArr[0] + "๋‚˜๋ˆ„๊ธฐ" + numArr [0 + 1] + "=" + result);
        } else {
            document.getElementById("result").innerHTML = "์—ฐ์‚ฐ์ž์—๋Š” + - * / ์ค‘ ํ•˜๋‚˜๋งŒ ๋„ฃ์–ด์ฃผ์„ธ์š”";
        } // 0๋ฒˆ์จฐ, 1๋ฒˆ์งธ ์‚ฌ์น™์—ฐ์‚ฐ ๋

        for (var i = 2; i < numArr.length; i++) {
            // ์ด์ œ ๊ฒฐ๊ณผ & 2๋ฒˆ์งธ, ๊ฒฐ๊ณผ& 3๋ฒˆ์จฐ .. ๊ณ„์‚ฐํ•ด์•ผ
            if (symArr[i] === "+") {
                result += result + numArr[i + 1];
            } else if (symArr[0] === "-") {
                result += result - numArr[i + 1];
            } else if (symArr[0] === "*") {
                result += result * numArr[i + 1];
            } else if (symArr[0] === "/") {
                result += result / numArr[i + 1];
            } else {
                document.getElementById("result").innerHTML = "์—ฐ์‚ฐ์ž์—๋Š” + - * / ์ค‘ ํ•˜๋‚˜๋งŒ ๋„ฃ์–ด์ฃผ์„ธ์š”";
            }
        }
    }

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

 


ํ•ด์„ค

- ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ ๋•Œ๋Š”..
[์ˆซ์ž1, ์—ฐ์‚ฐ์ž1, ์ˆซ์ž2, ์—ฐ์‚ฐ์ž 2..] -> ๋ฅผ ๋ฐ˜๋ณตํ•˜๋ฉด ์•ˆ ๋œ๋‹ค! -> ์ด๊ฒƒ์ด ๋‚ด ์ฝ”๋“œ์˜ ๋ฌธ์ œ์ ,..
์ˆซ์ž1 ์„ ๋ฐ›์€ ๋’ค, [์—ฐ์‚ฐ์ž1, ์ˆซ์ž2, ์—ฐ์‚ฐ์ž2, ์ˆซ์ž3, ์—ฐ์‚ฐ์ž3, ์ˆซ์ž4]..->๋ฅผ ๋ฐ˜๋ณตํ•ด์•ผ ํ•œ๋‹ค!
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>๋ฌธ์ž์—ด ๊ณ„์‚ฐ๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ</h1>
<div id="output">์—ฌ๊ธฐ์— ๊ฒฐ๊ณผ๊ฐ€ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.</div>
<script>
    function main() {
        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++;
        }

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

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

์ฒ˜์Œ์— result = n1์„ ํ•œ ์ด์œ  : ์ฒ˜์Œ ์ž…๋ ฅ๋ฐ›์€ ์ˆซ์ž๋ฅผ result๊ฐ’์œผ๋กœ ๋„ฃ์–ด์„œ, ์•„๋ž˜์˜ while๋ฐ˜๋ณต๋ฌธ์„ ๊ณ„์† ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•ด.

if (op === 'q') { break; } ๊ฐ€ op๋ฐ”๋กœ ๋ฐ‘์— ์žˆ๋Š” ์ด์œ  : op(์—ฐ์‚ฐ์ž)๊ฐ’์œผ๋กœ q๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ์ฆ‰์‹œ ๋ฐ˜๋ณต๋ฌธ์„ ์ข…๋ฃŒํ•˜๊ธฐ ์œ„ํ•ด.

์ƒ๋‹นํžˆ ๋‚œ์ด๋„๊ฐ€ ์žˆ๋Š” ๋ฌธ์ œ์˜€๋‹ค. (์ฒ˜์Œ ํ‘ผ ๋ฌธ์ œ ๊ฐ™์€ ๋Š๋‚Œ)