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

[์ธํ”„๋Ÿฐ] Javascript์ž…๋ฌธ - ๋ฐฐ์—ด๋กœ ๊ตฌ๊ตฌ๋‹จ ๋‹ค์‹œ ๊ตฌํ˜„ (์‹ค์Šต - ํ•ด์„ค ๋ฐ ์ฐธ๊ณ ์ž๋ฃŒ, ๋ฐฐ์—ด์ดˆ๊ธฐํ™”) ๋ณธ๋ฌธ

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

[์ธํ”„๋Ÿฐ] Javascript์ž…๋ฌธ - ๋ฐฐ์—ด๋กœ ๊ตฌ๊ตฌ๋‹จ ๋‹ค์‹œ ๊ตฌํ˜„ (์‹ค์Šต - ํ•ด์„ค ๋ฐ ์ฐธ๊ณ ์ž๋ฃŒ, ๋ฐฐ์—ด์ดˆ๊ธฐํ™”)

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

 

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

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

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

  • ๊ตฌ๊ตฌ๋‹จ ๊ฒฐ๊ณผ์„ ๋ฐฐ์—ด์— ์ €์žฅํ•œ ํ›„ ๋ฐฐ์—ด์˜ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๊ฒฐ๊ณผ๋ฌผ

  • ๊ตฌ๊ตฌ๋‹จ์„ ์ฝ˜์†”์— ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ
  • ๊ตฌ๊ตฌ๋‹จ์„ html์— ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ
  • github ์ €์žฅ์†Œ

ํžŒํŠธ

  • 2๋‹จ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ๋‹ค.
  • for๋ฌธ์„ ๋Œ๋ฉด์„œ ๊ตฌ๊ตฌ๋‹จ ์‹คํ–‰ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค.
  • ๋ฐฐ์—ด์˜ ํฌ๊ธฐ ๋งŒํผ for ๋ฌธ์„ ํ†ตํ•ด ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

