๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

ํ”„์—” ๊ณต๋ถ€/๐Ÿซง ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€

[์•Œ๊ณ ๋ฆฌ์ฆ˜ || ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ฆฌ์ŠคํŠธ ์ž๋ฅด๊ธฐ

728x90

๋ฌธ์ œ๋ฅผ ์ฝ๊ณ ๋‚˜์„œ ์ด๊ฑฐ์Šจ switch ๋ฌธ์„ ์“ฐ๊ธฐ ๋”ฑ ์ข‹๋‹ค! ๋ผ๊ณ  ์ƒ๊ฐํ•จ. 

 

function solution(n, slicer, num_list) {
    let result = [];
    switch (n) {
        case 1:
            result = num_list.slice(0, slicer[1] + 1);
            break;
        case 2:
            result = num_list.slice(slicer[0]);
            break;
        case 3:
            result = num_list.slice(slicer[0], slicer[1] + 1);
            break;
        case 4:
            result = num_list.slice(slicer[0], slicer[1] + 1).filter((v, i) => i % slicer[2] === 0);
    }
    return(result)
}

 

 

 

 

์–ด์—‰? ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด๋ฅผ ๋ณด๋‹ˆ ๋ฐ”๋กœ return ํ•ด์ค˜๋„ ๋˜๋Š”๊ตฌ๋‚˜.. ํ•˜๊ธด ํ•จ์ˆ˜๊ฐ€ ์ข…๋ฃŒ๋˜๋Š”๊ฒŒ ๋งž์ง€..

  switch(n) {
        case 1:
            return num_list.slice(0, b + 1);
        case 2:
            return num_list.slice(a);
        case 3:
            return num_list.slice(a, b + 1);
        case 4:
            return num_list.slice(a, b + 1).filter((_, idx) => !(idx % c));
    }

๊ทธ๋ฆฌ๊ณ  case4์—์„œ ๋‚˜๋จธ์ง€๊ฐ€ 0 ์ด๋ฉด => falsy ๊ฐ’=> false ๊ฐ€ ๋‚˜์˜ด => !(๋‚˜๋จธ์ง€๊ฐ€ 0์ด ๋‚˜์˜ค๋Š” ์—ฐ์‚ฐ) 

์ด๋ ‡๊ฒŒ ์จ๋„ ๋˜๋Š”๊ตฌ๋‚˜, , ์˜คํ˜ธ..

 

 

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/switch

 

switch - JavaScript | MDN

The switch statement evaluates an expression, matching the expression's value to a case clause, and executes statements associated with that case, as well as statements in cases that follow the matching case.

developer.mozilla.org

 

728x90
๋ฐ˜์‘ํ˜•