본문 바로가기
코테연습

18.Multiples of 3 or 5

by hxunz 2022. 3. 29.

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.

 

function solution(number){
  let num = 0;
  for (i=1; i < number; i++) {
    if (i % 3 === 0 || i % 5 === 0) {
      num += i;
    }
  }
  return num;
}

이제 코드워즈 6kyu 문제로 넘어왔다 ~~~

먼저 num에다가 0을 선언하고 반복문을 사용해서 주어진 숫자까지 반복되도록했다.

숫자가 3의배수이거나 5의 배수인 경우에는 num에다가 더해주기로 했다. 

그런다음에 num을 리턴

 

 

'코테연습' 카테고리의 다른 글

20.Stop gninnipS My sdroW!  (0) 2022.03.30
19.Find the odd int  (0) 2022.03.29
17.Find the next perfect square!  (0) 2022.03.27
16.Growth of a Population  (0) 2022.03.26
15.Two to One  (0) 2022.03.24

댓글