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

[์ธํ”„๋Ÿฐ] Javascript์ž…๋ฌธ - ์—ฐ์Šต๋ฌธ์ œ3 ์ •๋ฆฌ์™€ ํ”ผ๋“œ๋ฐฑ ๋ณธ๋ฌธ

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

[์ธํ”„๋Ÿฐ] Javascript์ž…๋ฌธ - ์—ฐ์Šต๋ฌธ์ œ3 ์ •๋ฆฌ์™€ ํ”ผ๋“œ๋ฐฑ

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

 

//////////////↑์ •๋‹ต ์ฝ”๋“œ///↓๋‚ด๊ฐ€ ์“ด ์ฝ”๋“œ////////////

์ •๋‹ต ์ฝ”๋“œ ๋ณธ๋ฌธ

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>์†Œ์ˆ˜ ์ฐพ๊ธฐ</title>
</head>
<body>
    <h1>์†Œ์ˆ˜ ๋ชฉ๋ก๋“ค</h1>
    <div id = "result"></div>
    <script>
        var numbers = {};
        numbers.all = [];
        numbers.prime = [];

        numbers.isPrime = function(number) {
            for (var i = 2; i < number; i++) {
                if (number % i == 0) {
                    return false;
                }
            }
            return true;
        };

        numbers.addAllNumber = function(max_num) {
            for (var i = 2; i <= max_num; i++) {
                   this.all.push(i);
            }
        };

        numbers.calcPrimes = function() {
            for (var i = 0; i < this.all.length; i++) {
                var n = this.all[i];
                if (this.isPrime(n)) {
                    this.prime.push(n);
                }
            }
        };

        numbers.getAllPrimeNumbers = function() {
            var ret = "";
            ret += "<h2>์ „์ฒด ์†Œ์ˆ˜์˜ ๊ฐฏ์ˆ˜:" + this.prime.length + "</h2>\n";
            for (var i = 0; i < this.prime.length; i++) {
                ret += (i+1) + "๋ฒˆ์งธ ์†Œ์ˆ˜: " + this.prime[i] + "<br>\n";
            }
            return ret;
        };

        numbers.addAllNumber(10000);
        numbers.calcPrimes();
        document.getElementById('result').innerHTML = numbers.getAllPrimeNumbers();

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

 


 

var numbers = {};
numbers.all = [];
numbers.prime = [];
        
//////////////↑์ •๋‹ต ์ฝ”๋“œ///↓๋‚ด๊ฐ€ ์“ด ์ฝ”๋“œ////////////

numbers = {}; // ๊ฐ์ฒด
numbers.all = []; //์†์„ฑ)์ˆซ์ž ๋ฐฐ์—ด
numbers.prime = []; //์†์„ฑ)์†Œ์ˆ˜ ๋ฐฐ์—ด

 

- ๊ฐ์ฒด ์„ ์–ธํ•  ๋•Œ var ๋ฅผ ๋ถ™์ด์ž.

 

numbers.isPrime = function(number) {
    for (var i = 2; i < number; i++) {
      if (number % i == 0) {
           return false;
      }
    }
    return true;
};
        
//////////////↑์ •๋‹ต ์ฝ”๋“œ///↓๋‚ด๊ฐ€ ์“ด ์ฝ”๋“œ////////////
        
numbers.isPrime = function (number) {
    for (var i = 1; i < number; i++) {
      if (number % i === 0) {
      }
    }
};

numbers.isPrime ์€ ์–ด๋–ค ์ˆซ์ž๊ฐ€ ์†Œ์ˆ˜์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ํŒ๋‹จํ•˜๋Š” ๋ฉ”์„œ๋“œ.

( โ— 1์€ ์†Œ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 2๋ถ€ํ„ฐ ์†Œ์ˆ˜ํ…Œ์ŠคํŠธ๋ฅผ ํ•˜๊ณ , ์ธ์ˆ˜๋„ 2๋ถ€ํ„ฐ ์‹œ์ž‘ํ•จ.)
(var i = 2;)

for - i๋Š” number๋ฏธ๋งŒ, 2 ์ด์ƒ์˜ 1์”ฉ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆ˜.(2,3,4,5..)

if - (number๋ฅผ i๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ 0์ด๋ฉด) ์†Œ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ค.
์ฆ‰ 1๊ณผ ์ž๊ธฐ ์ž์‹  ์™ธ์— ๋‚˜๋ˆ„์–ด๋–จ์–ด์ง€๋Š” ์ธ์ˆ˜๊ฐ€ ์žˆ๋‹ค = ์†Œ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ false๋ฅผ ๋ฐ˜ํ™˜ return false;

