algorithm
[코플릿]Algorithm Basic-02_computeWhenDouble
개발하는라푼젤
2022. 7. 31. 11:20
728x90
연이율을 입력받아 원금이 2배 이상이 될 때까지 걸리는 시간(년)을 리턴해야 합니다.
입출력예시
더보기
//입출력예시
let output = computeWhenDouble(7);
console.log(output); // --> 11
output = computeWhenDouble(10);
console.log(output); // --> 8
function computeWhenDouble(interestRate) {
let rate = 1 + interestRate / 100;
let principal = 1; //원금
let year = 0;
while (principal < 2) { //원금이 2배될때까지
principal = principal * rate;
year++;
}
return year;
}
let output = computeWhenDouble(7);
function computeWhenDouble(연이율) {
let 비율 = 1+연이율 /100
let 원금 = 1;
let 해 = 0;
while(원금 < 2) { //2배이상이 되어야하는데 왜 '2보다 작은'때까지인가?
원금 = 원금 * 비율
해++;
}
}
2배이상이 되어야하는데 왜 '2보다 작은'때까지인가??????