Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Translating math formula JavaScript Help

1. ## Translating math formula JavaScript Help

Greetings

I am currently in the process of learning javascript and been working on working on this small simple project that calculates economic formula instantly. I managed to get 7 out of 8 formula to work but got stuck on the last one. I was wondering if some could please show me what I did wrong or how to fix my errors. I will post the formula below.

Formula is Finding Future Value (F) given Gradient Value (G),
Interest (I), and year (N).

F / G: = 1 / N - N / (1 + I)^N - 1

My Problem is when I added value G = 10, I = 10, N =10
I get a result of 93.73 which is wrong. Where i calculate 37.31 as correct.

When I do G =1, I = 10, N = 10.
I get a result of 3.73, which is correct.

Regardless of what I do I can't seem to fix it. Any help and advice is very appreciated. Thank you very much.

Code:
```function fvg () {
power = document.form.gradient.value * (1 / (document.form.rate.value / 100)) - (document.form.year.value) / (Math.pow((1 + (document.form.rate.value / 100)), document.form.year.value) - 1);

document.form.amount.value = Math.round(power * 100) / 100;

document.form.interest.value = Math.round((document.form.amount.value - document.form.gradient. value) * 100) / 100;
}```

• Are you quite sure that the answer is not in fact 93.73? And that your formula is truly correct?

Simplified:-

Code:
```function fvg() {
var p = 0;
var g = 10;
var i = 10;
var n = 10;
p = g * (1 / (i /100));
p = p - (n / (Math.pow((1 + (i/100)), n) - 1));
p = Math.round(p*100)/100;
}```
As g occurs only in one line I do not see how it can be wrong if 3.73 is correct when g = 1.

Sampson was a strong man who let himself be led astray by a Jezebel, like Delilah. - Pupil's answer to Catholic Elementary School test.

• ## Users who have thanked Philip M for this post:

mosquitobite (01-25-2013)

• Hi Phillip
Thank you very much for your response and for your script verification. Now thats an interesting result, it is showing up the same as my scripts. It could very well be possible that this may be an accurate answer. I rechecked my formula in my econ text book. I did made a mistake in my earlier post for it should be A / G instead of F / G. My apologies for this misrepresentation.

it should be this formula, which is identical to my Econ book. So, theoretically it should be correct :/.

I can't say I am confident on my math skills but this what I got when I calculated manually. So base on my result I was expecting something similar for my script. Unless my calculation and order of operation is flawed.

Variables
G = 10 I = 10 / 100 = .10 N = 10

Formula
G = (1/.10) - (10 / ((1 + .10)^10) - 1)

My manual calculation and results
A) 1/.10 = 10 Formula - (1/I)
B) 1 + .10 = 1.10 => (1.10)^10 = 2.59 => 2.59 - 1 = 1.59 Formula (1+I)^10 - 1)
C) 10 / 1.59 = 6.28 Formula - 10 / (1+i)^N -1
D) 10 - 6.28 = 3.71 Formula (1/i) - (N / (1 + i)^N -1
E) 10 * 3.71 = 37.10 Formula G *1/i) - (N / (1 + i)^N -1

So I came to my final as 37.10 ~ 37.30 depending on the calculator I use. I was trying to replicate this with my javascript assuming my manual calculation is close of being correct. Would you happen something I did wrong? Thank you very much.

• F / G: = 1 / N - N / (1 + I)^N - 1

My Problem is when I added value G = 10, I = 10, N =10
I get a result of 93.73 which is wrong. Where i calculate 37.31 as correct.

When I do G =1, I = 10, N = 10.
I get a result of 3.73, which is correct.
*CLEARLY* if 3.73, is correct for G=1, then the answer should be 37.3 for G=10.

After all, to transform
Code:
`F / G: = 1 / N - N / (1 + I)^N - 1`
to solve for F, all you do is multiply both sides by G:
Code:
```G * ( F / G ) = G * ( 1 / N - N / (1 + I)^N - 1 )
-->>
F = G * ( 1 / N - N / (1 + I)^N - 1 )```
which means the answer for G=10 should be 10 times the answer for G=1.

Let's just translate that pictured formula, exactly, with no simplifications:
Code:
```<!DOCTYPE html>
<html>
<body>
<form id="theForm">
Rate: <input name="rate"/><br/>
Years: <input name="year"/><br>
<input type="button" value="calculate" onclick="fvg()"/>
<hr/>
</form>

<script type="text/javascript">

// UGUS = 1/i - n/( (1+i)^n -1 )

function UGUS( G, i, n )
{
// UGUS formula save that we multiply by G
return G * ( 1/i - ( n / ( Math.pow(1+i,n) - 1 ) ) );
}

function fvg ()
{
var form = document.getElementById("theForm");
var interest = form.rate.value / 100;
var periods = Number( form.year.value );
var power = UGUS( gradient, interest, periods );

form.amount.value = power.toFixed(2); // forget Math.round!

form.interest.value = ( power - gradient ).toFixed(2);

}

</script>
</body>
</html>```
And for G=1, I=10, N=10 indeed I get 3.73
And for G=10, I=10, N=10 indeed I get 37.25

• Quite frankly, I don't understand how this:
Code:
`F = G * ( 1 / N - N / (1 + I)^N - 1 )`
works at all, when *CLEARLY* the formula starts with 1/I and *NOT* 1/N.

Also, you are missing the *REQUIRED* parentheses around the sub-expression ( (1+I)^N - 1)

I think the only reason it *SEEMED* to work was because you used the *SAME NUMBER* (10) for both I and N!!!

PURE LUCK!

• ## Users who have thanked Old Pedant for this post:

mosquitobite (01-25-2013)

• Wow! Old Pendant, thank your script, this works out beautifully! I spent a couple hours on this, tweak with numerous amount of variation and still couldn't come up with a solution.

What I posted somehow worked mostly randomly moving chunks around. I think indeed it is PURE LUCK! Or maybe beginner's luck since I just started learning how to write javascripts .

A little more tweaking I have to do on my codes and see if I can really understand how this works based on your model.

Once again my appreciation to Old Pendant and Phillip M for your help and feedback. Thank you.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•