var results = []; var length = 9; for(var i=1; i <= length; i++) { // ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์—ด์— ์ €์žฅ } for(var i=1; i <= length; i++) { // ๋ฐฐ์—ด์˜ ๋‚ด์šฉ์„ ์ถœ๋ ฅ }

var results = []; 
var length = 9; 
for(var i=1; i <= length; i++) {
// ๊ณ„์‚ฐ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์—ด์— ์ €์žฅ 
}

for(var i=1; i <= length; i++) {
// ๋ฐฐ์—ด์˜ ๋‚ด์šฉ์„ ์ถœ๋ ฅ 
}

ํ•™์Šต ์ฐธ๊ณ ์ž๋ฃŒ

๋ฐฐ์—ด

์ƒํ™œ์ฝ”๋”ฉ์˜ ๊ด€๋ จ์ž๋ฃŒ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ํ•™์Šตํ•œ๋‹ค.

https://opentutorials.org/course/3085/18825

 

๋ฐฐ์—ด - ์ƒํ™œ์ฝ”๋”ฉ

์†Œ์Šค์ฝ”๋“œ ๋ณ€๊ฒฝ์‚ฌํ•ญ

opentutorials.org

 

์ถ”๊ฐ€ ์š”๊ตฌ์‚ฌํ•ญ

  • 2๋‹จ์—์„œ 9๋‹จ๊นŒ์ง€ ์ „์ฒด๋ฅผ ์ถœ๋ ฅํ•˜๋Š”๋ฐ ๋งŽ์€ ์ค‘๋ณต ์ฝ”๋“œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
  • ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•ด 2๋‹จ๊ณผ 9๋‹จ์— ๋Œ€ํ•œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ๋‹ค.

ํžŒํŠธ

  • ๋ฐ˜๋ณต๋ฌธ ์•ˆ์— ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ 2์ค‘ ๋ฐ˜๋ณต๋ฌธ์ด๋ผ๊ณ  ํ•œ๋‹ค.

 

 


 

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

- ๊ตฌ๊ตฌ๋‹จ์ด ๋™์ž‘ํ•˜๊ณ  ํ™”๋ฉด์— ๊ฒฐ๊ณผ๊ฐ€ ์ž˜ ์ถœ๋ ฅ.

- ์˜ค๋ฅ˜ : 2~9 ์‚ฌ์ด ์ž์—ฐ์ˆ˜๋งŒ ๊ณจ๋ผ์„œ ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜๊ฐ€ ์—†์Œ.
2๋ฏธ๋งŒ 9์ด์ƒ์˜ ์ˆซ์ž๊ฐ€ ๋“ค์–ด์™”์„ ๊ฒฝ์šฐ ๋‹ค์‹œ ์ž…๋ ฅํ•˜๋ผ๋Š” ์ฐฝ์ด ๋„์›Œ์ง€์ง€๋งŒ, ๊ฒฐ๊ณผ์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋‚จ.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h2>๊ตฌ๊ตฌ๋‹จ : n๋‹จ์„ ์ถœ๋ ฅํ•˜๊ธฐ</h2>
<div id="results"></div>
<script>
    var gugudan = {};
    gugudan.dan = [];
    gugudan.results = [];
    gugudan.length = 9;

    gugudan.run = function () {
        var n = prompt("๋ช‡ ๋‹จ์„ ์ถœ๋ ฅํ• ๊นŒ์š”? (2์™€ 9 ์‚ฌ์ด ์ž์—ฐ์ˆ˜ ์ž…๋ ฅ)");
        if (n < 2 || n > 10) {
            this.dan.length = 0;
            prompt("2์™€ 9 ์‚ฌ์ด ์ž์—ฐ์ˆ˜๋ฅผ ๋‹ค์‹œ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.")
        } else {
            this.dan.length = 0;
            this.dan.push(n);
            for (var i = 1; i <= this.length; i++) {
                var result = n * i;
                this.results.push(result);
            }
        }
    }

    gugudan.show = function () {
        var ret = "";
        ret += "<h2>" + this.dan + "์ถœ๋ ฅ</h2>\n";
        for (var i = 1; i <= this.length; i++) {
            var result = this.results[i - 1];
            ret += this.dan + " ๊ณฑํ•˜๊ธฐ " + i + " ๋Š” " + result + "<br>\n";
        }
        return ret;
    }

    gugudan.run();
    document.getElementById("results").innerHTML = gugudan.show();

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

 

๊ตฌ๊ธ€๋ง) ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”
jamesdreaming.tistory.com/44

-> ๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์ธ ๋ฐฐ์—ด์ด๋ฆ„.length=0;์œผ๋กœ ํ™œ์šฉ.


 

ํ•ด์„ค (์ฝ”๋”ฉ-๋ฐฐ์—ด๋กœ ๊ตฌ๊ตฌ๋‹จ ๊ตฌํ˜„)

-> ๋ฐฐ์—ด'๋งŒ' ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด์—ˆ์Œ!! ใ… ใ… ใ… 
ํ•จ์ˆ˜, ๊ฐ์ฒด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋’ค์— ๋” ๋‚˜์˜ด. 

-----------------

-๋ฐฐ์—ด๋„ ๊ฐ์ฒด์ด๋‹ค(object)
-๋ฐฐ์—ด์€ ๋Œ€๊ด„ํ˜ธ[], ๊ฐ์ฒด๋Š” ์ค‘๊ด„ํ˜ธ{} ๋กœ ํ‘œ์‹œ.

arr = []
๋ฐฐ์—ด.
arr[0] = 10
๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ๊ฐ’์„ 10์œผ๋กœ ํ•จ.
arr.length = 1
๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ์•Œ๋ ค์คŒ. ๋ฐฐ์—ด์˜ ๊ธธ์ด = ๋ฐฐ์—ด์˜ ์›์†Œ์˜ ๊ฐฏ์ˆ˜

-----------------

์ด ์‚ฌ์‹ค๋“ค์„ ๋ณต์Šตํ•˜๊ณ  ๊ตฌ๊ตฌ๋‹จ์„ ๋งŒ๋“ค์–ด ๋ณด์ž.

 

 

