๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ

์˜ค๋‹ต๋…ธํŠธ

by Youcodein 2022. 10. 4.
728x90
๋ฐ˜์‘ํ˜•

javascript

Javascript Test01

01. ๊ฒฐ๊ด๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.


{
    function func(){
        for( i=1; i<=7; i++){
            for( j=1; j<=i; j++){
                document.write(j);
            }
            document.write("<br>");
        }
    }
    func();
}

์ค‘์ฒฉ for๋ฌธ์— ๋Œ€ํ•œ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. i๊ฐ€ 1์”ฉ ์ฆ๊ฐ€ํ•จ์œผ๋กœ j๋ฅผ ์ถœ๋ ฅํ•  ๋•Œ ํ•œ์ž๋ฆฌ์”ฉ ๋Š˜์–ด๋‚ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๋‹ต์€ ์•„๋ž˜์— ๋‚˜์˜ค๋Š”๋Œ€๋กœ ๋‚˜์˜ต๋‹ˆ๋‹ค.

๊ฒฐ๊ณผ ๋ณด๊ธฐ

02. ๊ฒฐ๊ด๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

{
    function func() {
        let i = 10, hap = 0;
        while (i > 1) {
            if (i % 3 == 1) {
                hap += i;1
            }
        }
        document.write(hap);
    }
    func();
}

i๋Š” 10์—์„œ 1์”ฉ ๊ฐ์†Œํ•ฉ๋‹ˆ๋‹ค. while๋ฌธ์˜ ์กฐ๊ฑด์—์„œ i๋Š” 1๋ณด๋‹ค ํด ๋•Œ if๋ฌธ์˜ ์กฐ๊ฑด, i๋ฅผ 3์œผ๋กœ ๋‚˜๋ˆ„์—ˆ์„ ๋•Œ ๋‚˜๋จธ์ง€๊ฐ€ 1์ด ๋‚˜์˜ค๋ฉด hap์˜ ๊ฐ’์— ์ด์ „ i์˜ ๊ฐ’์„ ๋”ํ•ฉ๋‹ˆ๋‹ค. i๋ฅผ 3์œผ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ’์ด 1์ด ๋‚˜์˜ค๋Š” ๊ฒƒ์€ 7,4,1๊ฐ€ ๋˜๊ณ  ํ•ฉ์€ 12์ž…๋‹ˆ๋‹ค.

๊ฒฐ๊ณผ ๋ณด๊ธฐ

03. ๊ฒฐ๊ด๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

{
    function func(){
        let a = [1,2,3,4,5,6,7,8,9], cnt = 0;

        for( i=0; i<a.length; i++){
            if(a[i] % 2 == 0){
                cnt = cnt + 1;
            }
        }
        document.write(cnt);
    }
    func();
}   document.write(z);
}

for๋ฌธ์— ๋ฐฐ์—ด์— ๋Œ€ํ•œ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. i<9 ์ผ ๋•Œ a[i]๋ฅผ 2๋กœ ๋‚˜๋ˆ„์—ˆ์„๋•Œ ๋‚˜๋จธ์ง€๊ฐ€ 0์ด๋ฉด cnt์— 1์”ฉ ๋”ํ•ฉ๋‹ˆ๋‹ค. ์กฐ๊ฑด์— ๋งž๋Š” a[i]=2,4,6,8์ด๋ฏ€๋กœ 1๋ฅผ ๋„ค ๋ฒˆ ๋”ํ•ฉ๋‹ˆ๋‹ค.

๊ฒฐ๊ณผ ๋ณด๊ธฐ

04. ๊ฒฐ๊ด๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

{                    
    function func(data, exp){
        let result = 1;
        for(i=1; i < exp; i++){
            result = result * i;
        }
        document.write(result + data);
    }
    func(1, 5);
}

