본문 바로가기

Java Script/알고리즘 풀이

[알고리즘] 0829

문제 - Codewars_Is n divisible by x and y? (8kyu)

Create a function that checks if a number n is divisible by two numbers x AND y. All inputs are positive, non-zero numbers.

Examples:
1) n = 3, x = 1, y = 3 => true because 3 is divisible by 1 and 3
2) n = 12, x = 2, y = 6 => true because 12 is divisible by 2 and 6
3) n = 100, x = 5, y = 3 => false because 100 is not divisible by 3
4) n = 12, x = 7, y = 5 => false because 12 is neither divisible by 7 nor 5

문제 풀이

function isDivisible(n, x, y) {
  return n%x === 0 && n%y === 0 ? true : false;
}
  1. &&연산자를 사용해서 n을 x와 y로 나누었을 때 나머지 수가 모두 0이면 true를 반환, 아니면 false를 반환하도록 코드 작성

'Java Script > 알고리즘 풀이' 카테고리의 다른 글

[알고리즘] 0828  (0) 2023.08.28
[알고리즘] 0827  (0) 2023.08.27
[알고리즘] 0826  (0) 2023.08.26
[알고리즘] 0825  (0) 2023.08.25
[알고리즘] 0824  (0) 2023.08.24