i๋Š” number๋ฏธ๋งŒ, 2 ์ด์ƒ์˜ 1์”ฉ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆ˜ (2,3,4,5..) ๋ชจ๋‘์— ๋Œ€ํ•ด
i๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ 0์ด ์•„๋‹ˆ๋ฉด

์ฆ‰ ์œ„์˜ ๋ฐ˜๋ณต๋ฌธ&์กฐ๊ฑด๋ฌธ ๋ชจ๋‘์— ๋ถ€ํ•ฉํ•˜์ง€ ์•Š์•„ false๊ฐ€ ๋ฐ˜ํ™˜๋˜์ง€ ์•Š์œผ๋ฉด
๋ฆฌํ„ด๊ฐ’์œผ๋กœ true๋ฅผ ๋ฐ˜ํ™˜ํ•จ. return true;
( โ— ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— return true;๋Š” for๋ฌธ, if๋ฌธ ๋ฐ”๊นฅ์— ์žˆ์Œ!! for๋ฌธ์—๋„ if๋ฌธ์—๋„ ํ•ด๋‹น๋˜์ง€ ์•Š์•„์•ผ๋งŒ ์†Œ์ˆ˜์ด๋ฏ€๋กœ)

 

 

numbers.addAllNumber = function(max_num) {
            for (var i = 2; i <= max_num; i++) {
                   this.all.push(i);
            }
        };

//////////////↑์ •๋‹ต ์ฝ”๋“œ///↓๋‚ด๊ฐ€ ์“ด ์ฝ”๋“œ////////////

numbers.addAll = function (max_num) {
            for (var i = 1; i < max_num; i++) {
                this.all[i] = i;
            }
        };

numbers.addAllNumber ์€ number.all์ด๋ผ๋Š” ํ”„๋กœํผํ‹ฐ (๋ฐฐ์—ด)์— 1๋ถ€ํ„ฐ 10000์‚ฌ์ด์˜ ๋ชจ๋“  ์ˆซ์ž๋ฅผ ์ง‘์–ด๋„ฃ๋Š” ๋ฉ”์„œ๋“œ.

( โ— 1์€ ์†Œ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 1์ด ์•„๋‹Œ 2 ์ด์ƒ์˜ ์ˆซ์ž์— ๋Œ€ํ•ด ์†Œ์ˆ˜ํ…Œ์ŠคํŠธ๋ฅผ ํ•จ.)
(var i = 2;)

- 10000(max_num)์— ๋Œ€ํ•ด์„œ๋„ ์†Œ์ˆ˜ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด์•ผ ํ•˜๋ฏ€๋กœ i <= max_num; ์ด๋‹ค.

- 2๋ถ€ํ„ฐ 10000๊นŒ์ง€ ๋ชจ๋“  ์ž์—ฐ์ˆ˜๋ฅผ numbers.all์ด๋ผ๋Š” ๋ฐฐ์—ด์— ๋„ฃ๋Š”๋‹ค. this.all.push(i)

โ€ป this.all[i] = i; ๋กœ๋„ ๊ฐ™์€ ๋ฐฐ์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ธด ํ•˜๋‹ค!

 

 

 

numbers.calcPrimes = function() {
    for (var i = 0; i < this.all.length; i++) {
        var n = this.all[i];
        if (this.isPrime(n)) {
        	this.prime.push(n);
        }
    }
};

//////////////↑์ •๋‹ต ์ฝ”๋“œ///↓๋‚ด๊ฐ€ ์“ด ์ฝ”๋“œ////////////

numbers.addPrime = function () {
};// ๋ฉ”์„œ๋“œ)์†Œ์ˆ˜๋ผ๋ฉด ์†Œ์ˆ˜ ๋ฐฐ์—ด์— ์ถ”๊ฐ€

numbers.calcPrimes์€
numbers.isPrime(number)์—์„œ ์†Œ์ˆ˜๋กœ ํŒ๋‹จ๋œ ์ˆซ์ž๋“ค์„
numbers.prime์ด๋ผ๋Š” ํ”„๋กœํผํ‹ฐ (์†Œ์ˆ˜ ๋ฐฐ์—ด) ์— ๋„ฃ๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.

