y u no do it?

Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

Starts at just $1 per CPM or$0.10 per CPC.

# Attached image file has formula to add in javascript

Discussion in 'JavaScript' started by rajkumarcali, Jun 25, 2020.

1. #1
Hello Guys,

I need some help. Attached is the formula image which I want to use in Javascript but I dont know how to do the superscript (n) in Javascript

Here is my code

<script>
function myFunction() {
var pmt;
var PV = 201508;
var i1 = 3.625;
var i2=3.625/100;
var i = i2/12;
var n= 360;

pmt = PV * i(1+ i)/(1 + i) - 1;
}
</script>
Code (markup):
Can someone tell me how I can use the attached formula image in javascript.

Thanks

Raj

#### Attached Files:

• ###### formula.jpg
File size:
85.3 KB
Views:
8
Jun 25, 2020 IP
2. ### sarahkiTamerStaff

Messages:
26,126
3,821
108
Trophy Points:
665
#2
Javascript generates html your question is really "how do I do this in HTML"

This "should" work but it seems browser support is limited
<MATH>
PMT =
<box>
PVi(1 + i)<sup>n</sup>
<over>
(1 + i)<sup>n</sup> - 1
</box>
</MATH>
Code (markup):
all the advice online seems to be to use MathJax

Jun 25, 2020 IP
3. ### rajkumarcaliPeon

Messages:
9
0
0
Trophy Points:
1
#3
Hello,

How can I use the formula in my code. I dont need the html. I have the variables defined in my javascript. I just want use the formula to calculate PMT.

If you use the image it has the formula how do I convert that formula in my javascript code.

Thanks

Raj

Jun 25, 2020 IP
4. ### sarahkiTamerStaff

Messages:
26,126
3,821
108
Trophy Points:
665
#4
oh, you mean you actually want the answer?

and you want to use javascript to do the math?

Jun 25, 2020 IP
5. ### rajkumarcaliPeon

Messages:
9
0
0
Trophy Points:
1
#5
Yes I want to use javascript to do the math

Jun 25, 2020 IP
6. ### hdewantaraWell-Known Member

Messages:
485
44
22
Trophy Points:
130
#6
Jun 25, 2020 IP
7. ### rajkumarcaliPeon

Messages:
9
0
0
Trophy Points:
1
#7
Thank you much for your advise to use Math.pow. I was able to succeed to solve one equation. I have 2 equations to solve. The value I get in the first equation which is PMT is then passed to second equation and Now I am having a tough time to solve the second equation.

Here is my javascript code

<script>
function myFunction() {

var fees =1508;
var p =200000;
var n=360;

var pv = p + fees;
var i1=3.625;
var i2= i1/100;
var i = i2/12;

var c1= pv *i;
var c2 = Math.pow(1+i, n);
var c3 = c2 -1;

// first equation is solved here
var PMT = c1 * c2 / c3;

//Second equation begins here

var apr;
PMT = (p *apr) (Math.pow (1 + apr, 360))/(Math.pow (1+apr , 360)) -1;

}
</script>

So I am I able to get the value of PMT in first equation now I transfer that value in second equation to calculate apr and my javascript is not working. Attached are both equations.

Can someone please tell me what I am doing wrong. In the image the value is i and in my code it means apr

Any help is highly appreciated it.

Thanks

Raj

#### Attached Files:

• ###### IMG_2665.jpg
File size:
77.8 KB
Views:
5
Jun 26, 2020 IP
8. ### hdewantaraWell-Known Member

Messages:
485
44
22
Trophy Points:
130
#8
PMT = (p *apr) (Math.pow (1 + apr, 360))/(Math.pow (1+apr , 360)) -1;
Code (markup):
javascript isn't Matlab software so it doesn't understand how to solve apr using that equation. So I guess you need to solve this equation (by hand) first into a new function i.e. apr = fn(PMT) and then use this new function to calculate apr. Right?

Jun 26, 2020 IP
9. ### rajkumarcaliPeon

Messages:
9
0
0
Trophy Points:
1
#9
I am new to Javascript. I am just learning. Can you please give me a hint on how to solve the second equation. I converted the equation to Javascript but it isnt working your right to solve it on a piece of paper first.

Jun 27, 2020 IP
10. ### hdewantaraWell-Known Member

Messages:
485
44
22
Trophy Points:
130
#10
Yeah, as I am to financial / banking calculation haha
apr is abbreviation for annual percentage rate I presume.

how do you usually calculate this apr; with Excel Goal Seek?
And does it has valid range e.g. from 0 to 1?

Jun 27, 2020 IP
11. ### rajkumarcaliPeon

Messages:
9
0
0
Trophy Points:
1
#11
here is the formula

https://www.calculatorsoup.com/calculators/financial/apr-calculator-basic.php

I have to convert it into javascript

The problem is in this example the variable is (i) I am trying to find out how to break the second equation.

Any hints or ideas you can share.

Jun 27, 2020 IP
12. ### hdewantaraWell-Known Member

Messages:
485
44
22
Trophy Points:
130
#12
To quote the last paragraph:
Sorry Raj, I myself am foreign to the nature of the APR equation and so I can't guarantee that my demo (see below) has robust solution. Instead of Newton-Raphson method suggested by your article, this demo - oh, I have difficulty explaining this - does guessing by using javascript natural random generator and tries to reduce its search range until it meets specified precision.

