*JS help*

- Started
- Last post
- 13 Responses

- i_monk
My client has this PDF that needs to calculate and display bunch of numbers using java script. It looks pretty straight forward to me, and I've found plenty of examples of bits and pieces online, but I'm not a JS expert and it isn't working as expected.

Here's what I'm working with and what it's supposed to do:

I found pv function code for JS here:

http://www.mohaniyer.com/old/pvc…Help me QBN, you're my only hope.

- i_monk0
Here's what I've cobbled together so far, but this is really outside my area... I could play with this for weeks and not figure out what's missing:

function pv(rate, per, nper, pay, fv)

{

var rate=this.getField("Rate");

var per=this.getField("Years");

var nper=this.getField("Shortfall");

var pay=0;

var fv=1;

}

var amount = this.getField("Amount");

var choice = this.getField("Choice1");

if (choice.value=="1")

{

amount.valueAsString = nper/rate;

}

else

{

amount.valueAsString = pv(rate,per,nper,pay,fv);

}- I can't tell if it's doing what I want, or figure out how to make it show the results.i_monk

- mikotondria30
I love this place, but I don't tend to post many technical questions here as people are too busy being awesome at work, and come here to unwind, and be rude to other people. Myself included.

If you don't get any joy, try somewhere like

http://stackoverflow.com/questio…

- Stugoo0
Best thing to do is to break down the thing into simple tasks.

- uan0
your script:

1. definition of function pv.

2. definition of 2 global vars (amount, choice)

3. conditional statement: if choice==1 --> amount.valueAsString = nper/rat ((your js doesn't know what nper/rat are at that stage)).

else amount.valueAsString = call pv function with rate, per, nper, pay and fv ((you would need to put values in there, something like fv(1,2,1,1,1) ))

4. your function pv is called (as explained in 3 without values). pv gets rate, per, nper values from some field (overwrites what you passed in the call) and defines pay and fv...does nothing else...I don't know if this helps a bit, but you have a big mess in your script...

...do what Stugoo said. Start with simple, small steps and build up from there.

- Stugoo0
I have no idea if the values are right but here is something dirty that I threw together.

I haven't commented it as I need to go to the pub now but it should be enough to get you started

- i_monk0
Thanks Stugoo, but I'm just going to pass this on to the client, it's beyond my ability and I really don't have time to learn javascript for the sake of one document.

- i_monk0
Hmm... I think I figured it out (mostly). PDFs have a built-in on-mouse-up action for radio buttons. So I stuck the code for "yes" on the yes button and it worked. I still have to figure out something with the "no" coding, but I think I can get it.

YES:

var N_Shortfall = this.getField("N_Shortfall").val...

var N_Rate = this.getField("N_Rate").value;

this.getField("N_Amount").value = N_Shortfall*N_Rate;NO:

var N_Shortfall = this.getField("N_Shortfall").val...

var N_Rate = this.getField("N_Rate").value;

var N_Years = this.getField("N_Years").value;

function pv(rate, years, short, fv, pd)

{

rate = N_Rate;

years = N_Years;

short = N_Shortfall;

fv = 0;

pd = 1;

}

this.getField("N_Amount").value = uhhh.....;I just have to figure out how to get that last line to execute the pv function...

- uan0
you need a return(some_value) in the pv function.

(look at your posted example: http://www.mohaniyer.com/old/pvc… )

then you can execute it with

whereItGoes = pv(someParamsIfNeeded);

- i_monk0
Yeah I was looking more closely at it and realized the function pv definition didn't end where I thought it did (this format is alien to me), so I'm reworking it:

var N_Shortfall = this.getField("N_Shortfall").val...

var N_Rate = this.getField("N_Rate").value;

var N_Years = this.getField("N_Years").value;

function pv(rate, years, short, fv, pd)

{

rate = N_Rate;

years = N_Years;

short = N_Shortfall;

fv = 0;

pd = 1;

if (rate ==0)

{

pv_value = -(fv + (pd * short));

}

else

{

x = Math.pow(1 + rate, -short);

y = Math.pow(1 + rate, short);

pv_value = - (x * (fv * rate - pd + y * pd )) / rate;

}

return (pv_value);

}

this.getField("N_Amount").value = pv();This is at least producing a number where I expect it to be, and one that's different from the "Yes" number.

- uan0
another detail about js syntax:

function pv(rate, years, short, fv, pd) {...};

defines a function pv that expects 5 parameters. it will take the received params and store them for the time of the function with those variable-names.

if you call pv(1,2,3,4,5); you will have inside the function pv this vars:

rate = 1; years = 2; short = 3; fv = 4; pd = 5.if you don't need to change those params before calling the function, you can just define the function like this: function pv() {...}; and call it as you did with = pv();

- i_monk0
So I solved it... by completely ignoring the PV function coding I found and figuring out the actual math involved (the "else" below).

var N_Shortfall = this.getField("N_Shortfall").val...

var N_Rate = this.getField("N_Rate").value / 100;

var N_Years = this.getField("N_Years").value;

var N_ComInt = Math.pow((1+N_Rate),N_Years);if (N_Rate == 0) {

N_No = 0 - (N_Shortfall * N_Years);

}

else {

N_No = (-N_Shortfall * (1 + N_Rate) * ((N_ComInt - 1)/N_Rate))/N_ComInt;

}

this.getField("N_Amount").value = Math.abs(N_No);

- fadein110
geeks