data, exp๋ณ€์ˆ˜์— 1, 5 ๊ฐ’์„ ๋Œ€์ž…ํ•˜๋ฉด for๋ฌธ์ด 1~4๊นŒ์ง€๋กœ ์ง€์ •๋˜์–ด ์ˆœ์„œ๋Œ€๋กœ 1, 2, 6, 24๊ฐ€ ๋‚˜์˜ค๋ฏ€๋กœ ์ตœ์ข…๊ฐ’์ธ 24๊ฐ€ result๊ฐ’์ด ๋ฉ๋‹ˆ๋‹ค. ์ตœ์ข…๊ฐ’ 24์— data = 1์„ ๋”ํ•ด์ฃผ๋ฉด ์ •๋‹ต์€ 25๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

๊ฒฐ๊ณผ ๋ณด๊ธฐ

05. ๊ฒฐ๊ด๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

{
    function func(){
        let a = [2, 3, 2, 4, 5, 6, 7, 2, 3, 3, 2];
        let value = 0;
        for(i = 0; i < a.length; i++){
            if(a[i] != 2){
                value++
            }
        }
        document.write(value);
    }
    func();
}

if(๋ฐฐ์—ด a์˜ ์ธ๋ฑ์Šค๊ฐ’ i์— ์œ„์น˜ํ•œ ์ˆ˜๊ฐ€ 2๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด) value๊ฐ’์— 1์”ฉ ๋”ํ•ด์ค๋‹ˆ๋‹ค. [3, 4, 5, 6, 7, 3, 3] ์ด 7๊ฐœ ์ด๋ฏ€๋กœ, value๊ฐ’์€ 1์„ 7๋ฒˆ ๋”ํ•œ 7์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

๊ฒฐ๊ณผ ๋ณด๊ธฐ

06. ๊ฒฐ๊ด๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

{
    function func(){
        let i, j = 0;
        for( i=0; i<5; i++){        
            j += i;                    
        }
        document.write(i);  
        document.write(j);    
    }
    func();
}

i๊ฐ€ 5๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ๊ณ„์† ๋Š˜์–ด๋‚˜๋Š” i๊ฐ’์„ ๋”ํ•ด j์— ์ €์žฅ์‹œ์ผœ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๊ฒฐ๊ณผ ๋ณด๊ธฐ

07. ๊ฒฐ๊ด๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

function func(){
    let a = 12, b = 8, c = 2, d = 3;
    a /= b - c * d;
    document.write(a);
    document.write(a*b);
}
func();

a, b, c, d ๋ณ€์ˆ˜์— ๊ฐ ๊ฐ’์„ ์ €์žฅ, 8 - 2 * 3 = 2 ๊ฐ’์œผ๋กœ a๊ฐ’์„ ๋‚˜๋ˆ„๋ฉด 6์ด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  a์— b๋ฅผ ๊ณฑํ•˜๋ฉด 48์ด ๋‚˜์˜ต๋‹ˆ๋‹ค.

๊ฒฐ๊ณผ ๋ณด๊ธฐ

08. ๊ฒฐ๊ด๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

function func(){
    let a = 0, sum = 0;
    while( a < 10 ){  
        a++;       
        if(a % 2 == 1) continue;        
        sum += a;                      
    }
    document.write(sum);
}
func();

a๊ฐ€ 10์ด ๋  ๋•Œ๊นŒ์ง€ a์˜ ๊ฐ’์„ ์ฆ๊ฐ€์‹œํ‚ค๊ณ 
๋งŒ์•ฝ a๋ฅผ 2๋กœ ๋‚˜๋ˆ„์—ˆ์„ ๋•Œ ๋‚˜๋จธ์ง€๊ฐ€ 1์ด ๋˜๋ฉด ๊ฑด๋„ˆ๋œ๋‹ˆ๋‹ค.
๊ฑด๋„ˆ๋›ด ๊ฐ’์„ ์ œ์™ธํ•จ a์˜ ๊ฐ’์„ sum์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
a๋ฅผ 2๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ 1์ด ๋˜๋Š” ๊ฐ’ == 1, 3, 5, 7, 9 ์ฆ‰, ํ™€์ˆ˜๋ฅผ ๊ฑด๋„ˆ๋›ฐ๋ฉด
2+4+6+8+10 = 30

