프로그래머스(코딩 테스트)/Lv.0
홀짝에 따라 다른 값 반환하기
걍가영
2024. 10. 15. 15:06
// javascript
const solution = (n) => {
return n % 2 === 1
? Array.from({ length: Math.floor(n / 2) + 1 }, (_, index) => index * 2 + 1).reduce((sum, num) => sum + num, 0)
: Array.from({ length: n / 2 }, (_, index) => (index + 1) * 2).reduce((sum, num) => sum + num * num, 0);
}
// typescript
const solution = (n: number): number => {
return n % 2 === 1
? Array.from({ length: Math.floor(n / 2) + 1 }, (_, index) => index * 2 + 1).reduce((sum, num) => sum + num, 0)
: Array.from({ length: n / 2 }, (_, index) => (index + 1) * 2).reduce((sum, num) => sum + num * num, 0);
}