https://www.codewars.com/kata/514b92a657cdc65150000006
DESCRIPTION:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in. Additionally, if the number is negative, return 0 (for languages that do have them).
Note: If the number is a multiple of both 3 and 5, only count it once.
Courtesy of projecteuler.net (Problem 1)
설명:
3 또는 5의 배수인 10 이하의 수를 나열하면 3, 5, 6 그리고 9가 됩니다. 이 배수의 합은 23입니다. 전달된 숫자보다 작은 3 또는 5의 배수의 합을 반환하는 답을 완성하세요.
또한, 숫자가 음수이면 0을 반환합니다.(해당하는 언어의 경우)
주의: 숫자가 3과 5의 배수인 경우 한 번만 계산합니다.
projecteuler.net 제공 (Problem 1)
풀이
function solution(number){
let answer = 0;
for(let i = 3; i < number; i++){
if(i % 3 === 0 || i % 5 === 0) answer += i;
}
return answer;
}
answer의 값을 0으로 설정하고 i를 3부터 시작해서 i가 3의 배수이거나 5의 배수이면 answer에 i값을 더한다. i가 3보다 작으면(음수 포함) 처음에 설정된 answer의 값 0이 반환된다.
'알고리즘 > Codewars' 카테고리의 다른 글
[Codewars] Binary Addition (7 kyu) / JavaScript (0) | 2022.09.29 |
---|---|
[Codewars] Descending Order (7 kyu) / JavaScript (0) | 2022.09.28 |
[Codewars] Credit Card Mask (7 kyu) / JavaScript (0) | 2022.09.26 |
[Codewars] List Filtering (7 kyu) / JavaScript (0) | 2022.09.26 |
[Codewars] Thinkful - Logic Drills: Traffic light (8 kyu) / JavaScript (0) | 2022.09.25 |
댓글