본문 바로가기
codes/daily coding

02_computeWhenDouble

by Mia_ 2022. 11. 20.
function computeWhenDouble(interestRate) {
  //Q. 연이율을 입력받아 원금의 2배 이상이 될 때까지
  //걸리는 시간(년)을 리턴
  // 이율 : 원금에 대한 이자의 비율 // 연이율 : 1년을 단위로 정한 비율

  //7 %의 연이율이라면 1.07 상태로 만들어줌
  let rate = 1 + interestRate / 100;
  
  //원금을 1로 설정
  let principal = 1;
  let year = 0;
  while(principal < 2){
    principal = principal * rate;
    //console.log(principal);
    year++;
  }
  return year;

}
//다시 풀어본 코드
function computeWhenDouble(interestRate) {
  //Q. 연이율을 입력받아 원금의 2배 이상이 될 때까지
  //걸리는 시간(년)을 리턴
  // 이율 : 원금에 대한 이자의 비율 // 연이율 : 1년을 단위로 정한 비율

  //interestRate = 7 가정시 0.07 + 1 => 1.07 상태로 만듬
  let cal = 1 + interestRate / 100;

  let principal = 1;
  let yearCount = 0;
  while(principal < 2){
    principal = principal * cal;
    yearCount++;
  }
  return yearCount;
}

'codes > daily coding' 카테고리의 다른 글

22_fibonacci  (0) 2022.12.20
19_decryptCaesarCipher  (0) 2022.12.11
18_numberSearch  (0) 2022.12.11
03_powerOfTwo  (0) 2022.12.07
01_transformFirstAndLast  (0) 2022.11.20