Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Translating math formula JavaScript Help

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 01-24-2013, 01:10 PM PM User | #1 mosquitobite New to the CF scene   Join Date: Jan 2013 Posts: 4 Thanks: 2 Thanked 1 Time in 1 Post 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; }``` Last edited by mosquitobite; 01-24-2013 at 01:18 PM..
 01-24-2013, 03:05 PM PM User | #2 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,473 Thanks: 200 Thanked 2,469 Times in 2,447 Posts 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; alert (p); // 93.73 }``` 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. __________________ All the code given in this post has been tested and is intended to address the question asked. Unless stated otherwise it is not just a demonstration.
 Users who have thanked Philip M for this post: mosquitobite (01-25-2013)
 01-24-2013, 04:01 PM PM User | #3 mosquitobite New to the CF scene   Join Date: Jan 2013 Posts: 4 Thanks: 2 Thanked 1 Time in 1 Post 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.
01-24-2013, 09:31 PM   PM User | #4
Old Pedant
Supreme Master coder!

Join Date: Feb 2009
Posts: 24,943
Thanks: 75
Thanked 4,306 Times in 4,273 Posts
Quote:
 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
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

 01-24-2013, 09:34 PM PM User | #5 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,943 Thanks: 75 Thanked 4,306 Times in 4,273 Posts 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! __________________ An optimist sees the glass as half full. A pessimist sees the glass as half empty. A realist drinks it no matter how much there is.
 Users who have thanked Old Pedant for this post: mosquitobite (01-25-2013)
 01-25-2013, 01:32 PM PM User | #6 mosquitobite New to the CF scene   Join Date: Jan 2013 Posts: 4 Thanks: 2 Thanked 1 Time in 1 Post 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.

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 10:01 PM.