๊ฒฐ๊ณผ ๋ณด๊ธฐ

09. ๊ฒฐ๊ด๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.


    {
        function func(){
            let i = 0, sum = 0;
            while( 1 ){ 
                i++;
                if( i > 10 ) break;
                if( i % 5 == 0) continue;
                sum += i;
            }
            document.write(sum);
        }
        func();
    }

while(1)์€ true์ด๋‹ˆ ์‹คํ–‰์‹œํ‚ต๋‹ˆ๋‹ค.
i์˜๊ฐ’์€ 1++๋กœ 1๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
์ฒซ๋ฒˆ์งธ if๋ฌธ์„ ์ดํ›„์˜ i์˜ ๊ฐ’์€ 1~10
๋‘๋ฒˆ์งธ if๋ฌธ์„ ๋งŒ์กฑํ•˜๋Š” ๊ฐ’์€ 5, 10์œผ๋กœ ์ด๋ฅผ ์ œ์™ธํ•˜๊ณ  ๋”ํ•ฉ๋‹ˆ๋‹ค.
๋‹ต์€ 1+2+3+4+6+7+8+9๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

๊ฒฐ๊ณผ ๋ณด๊ธฐ

10. ๊ฒฐ๊ด๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

{
    let num = 0;

    while(num < 2000){
        num++;
        document.write(num);
        if(num == 10){
            break;
        }
    }
}

2000์ด๋ž€ ์ˆซ์ž๋Š” ํ•จ์ •์ž…๋‹ˆ๋‹ค!
๋ณ€์ˆ˜ num = 0์€ ์ฒ˜์Œ์— ++๋˜๊ธฐ ๋•Œ๋ฌธ์— 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
num์ด 10์ด ๋ ๋•Œ๊นŒ์ง€ 1์”ฉ ์ฆ๊ฐ€์‹œํ‚ต๋‹ˆ๋‹ค.
num์ด 10์ด ๋˜๋ฉด ๋ฉˆ์ถฅ๋‹ˆ๋‹ค.

๊ฒฐ๊ณผ ๋ณด๊ธฐ

11. ๊ฒฐ๊ด๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

{
    function solution(a, b) {
        let num = a > b;
        return num;
    }
    document.write(solution(5, 9));
}

5>9

๊ฒฐ๊ณผ ๋ณด๊ธฐ

12. ๊ฒฐ๊ด๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

{
    function solution(text) {
        return text.split("").join("/").replace("s", "site");
    }
    document.write(solution("webs"));
}

1. split์œผ๋กœ ํ•œ๊ธ€์ž์”ฉ ๋‚˜๋ˆˆ๋‹ค
2. join์œผ๋กœ ๋ถ™์ธ๋‹ค (์‚ฌ์ด์— / ๋„ฃ๋Š”๋‹ค)
3. replace๋กœ s๋ฅผ site๋กœ ์น˜ํ™˜ํ•œ๋‹ค
1. w,e,b,s
2. w/e/b/s
3. w/e/b/site

๊ฒฐ๊ณผ ๋ณด๊ธฐ

13. ๊ฒฐ๊ด๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

function func(){
    let a = [1,2,3,4,5,6,7,8,9], cnt = 0;

    for( i=0; i<a.length; i++ ){     
        if( a[i] % 2 == 0){            
            cnt = cnt + 1;
        }                              
    }
    document.write(cnt);                
}
func();

1. i๊ฐ€ 9๋ฅผ ๋„˜์ง€ ์•Š๋Š” 8์ด ๋ ๋•Œ๊นŒ์ง€ i์˜ ๊ฐ’์ด ๊ณ„์† ์ถ”๊ฐ€๋œ๋‹ค.
2. ์กฐ๊ฑด์— ๋งž๋Š” a[i]์˜ ๊ฐ’ ์ค‘ 2๋กœ ๋‚˜๋ˆ„์—ˆ์„๋•Œ ๋‚˜๋จธ์ง€๊ฐ€ 0์ธ ๊ฐ’
3. if ์กฐ๊ฑด์— ๋งž๋Š” ํšŸ์ˆ˜๋งŒํผ 1์”ฉ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
4. ํ•ด๋‹น ์กฐ๊ฑด์„ ์ถฉ์กฑํ•˜๋Š” ์ˆ˜ == 2,4,6,8 ์ด 4๊ฐœ