๊ฐ€์žฅ ์–ด๋ ค์šธ ์ค„ ์•Œ์•˜๋Š”๋ฐ ์ƒ๊ฐ๋ณด๋‹ค ๊ฐ„๋‹จํ•˜๋‹ค.
numbers.isPrime์—์„œ ๋ฆฌํ„ด๊ฐ’์ด true์ธ ์ˆซ์ž๋“ค๋งŒ numbers.prime์— ๋„ฃ์œผ๋ฉด ๋œ๋‹ค.
๊ทผ๋ฐ ๋ฆฌํ„ด๊ฐ’์ด true์ธ๊ฑธ ์–ด๋–ป๊ฒŒ ํŒ๋ณ„ํ•ด์„œ ๋„ฃ๋‚˜?.. ํ•˜๊ณ  ํ•œ์ฐธ ๊ณ ๋ฏผํ–ˆ๋Š”๋ฐ
this.isPrime(n)์˜ ๋ฆฌํ„ด๊ฐ’ ์ž์ฒด๊ฐ€ true์ด๋ฉด ์ด ์ž์ฒด๊ฐ€ if๋ฌธ์˜ ์กฐ๊ฑด์‹์ด ๋˜๋Š” ๊ฑฐ์˜€๋‹ค. '0'...!!

 

-for
i๋Š” 0๋ถ€ํ„ฐ this.all.length(numbers.all์ด๋ผ๋Š” ์ˆซ์ž ๋ฐฐ์—ด์˜ ๊ธธ์ด๋งŒํผ = ๋ฐฐ์—ด ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€!) ๊นŒ์ง€ 1์”ฉ ์ฆ๊ฐ€ํ•˜๋ฉฐ ๋ฐ˜๋ณต.
n์ด๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ this.all์˜ [i]๋ฒˆ์งธ ๊ฐ’์œผ๋กœ ํ•œ๋‹ค.

-if
๋งŒ์•ฝ n์˜ ์†Œ์ˆ˜ ํŒ๋ณ„ ๊ฒฐ๊ณผ(numbers.isPrime์˜ ๋ฆฌํ„ด๊ฐ’)๊ฐ€ true์ด๋ฉด -> (this.isPrime(n)) 
numbers.prime์ด๋ผ๋Š” ๋ฐฐ์—ด์˜ ๋งจ ๋์— n์„ ๋„ฃ๋Š”๋‹ค.
(๋ฐ˜๋Œ€๋กœ this.isPrime(n)์˜ ๊ฒฐ๊ณผ๊ฐ€ false์ด๋ฉด, ์กฐ๊ฑด๋ฌธ์ด false์ด๋ฏ€๋กœ ๊ณง๋ฐ”๋กœ ์กฐ๊ฑด๋ฌธ์„ ๋น ์ ธ๋‚˜๊ฐ„๋‹ค.)
โ— ์ด๋Ÿฐ ์ฝ”๋”ฉ์€ numbers.isPrime()์˜ ๋ฆฌํ„ด๊ฐ’์„ true/fasle๋กœ ์ •ํ•ด ๋‘์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋Šฅํ•œ ๊ฒƒ!

 

 

numbers.getAllPrimeNumbers = function() {
            var ret = "";
            ret += "<h2>์ „์ฒด ์†Œ์ˆ˜์˜ ๊ฐฏ์ˆ˜:" + this.prime.length + "</h2>\n";
            for (var i = 0; i < this.prime.length; i++) {
                ret += (i+1) + "๋ฒˆ์งธ ์†Œ์ˆ˜: " + this.prime[i] + "<br>\n";
            }
            return ret;
        };
        
//////////////↑์ •๋‹ต ์ฝ”๋“œ///↓๋‚ด๊ฐ€ ์“ด ์ฝ”๋“œ////////////

 numbers.getAllPrimes = function () {
           
        };// ๋ฉ”์„œ๋“œ) ์†Œ์ˆ˜ ์ „์ฒด ๊ฐ€์ ธ์˜ค๊ธฐ

numbers.getAllPrimeNumbers๋Š” ๊ณ„์‚ฐ๋œ ์ „์ฒด ์†Œ์ˆ˜์˜ ๊ฐฏ์ˆ˜์™€ n๋ฒˆ์งธ ์†Œ์ˆ˜ ์ •๋ณด๋“ค์„ ์ถœ๋ ฅํ•ด ์ฃผ๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.

ret ์ด๋ผ๋Š” ๋น„์–ด ์žˆ๋Š” ๊ฐ’์„ ๋งŒ๋“ ๋‹ค. var ret = "";
ret ์— "<h2>์ „์ฒด ์†Œ์ˆ˜์˜ ๊ฐฏ์ˆ˜:"+this.prime.length+"</h2>\n";์„ ๋”ํ•œ ๊ฒƒ์„ ret์œผ๋กœ ํ•œ๋‹ค. 
(์ „์ฒด ์†Œ์ˆ˜์˜ ๊ฐฏ์ˆ˜ = numbers.prime ๋ฐฐ์—ด์˜ ๊ธธ์ด ์ด๋ฏ€๋กœ)

