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
728x90
๋ฐ์ํ