๊ฒฐ๊ณผ ๋ณด๊ธฐ

14. ๊ฒฐ๊ด๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

function func(data, exp){   
    let result = 1;
    for( i=1; i<exp; i++){   
        result = result * i;
    }
    document.write(result + data);     
}        
func(1, 5);

1. data == 1 / exp == 5 ๋Œ€์ž…
2. i๊ฐ€ 4๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ for๋ฌธ ์‹ ๋ฐ˜๋ณต
3. 1, 2, 6, 24
4. 24+1 = 25

๊ฒฐ๊ณผ ๋ณด๊ธฐ

15. ๊ฒฐ๊ด๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

{
    function func(){
        let a = [2,3,2,4,5,6,7,2,3,3,2];
        let value = 0;
        for( i=0; i<a.length; i++){      
            if( a[i] != 2 ){    
                value++;       
            }
        }
        document.write(value);  

    }
    func();
}

1. i๊ฐ€ 10์ด ๋ ๋•Œ๊นŒ์ง€ a ๋ฐฐ์—ด ์ค‘ 2๊ฐ€ ์•„๋‹Œ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์œผ๋ฉด
2. value์ˆ˜๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ๋ผ
3. 2 ์•„๋‹Œ ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜๋Š” 7๊ฐœ
4. ์ •๋‹ต์€ 7

๊ฒฐ๊ณผ ๋ณด๊ธฐ

16. ๊ฒฐ๊ด๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

{
    function func(){
        let i, j = 0;

        for( i=0; i<=8; i++){  
            j += i;                
        }
        document.write(j+i)        
    }
    func();
}

1. i๊ฐ€ 8์ด ๋  ๋•Œ๊นŒ์ง€ ๊ณ„์† ๋Š˜์–ด๋‚˜๋Š” i๊ฐ’์„ j์— ์ €์žฅ์‹œํ‚ต๋‹ˆ๋‹ค
2. j == 0+1+2+3+4+5+6+7+8
3. i์˜ ๊ฐœ์ˆ˜๋Š” 0๋ถ€ํ„ฐ 8๊นŒ์ง€ ์ด 9๊ฐœ
4. 36 + 9 == 45

๊ฒฐ๊ณผ ๋ณด๊ธฐ

17. ๊ฒฐ๊ด๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

{
    function func(){
        let a = 12, b = 8, c = 2, d = 3;
        a /= b - c * d;

        document.write(a);  // 6
    }
    func();
}

๋‹จ์ˆœ ์‚ฌ์น™์—ฐ์‚ฐ

๊ฒฐ๊ณผ ๋ณด๊ธฐ

18. ๊ฒฐ๊ด๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

{
    function func13(){
        let a, b, c, result;
        a = 10;
        b = 20;
        c = 30;
        result = a < b ? b/a : c;

        document.write(result);         
    }
    func13();
}

1. 10๋ณด๋‹ค 20์ด ํด ๊ฒฝ์šฐ b๋ฅผ a๋กœ ๋‚˜๋ˆ ๋ผ == 20/10 || ์•„๋‹Œ ๊ฒฝ์šฐ c ์ถœ๋ ฅ
2. 20์„ 10์œผ๋กœ ๋‚˜๋ˆ„๋ฉด 2๊ฐ€ ๋จ

๊ฒฐ๊ณผ ๋ณด๊ธฐ

19. ๊ฒฐ๊ด๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

{
    function func(){
        let a = 0, sum = 0;
        while( a < 10 ){  
            a++;              
            if(a % 2 == 1) continue;      
            sum += a;                  
        }
        document.write(sum); 
    }
    func();
}

