-2

So I need to create three input fields with one button. One input field for a date, one for an amount of money and one for an interest rate. When clicking on the button an overview of the sum must be displayed over the course of the next years until its doubled using the given interest rate.

My question is how can I loop through these years until the sum is doubled. The rest of the code I have and works.

I'm new to javascript and coding and would appreciate anyones help.

This is the code I have right now:

s = sum
d = date
r = interest rate

for (var i = 0; i < s; i++) {
   result.innerHTML += d + " " + ((r / 100 + 1) * s) + "<br>";
}

The complete code : https://jsfiddle.net/swateen/bjah4rc0/

swateen
  • 1
  • 1
  • Not sure what is Sum here? And also what do you want? You want to show the amount (with interest) from the given date to till the date? – Nimitt Shah Sep 18 '18 at 14:57
  • i should have made that more clear: sum is the amount of money from the second input field. I want to show the amount (with interest) for each year until the amount is doubled. – swateen Sep 18 '18 at 15:01
  • It would be helpful if you could [edit] your question to include actual code (HTML & JavaScript) that you've built so far, that's relevant to the question you have. See [mcve] for information on how to do that. You can use Stack Snippets (the icon is `<>` in the editor toolbar) to help construct the code. – Heretic Monkey Sep 18 '18 at 15:06
  • thanks for the tip. the complete code can be found here : https://jsfiddle.net/swateen/bjah4rc0/ – swateen Sep 18 '18 at 15:16
  • the reason for down voting is that this problem is not about javascript or loops or any other thing. its just about programming logic. i would though, post the solution but since there is already one, i think you dont need anymore answers. happy coding mate. – Wahid Masud Sep 18 '18 at 15:30
  • Thank you for clarifying Wahid! Next time I will make sure to categorize it correctly. You too! – swateen Sep 18 '18 at 15:32

1 Answers1

0
function loop() {
    var d = new Date(inputA.value);
    var s = inputB.value;
    var r = inputC.value;
    var doubleS = s * 2;

    while(s < doubleS)
        d = new Date(d.getFullYear() + 1, d.getMonth(), d.getDate());
        s = ((r / 100 + 1) * s);
        result.innerHTML += d + " " + s + "<br>";
    }
}
Without Haste
  • 507
  • 2
  • 6
  • Thank you so much! One minor problem, instead of giving the date it gives "Invalid date" – swateen Sep 18 '18 at 15:28
  • Date expects format "dd/mm/yyyy". Read through https://stackoverflow.com/questions/10430321/how-to-parse-a-dd-mm-yyyy-or-dd-mm-yyyy-or-dd-mmm-yyyy-formatted-date-stri to see different ways to parse other formats. – Without Haste Sep 18 '18 at 15:32
  • Thanks for the help! – swateen Sep 18 '18 at 18:41