Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Online calculator in Javascript

Dear All,

My request is connected to this discussion : https://www.sawtoothsoftware.com/forum/1474/need-script-constant-sum-total-free-form-numeric-entry-list?show=1474#q1474

I have created an online calculator (free format) to show respondents savings they can do on a product. Product got some variables which can be modified according to needs.  In the calculator there are 8 rows ,  first five can be modified and last 3 give results based on these 5 rows.
Variables and formulas are as below :
r1
r2
r3
r4
r5
r6=r5*r1/r2+r1
r7=r6-r1
r8=r7*r4*r3

Im able to make working in javascript formula for r6 but I got stuck on r7 as it uses  same variable
for r6 and r7 => I mean 'r1'.
So the HTML <Head> Tag is as below :

<script type="text/javascript">

function calcR6()
{
var r6 = 0;
r6 = (Number(document.getElementById("Throughput_r5").value)
* Number(document.getElementById("Throughput_r1").value)
/ Number(document.getElementById("Throughput_r2").value))
+ Number(document.getElementById("Throughput_r1").value)
;
document.getElementById("Throughput_r6").value = r6;
}

function calcR7()
{
var r7= 0;
r7 = Number(document.getElementById("Throughput_r6").value)   
- Number(document.getElementById("Throughput_r1").value);  
document.getElementById("Throughput_r7").value = r7;
}


function calcR8()
{
var r8 = 0;
r8 = Number(document.getElementById("Throughput_r7").value)
* Number(document.getElementById("Throughput_r4").value)
* Number(document.getElementById("Throughput_r3").value)
;
document.getElementById("Throughput_r8").value = r8;
}


</script>


and the grid setup for r1
<td style="text-align: left;">  <input name="Throughput_r1" id="Throughput_r1" type="text" size="3" onChange="calcR6()" > </td>

the issue is that r1 I would need to pipe in to calcR6()  and calcR7() functions... something like :
<td style="text-align: left;">  <input name="Throughput_r1" id="Throughput_r1" type="text" size="3" onChange="calcR6()" onChange="calcR7()" > </td>

but obviously it doesn't work....
I would be very grateful for any ideas .....

robson
asked Oct 15, 2014 by robson Bronze (595 points)
edited Oct 16, 2014 by Walter Williams

1 Answer

0 votes
Could you try having it call a function that then calls both calcR6 and calcR7?  For example, the grid setup for r1 could call:
onChange="calcR6R7"

Then, add a function that calls both R6 and R7:
function calcR6R7 ()
{
   calcR6();
   calcR7();
}
answered Oct 18, 2014 by Gary Baker Silver Sawtooth Software, Inc. (6,780 points)
Thank yopu Gary,
you are right.  I already found this  solution and it is as you suggest .  Instead of 3 functions - just one. Called the function 'THR'. So the Java code looks as below :
<script type="text/javascript">

function calcTHR()
{
var r6 = 0;
var r7 = 0;
var r8 = 0;


r6 = (Number(document.getElementById("Throughput_r5").value)
* Number(document.getElementById("Throughput_r1").value)
/ Number(document.getElementById("Throughput_r2").value))
+ Number(document.getElementById("Throughput_r1").value)
;
document.getElementById("Throughput_r6").value = Math.round(r6);
 

r7 = Number(document.getElementById("Throughput_r6").value)   
- Number(document.getElementById("Throughput_r1").value);   
document.getElementById("Throughput_r7").value = Math.round(r7);
 

r8 = Number(document.getElementById("Throughput_r7").value)
* Number(document.getElementById("Throughput_r4").value)
* Number(document.getElementById("Throughput_r3").value)
;
document.getElementById("Throughput_r8").value = Math.round(r8);
}
</script>


and the grid setup :
<td style="text-align: left;">  <input name="Throughput_r1" id="Throughput_r1" type="text" size="3" onChange="calcTHR()" > </td>

As a comment I can add that helpful also is to add below the grid setup :
<script type="text/javascript">
calcTHR();
</script>

It enables automatic calculation . Somebody can add also a button 'Calculation' below the grid setup if necessary.

All the best and thank you very much for your answer !
robson
...