1. a๊ฐ€ 10์ด ๋ ๋•Œ๊นŒ์ง€ a๊ฐ’ ์ฆ๊ฐ€
2. a๋ฅผ 2๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ 1์ด ๋˜๋Š” ๊ฐ’ ๊ฑด๋„ˆ๋›ฐ๊ณ  ๊ทธ ๊ฐ’์„ ์ œ์™ธํ•œ a๊ฐ’ sum์— ์ €์žฅ
3. a๋ฅผ 2๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ 1์ด ๋˜๋Š” ๊ฐ’ == 1, 3, 5, 7, 9 ์ด๊ฒƒ๋“ค, ์ฆ‰ ํ™€์ˆ˜ ๊ฑด๋„ˆ๋œ€
4. 2+4+6+8+10 == 30

๊ฒฐ๊ณผ ๋ณด๊ธฐ

20. ๊ฒฐ๊ด๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

{
    function func(){
        let i = 0, sum = 0;
        while( 0 ){
            i++;
            if( i > 10 ) break;
            if( i % 5 == 0) continue;
            sum += i;
        }
        document.write(sum);
    }
    func();
}

ํ•จ์ •๋ฌธ์ œ! 0์ด ๋“ค์–ด๊ฐ€๊ธฐ ๋•Œ๋ฌธ์— ์‹คํ–‰์ด ๋˜์ง€ ์•Š๋Š”๋‹ค. ์ •๋‹ต์€ 0

๊ฒฐ๊ณผ ๋ณด๊ธฐ

21. ๊ฒฐ๊ด๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

{
    function func(s){
        let answer = "";
        for(let x of s){        
            if( x != 'w') answer += '#'; 
            else answer += x;     
        }
        return answer;   
    }
    
    let str = "webstoryboy";
    document.write(func(str));
}

1. x ๋ณ€์ˆ˜์— "webstoryboy" ๋ฌธ์ž์—ด ์ €์žฅ
2. x ์ค‘ 'w'๊ฐ€ ์•„๋‹Œ ๊ฒƒ๋“ค์„ '#'์œผ๋กœ ๋ฐ”๊พผ๋‹ค
3. "webstoryboy" ์ค‘ w๊ฐ€ ์•„๋‹Œ ๊ฑธ ์ „๋ถ€ #์œผ๋กœ ๋ฐ”๊ฟˆ

๊ฒฐ๊ณผ ๋ณด๊ธฐ

22. ๊ฒฐ๊ด๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

{
    function func(s, t){
        let answer = 0;
        for(let x of s){
            if(x !== t) answer++;
        }
        return answer; 
    }
    
    let str = "wbestoryboy";
    document.write(func(str, 't'));
}

1. x ๋ณ€์ˆ˜์— "webstoryboy" ๋ฌธ์ž์—ด ์ €์žฅ
2. x ์ค‘ 't'๊ฐ€ ์•„๋‹Œ ๊ธ€์ž์ˆ˜๋งŒํผ answer๊ฐ’ 1์”ฉ ์ฆ๊ฐ€์‹œํ‚ค๊ธฐ
3. t ์•„๋‹Œ ๊ธ€์ž๊ฐ€ ์ด 10์ž

๊ฒฐ๊ณผ ๋ณด๊ธฐ

23. ๊ฒฐ๊ด๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

{
    function solution(n) {
        var answer = [];
        answer = n.toString().split('').reverse().map(val => parseInt(val));

        return answer;
    }
    
    document.write(solution(12345))
}