Feel free to modify it and good luck

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>apr estimator</title>
<style>
form{
display: inline-block;
}
label{
display: block;
margin-bottom: 0.1em;
}
label:after{
content: '';
display: block;
clear: right;
}
input[type="text"]{
float: right;
width: 12em;
margin-left: 0.5em;
}
border: 0;
}
</style>
<body>
<h1>apr estimator</h1>

<form autocomplete="off">
<fieldset>
<label>fees: <input type="text" name="fees" value="1508"></label>
<label>p: <input type="text" name="p" value="200000"></label>
<label>n: <input type="text" name="n" value="360"></label>
<label>i1: <input type="text" name="i1" value="3.625"></label>
<label>prec: <input type="text" name="prec" value="0.00001"></label>
<hr>
<hr>
</fieldset>

<button name="submitButt" type="submit">calculate</button>
</form>

<script>
'use strict';

var elForm = document.getElementsByTagName('form')[0],
elButt = elForm['submitButt'],
p,
n,
pmt,
apr = 0,
kApr = 0.01,
prec, //precision
err = 999, //assume initial error of newPmt - pmt
t = null;

function guessApr(){
if(err < prec)//stop calculation and show result
elButt.textContent = 'calculate';
else{
var newApr = apr + kApr * (2 * Math.random() - 1),
newPmt = p * newApr * Math.pow(1 + newApr, n) / ((Math.pow (1 + newApr, n)) - 1),

if(newApr > 0 && newErr < err){//if is better then save, show and continue with new apr guess
apr = newApr;
kApr /= 2; //reduce search range
console.log(kApr);
elForm['apr'].value = apr;//show it
}

//else continue with previous apr guess
t = setTimeout(guessApr, 100);
}
};

elForm.onsubmit = function(ev){
ev.preventDefault();

if(elButt.textContent === 'calculate'){
elButt.textContent = 'stop';

//get numbers
p = Number(elForm['p'].value);
n = Number(elForm['n'].value);
prec = Number(elForm['prec'].value);

//pmt calculation
this['pv'].value = p + Number(this['fees'].value);

var i2 = Number(this['i1'].value) / 100;

this['i'].value = i2 / 12;

this['c1'].value = Number(this['pv'].value) * Number(this['i'].value);
this['c2'].value = Math.pow(1 + Number(this['i'].value), Number(this['n'].value));
this['c3'].value = Number(this['c2'].value) - 1;

pmt = Number(this['c1'].value) * Number(this['c2'].value) / Number(this['c3'].value);
this['pmt'].value = pmt;

//apr calculation
t = setTimeout(guessApr, 100);
}
else{
clearTimeout(t);
elButt.textContent = 'calculate';
}
};
</script>
</body>
</html>

HTML:

Jun 29, 2020 IP
13. ### rajkumarcaliPeon

Messages:
9
0
0
Trophy Points:
1
#13

The equation is working. I have a small request for you. the APR value your getting I have to multiply it by 1200 and then I have to use .toFixed function to round to 3 digits.

Can you tell me where in the code I can do it.

Also when you click on calculate button it takes sometime for the calculation to happen. I have to do the calculation on pageLoad and if lots of people click on te website this script will take a long time to run. Is there anything that can be done to expedite the calculation.

Again your great soul thanks for helping me appreciate it.

Jun 29, 2020 IP
14. ### hdewantaraWell-Known Member

Messages:
485
44
22
Trophy Points:
130
#14
Hi Raj,

I would use that at line #81:
elForm['apr'].value = apr;//show it
Code (JavaScript):
That's because my demo is just using random search algorithm; it even doesn't have a name you know . You should try to search the web for one optimal search algorithm, specifically for APR function and then just replace mine.

Cool

Jun 29, 2020 IP
15. ### rajkumarcaliPeon

Messages:
9
0
0
Trophy Points:
1
#15
Hello,

I was able to do the multiplcation with 1200 and also apply toFixed to the code.

I have few questions for you in the below code

where prec=0.00001

If I shorten this to 0.1 it still calculates the APR but its little bit off.

Any way to run this code faster because its taking a long time to get the value ?

Jun 30, 2020 IP
16. ### hdewantaraWell-Known Member

Messages:
485
44
22
Trophy Points:
130
#16
Well... you could speed up just a bit by setting 2 lines which contains:
t = setTimeout(guessApr, 100);
Code (JavaScript):
to have 0ms timeout instead of 100ms. That's all I know.

The guessApr() is a search function to calculate a new PMT of a random APR value. This PMT is then compared to the PMT of the 1st calculation to get err (PMT error / difference). When this err value is larger than prec, guessApr() is called again (with smaller search range) through setTimeout(). This may happen several times, create many loops and so is the reason of the long wait. If err value is less than prec, loop to call guessApr() ends thus calculation stops.

True that setting large prec value e.g. 0.1 may reduce number of loops and so accelerates this search. But the result will not be as precise as setting prec = 0.00001.

Hope this makes some sense.

Jun 30, 2020 IP

Messages:
9