Home > Software engineering >  JavaScript calculating deposit in bank for 3 years
JavaScript calculating deposit in bank for 3 years

Time:02-26

Problem: You have deposited a sum into your bank account for 3 years. The bank has announced an interest of 5% per year. Each time the interest is calculated and added to your deposit. You have to calculate the amount in your deposit for each year. Also I must print the number with two numbers after the decimal point.

let i = gets();
let input = Number(i);


let firstYear = input   (input*(5/100));
let secondYear = firstYear   (firstYear*(5/100));
let thirdYear = secondYear   (secondYear*(5/100));

let printFirst = firstYear.toFixed(2);
let printSecond = firstYear.toFixed(2);
let printThird = firstYear.toFixed(2);

print(printFirst);
print(printSecond);
print(printThird);

CodePudding user response:

You can use a for loop like this

for(let i=0;i < 3;i  ){ 
    sum  = sum * 0.05;
    console.log(`${i} year: ${sum.toFixed(2)}`)
}

CodePudding user response:

I would write this as a comment, but I don't have enough reputation yet.

I think the main issue is that you (are getting/would get) an error when running your code. You haven't included the error in your question, which means you probably haven't actually tried running it yet.

Anyway, to fix it note that print(...) is not a javascript built-in. If you've only used python before it's probably not obvious, but the equivalent in javascript is console.log(...)


edit

Looking at your question again, you also have gets() on the first line, which is also not a javascript built-in. But it's not a python built-in either as far as I know. Regardless, if you are trying to get user input in JS, you would use prompt('enter a number: ')

  • Related