1. n์„ toString์œผ๋กœ ์ˆซ์ž์—์„œ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊ฟˆ
2. split ์•ˆ์— ''๋ฅผ ๋„ฃ์Œ -> ํ•˜๋‚˜ํ•˜๋‚˜ ์ชผ๊ฐœ์ง ['1', '2', '3', '4', '5'] ์ด๋Ÿฐ ์‹์œผ๋กœ...
3. reverse๋ฅผ ํ†ตํ•ด ๋ฐฐ์—ด ์•ˆ์˜ ๊ฐ’์„ ์ „๋ถ€ ๊ฑฐ๊พธ๋กœ ์ •๋ ฌํ•จ ['5', '4', '3', '2', '1']
4. map(val => parseInt(val)) : ๋‹ค์‹œ ๋ฌธ์ž์—ด์„ ์ˆซ์ž(์ •์ˆ˜)๋กœ ๋ณ€ํ™˜ํ•˜๊ณ 
5. ๋ฐฐ์—ด(๊ด„ํ˜ธ ์•ˆ์— ๋ฐ์ดํ„ฐ)ํ˜•ํƒœ๊ฐ€ ์•„๋‹Œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•ด๋ƒ„
6. ์ฆ‰, 5,4,3,2,1 ์ด๋ ‡๊ฒŒ ์ถœ๋ ฅํ•จ.

๊ฒฐ๊ณผ ๋ณด๊ธฐ

23. ๊ฒฐ๊ด๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

{
    function solution(arr, divisor) {
        var answer = [];
        
        const div = arr.filter( el => el % divisor == 0 );    
        answer = div.length > 0 ? div.sort( (a, b ) => a - b ) : [-1];
        
        return answer
    }
            
    document.write(solution([1,2,3,4,5], 2))
}

1. filter๋กœ 2๋กœ ๋‚˜๋ˆด์„ ๋•Œ ๋‚˜๋จธ์ง€๊ฐ’์ด 0์ธ (์ฆ‰ 2์˜ ๋ฐฐ์ˆ˜์ธ) ๊ฐ’์„ ๊ณจ๋ผ๋ƒ„ == 2, 4
2. div.length(2) > 0์ด ์ฐธ์ž„ : ์ฐธ์ด๋ฉด div์•ˆ์˜ ์ˆœ์„œ๋ฅผ a-b ์ˆœ์„œ๋กœ ์ •๋ ฌํ•˜๊ณ  ๊ฑฐ์ง“์ด๋ฉด -1 ์ถœ๋ ฅ
3. a-b ์ˆœ์„œ๋Œ€๋กœ [2, 4] ์ถœ๋ ฅ๋จ

๊ฒฐ๊ณผ ๋ณด๊ธฐ

23. ๊ฒฐ๊ด๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

{
    function solution(numbers) {
        let answer = 0;
        for(let i=0; i<10; i++){ 
            if(!numbers.includes(i)){   
                answer += i;            
            }
        }
        return answer;
    }
    document.write(solution([1,2,3,4,6,7,8,0]))
}

1. i ์•ˆ์— 0๋ถ€ํ„ฐ 9๊นŒ์ง€
2. 0๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ์ˆซ์ž ์ค‘์— numbers ๋ฐฐ์—ด ์•ˆ์— ์—†๋Š” ๊ฐ’ == 5,9
3. 5+9 == 14

๊ฒฐ๊ณผ ๋ณด๊ธฐ

23. ๊ฒฐ๊ด๊ฐ’์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

{
    function solution(n) {
        for(let i=1; i<n; i++) { 
            if(n%i === 1) return i;    
        }
    }
    document.write(solution(10)) 
}

1. i ์•ˆ์— 0๋ถ€ํ„ฐ 9๊นŒ์ง€
2. 10์„ 1~9๋กœ ๋‚˜๋ˆด์„ ๋•Œ ๋‚˜๋จธ์ง€๊ฐ€ 1์ธ ๊ฒฝ์šฐ i๋ฅผ ์ถœ๋ ฅ == 10์„ 3,9๋กœ ๋‚˜๋ˆŒ ๋•Œ 1์ด ๋‚จ์Œ
3. return ์œ„์น˜๊ฐ€ for๋ฌธ ์•ˆ์— ์žˆ์œผ๋ฏ€๋กœ 3์ด ๋‚˜์™”์„ ๋•Œ ๋ฐ”๋กœ ์ข…๋ฃŒ์‹œ์ผœ์„œ 9๋Š” ๋ฌด์‹œํ•˜๊ณ  3์ด ์ถœ๋ ฅ๋จ

๊ฒฐ๊ณผ ๋ณด๊ธฐ
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€