numbers.prime ์˜ ์ „์ฒด ์†Œ์ˆ˜๋ฅผ ๋ชจ๋‘ ์ถœ๋ ฅํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•œ๋‹ค.

-for
0๋ฒˆ์งธ๋ถ€ํ„ฐ ์„ธ์•ผ ํ•˜๋ฏ€๋กœ 0์œผ๋กœ ์‹œ์ž‘. var i = 0;
๋ฐ˜๋ณต์ด ๋๋‚˜๋Š” ๊ฑด numbers.prime์˜ ๊ธธ์ด-1๊นŒ์ง€์ด๋ฏ€๋กœ i<this.prime.length;
( โ— ์†Œ์ˆ˜ ๋ฐฐ์—ด์˜ 1๋ฒˆ์งธ๋ถ€ํ„ฐ ๋๊นŒ์ง€ === 0 ๋ฒˆ์งธ์—์„œ ๋-1๊นŒ์ง€.
๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— i<=this.prime.length;๊ฐ€ ์•„๋‹Œ i<this.prime.length;์ด๋‹ค.
<= ๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค์ง€๋งŒ, ์ •ํ™•ํ•œ ์ฝ”๋“œ๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ ์ง€์–‘ํ•˜์ž.) 
ret ์— (i+1) + "๋ฒˆ์งธ ์†Œ์ˆ˜: " + this.prime[i] + "<br>\n"; ๋ฅผ ๋”ํ•œ ๊ฒƒ์„ ret์œผ๋กœ ํ•œ๋‹ค.

๋ฐ˜๋ณต๋ฌธ์ด ๋๋‚˜๋ฉด ret๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

โ“ \n (์—ญ์Šฌ๋ž˜์‹œ n)์ด๋ž€?
์ค„๋ฐ”๊ฟˆ์˜ ๊ธฐ๋Šฅ์„ ํ•œ๋‹ค. 

 

 

 

 

numbers.addAllNumber(10000);
numbers.calcPrimes();
document.getElementById('result').innerHTML = numbers.getAllPrimeNumbers();

/// ๋‚ด๊ฐ€ ์ง  ์ฝ”๋“œ - ์—†์Œ

๋งˆ๋ฌด๋ฆฌ ์ฝ”๋“œ.
๋งŒ๋“  ๋ฉ”์„œ๋“œ๋“ค์„ ์‹คํ–‰ํ•ด ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

numbers.addAllNumber(number)์— 10000์„ ์ž…๋ ฅํ•ด ์‹คํ–‰ํ•œ๋‹ค.
numbers.calcPrimes()์„ ์‹คํ–‰ํ•œ๋‹ค.
์›น ๋ฌธ์„œ์˜ 'result'๋ž€์— numbers.getAllPrimeNumbers()์˜ ๊ฒฐ๊ณผ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

โ“ .innerHTML์ด๋ž€ ?
์„ ํƒํ•œ ์˜์—ญ ๋‚ด์—์„œ html ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.
๋ณดํ†ต document.getElementById("~").innerHTML = ์‹คํ–‰ํ•  html์ฝ”๋“œ; ํ˜•ํƒœ๋กœ ๋งŽ์ด ์“ฐ์ธ๋‹ค.
id๊ฐ’์ด "~"์ธ ์˜์—ญ์— ์šฐํ•ญ์˜ html์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ผ๋Š” ์˜๋ฏธ.

 

 

 

๐Ÿ‘พํ•œ ์ค„์”ฉ ๋”ฐ๋ผ ์นœ ์™„์„ฑ ์ฝ”๋“œ๊ฐ€ ๋™์ž‘ํ•˜์ง€ ์•Š์•„์„œ ๋””๋ฒ„๊น….

- numbers.addAllNumber ์ฝ”๋“œ ์ค‘ 
this.all.push(i);๋ฅผ
this.all.push = i ์ด๋ ‡๊ฒŒ ์จ ๋†“์•˜๋‹ค. ใ…‹ใ…‹ใ… ใ… ..

 

 

์•ž์œผ๋กœ ์œ„์™€ ๊ฐ™์€ ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์— ๋” ์ต์ˆ™ํ•ด์ ธ์•ผ๊ฒ ๋‹ค!!