Algorithm/DailyCoding
[22.09.21] Daily Coding 1, 2
Dong _ hwa
2022. 9. 21. 16:46
1.
λ°°μ΄μ μ λ ₯λ°μ μ°¨λ‘λλ‘ λ°°μ΄μ 첫 μμμ λ§μ§λ§ μμλ₯Ό ν€μ κ°μΌλ‘ νλ κ°μ²΄λ₯Ό 리ν΄ν΄μΌ ν©λλ€.
function transformFirstAndLast(arr) {
let obj = {};
if (arr.length === 0) return {};
obj[arr[0]] = arr[arr.length-1]
return obj
}
2.
μ°μ΄μ¨μ μ λ ₯λ°μ μκΈμ΄ 2λ°° μ΄μμ΄ λ λκΉμ§ 걸리λ μκ°(λ )μ 리ν΄ν΄μΌ ν©λλ€.
function computeWhenDouble(interestRate) {
// μκΈμ μ μΈνκ³ , μμλ‘ 1λ‘ μ§μ
// nλ
μ μ΄μ = μ°μ΄μ¨ * μκΈ
// μκΈμ΄ 100μ΄κ³ μ°μ΄μ¨μ΄ 5%λΌλ©΄ 1λ
μ μ΄μλ 100 + 100 * ( 5 / 100 )
// μκΈμ΄ 2κ° λκΈ° μ κΉμ§ λ°λ³΅νλ€
// μκΈμ΄ 2κ° λμΌλ©΄ κ·Έλμ λ
(i) μλ₯Ό 리ν΄
let myMoney = 1;
let i = 0;
while ( myMoney < 2 ){
myMoney = myMoney + myMoney * (interestRate / 100)
i++
}return i
}
<λ νΌλ°μ€>
function computeWhenDouble(interestRate) {
let rate = 1 + interestRate / 100;
let principal = 1;
let year = 0;
while (principal < 2) {
principal = principal * rate;
year++;
}
return year;
}
μ¬μ€ λκ°κΈ΄ νλ°, λ³μλͺ μ΄λ μ΄μλ₯Ό λ°λ‘ μ€μ ν΄λμ κ±° λ³΄κ³ κ°μ Έμ λ΄€λ€