<์ „์ฒด ์ •๋‹ต์ฝ”๋“œ>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>๊ตฌ๊ตฌ๋‹จ ์ „์ฒด ์ถœ๋ ฅ</h1>
<script>
    var result = []; // ๋น„์–ด ์žˆ๋Š” ๋ฐฐ์—ด
    for (var n = 2; n <= 9; n++) {

        // calculate
        for (var i = 1; i <= 9; i++) {
            result[i] = n * 1;
        }
        // print
        document.write("<h2>" + n + "๋‹จ</h2>")
        for (var i = 1; i <= 9; i++) {
            var str = "";
            str += n;
            str += " ๊ณฑํ•˜๊ธฐ ";
            str += i;
            str += " ๋Š” "
            str += result[i];
            str += "<br>";
            document.write(str)
        }
    }
</script>
</body>
</html>

 

- ์œ„์™€ ๊ฐ™์€ ๋ฐ˜๋ณต๋ฌธ์—์„œ var i = 1; ๋กœ ์‹œ์ž‘ํ•˜๋ฉด,
๋ฐฐ์—ด์˜ ํŠน์„ฑ์ƒ 0๋ฒˆ์งธ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š”๋ฐ 0์„ ํ™œ์šฉํ•˜์ง€ ๋ชปํ•˜๊ฒŒ ๋œ๋‹ค.
๋”ฐ๋ผ์„œ ๊ฐ€๋Šฅํ•˜๋ฉด var i = 0;๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์งœ๋Š” ๊ฒŒ ์ข‹๋‹ค.

 

-> ์ฝ”๋“œ ์งœ๋Š” ์ˆœ์„œ.
์•„๋ฌด ์ˆซ์ž๋‚˜ ํ•œ ๋‹จ ๊ตฌ๊ตฌ๋‹จ์„ ๋จผ์ € ๊ตฌํ˜„ํ•˜๊ณ , ๊ทธ๊ฒƒ์„ 2~9๊นŒ์ง€ ๋™์ผํ•˜๊ฒŒ ์ ์šฉํ•ด ๊ตฌ๊ตฌ๋‹จ์„ ์™„์„ฑํ•œ๋‹ค.

var result = []; // ๋น„์–ด ์žˆ๋Š” ๋ฐฐ์—ด
var n = 3 // 3๋‹จ ๋จผ์ € ๊ตฌํ˜„
// calculate
for (var i = 1; i <= 9; i++) {
	result[i] = n * 1;
}
// print
document.write("<h2>" + n + "๋‹จ</h2>")
for (var i = 1; i <= 9; i++) {
    var str = "";
    str += n;
    str += " ๊ณฑํ•˜๊ธฐ ";
    str += i;
    str += " ๋Š” "
    str += result[i];
    str += "<br>";
    document.write(str)
}

-> 3๋‹จ์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋จผ์ € ๊ตฌํ˜„ํ•œ๋‹ค.
-> ์ž˜ ์ถœ๋ ฅ๋˜๋ฉด, ์œ„์˜ ์ฝ”๋“œ๋ฅผ 2๋‹จ~9๋‹จ์—๋„ ์ ์šฉํ•œ๋‹ค. (= ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉ)

var result = []; // ๋น„์–ด ์žˆ๋Š” ๋ฐฐ์—ด
    for (var n = 2; n <= 9; n++) {
        // calculate
        for (var i = 1; i <= 9; i++) {
            result[i] = n * 1;
        }
        // print
        document.write("<h2>" + n + "๋‹จ</h2>")
        for (var i = 1; i <= 9; i++) {
            var str = "";
            str += n;
            str += " ๊ณฑํ•˜๊ธฐ ";
            str += i;
            str += " ๋Š” "
            str += result[i];
            str += "<br>";
            document.write(str)
        }
    }

-> ์ฒ˜์Œ ๊ฐœ๋ฐœํ–ˆ๋˜ ๋‘ ๋ฐ˜๋ณต๋ฌธ(calculate, print) ์ „์ฒด๋ฅผ 2์—์„œ 9๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.
-> ๊ตฌ๊ตฌ๋‹จ ์™„์„ฑ!