Reputation: 19
I started studying javascript last weekend, I started from basics and now I'm in arithmetic. I tried to compute the interest but my code doesn't do anything. I'm not sure where's the error is, please help me with this. Thanks!
I tried to search for some codes that are relevant to my problem but it was so difficult to understand.
var amount = document.getElementById('amount').value;
var interest_rate = document.getElementById('interest_rate').value;
var days = document.getElementById('days').value;
var principal = amount;
var interest = (interest_rate * .01);
var payment = ((amount * interest) * days + principal) .toFixed(2);
I wanted to display the payment amount, that's all. Thanks! :)
Upvotes: 0
Views: 36
Reputation: 2404
Input values are strings. You need to turn them into a number in order to perform math operations, using parseFloat
for numbers with decimals or parseInt
for whole numbers.
(I guessed at which are whole numbers and which are decimals.)
var amount = parseFloat(document.getElementById('amount').value);
var interest_rate = parseFloat(document.getElementById('interest_rate').value);
var days = parseInt(document.getElementById('days').value);
var principal = amount;
var interest = (interest_rate * .01);
var payment = ((amount * interest) * days + principal) .toFixed(2);
Just so you are aware, toFixed()
will return a string. It’s fine for displaying to the user, but don’t try to do any math with it.
Upvotes: 2