Juni_Dev_log

(200205_TIL) 'Raindrops' 본문

CodingTest/Exercism

(200205_TIL) 'Raindrops'

Juni_K 2021. 2. 5. 10:52

▶ Raindrops

raindrops

📌 Problem

Introduction

Your task is to convert a number into a string that contains raindrop sounds corresponding to certain potential factors. A factor is a number that evenly divides into another number, leaving no remainder. The simplest way to test if a one number is a factor of another is to use the modulo operation.

The rules of raindrops are that if a given number:

  • has 3 as a factor, add 'Pling' to the result.
  • has 5 as a factor, add 'Plang' to the result.
  • has 7 as a factor, add 'Plong' to the result.
  • does not have any of 3, 5, or 7 as a factor, the result should be the digits of the number.

- 숫자를 문자열로 변환시켜야하는데, 빗방울 소리를 특정한 잠재적인 요인에 따라서 변환시켜본다.

- 빗물의 규칙은 이렇게 된다.

- 3이 들어가면, Pling 을 결과에 넣는다.

- 5가 들어가면, Plang 을 결과에 넣는다.

- 7이 들어가면, Plong 을 결과에 넣는다.

- 3,5,7을 요소로써 가지지 않는다면, 결과는 숫자의 자릿수여야한다.

 

Tip) 

1. 3을 나누어지면 Pling을 더한다.

2. 5로 나누어지면 Plang을 더한다.

3. 7로 나누어지면 Plong을 더한다.

 

만약, 세개 모두 나누어지지않는다면 숫자 그대로 반환한다.

 

예) convet(10) -> 5로 나누어지기 때문에 "Plang"을 반환

 

💯 Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
export const convert = (num) => {
    let string = '';
    if(num % 3 === 0){
        string += 'Pling';
    }
    else if(num % 5 === 0){
        string += 'Plang';
    }
    else if(num % 7 === 0){
        string += 'Plong';
    }
    else{
        string += String(num);
    }
    return string;
};
 
cs

- % 연산자와 += 할당 연산자를 이용해서 빈 문자열인 string 변수에 해당 조건에 맞는 문자열을 추가하는 방식으로 코드를 작성했다.

 

 

'CodingTest > Exercism' 카테고리의 다른 글

(200208_TIL) "Difference Of Squares"  (0) 2021.02.08
(200207_TIL) 'Word Count'  (0) 2021.02.07
(200204_TIL) 'Hamming'  (0) 2021.02.04
(200203_TIL) 'ETL'  (0) 2021.02.03
(200202_TIL) 'Triangle'  (0) 2021.02.02
Comments