[Daily Coding] _computeWhenDouble

fejigu·2022년 8월 6일
1

알고리즘 & 자료구조

목록 보기
12/24


문제

연이율을 입력받아 원금이 2배 이상이 될 때까지 걸리는 시간(년)을 리턴해야 합니다.

입력

인자 1 : interestRate
number 타입의 연이율 (%)

출력

number 타입을 리턴해야 합니다.

입출력 예시

let output = computeWhenDouble(7);
console.log(output); // --> 11
output = computeWhenDouble(10);
console.log(output); // --> 8

💻 코드 작성

[내가 작성한 코드 => 수도 코드까지..]

// 원금 * 연이율 = n년의 원금
// 원금 선언하고, 1을 임의로 할당
// (년)을 위한 변수 선언 
// 원금이 2배 이상일 경우 > 년 수를 리턴 
// 원금이 2배 안될경우 > 계속 반복 
function computeWhenDouble(interestRate) {
  let myMoney = 1;
  let year = 0;

  return year;
}

[레퍼런스 코드]

function computeWhenDouble(interestRate) {
  let rate = 1 + interestRate / 100;
  let principal = 1;
  let year = 0;
  while (principal < 2) {
    principal = principal * rate;
    year++;
  }
  return year;
}

📍 회고

문제에 대한 이해 자체가 어려워서 수도 코드 작성까지도 어려움을 느꼈다.
위 문제는 다시 풀어봐야할 것 같다...

profile
console.log(frontendjigu( ☕️, 📱); // true

0개의 댓글