# Tips and tricks #8: How to reuse existing calculator

This article describes process of reusing another calculator's logic (algorithm) in your calculator.

#### Tips and tricks series

- Tips and tricks #1: How to set output value
- Tips and tricks #2: How to make calculator translatable into different languages
- Tips and tricks #3: How to make calculator with custom errors
- Tips and tricks #4: How to make calculator with table output
- Tips and tricks #5: How to show/hide input and output controls in a calculator
- Tips and tricks #6: How to make calculator with virtual keyboard
- Tips and tricks #7: How to make calculator with SVG image map
- Tips and tricks #9: Big numbers

*This article relies on knowledge you should get from previous articles, so you may want to check them first.*

### Algorithm reuse

In this article we will create the calculator which will use calculator already existing on site. Technically, our calculator delegates part of calculations to another calculator or reuse another calculator's algorithm.

As example, we will create calculator which will be just enhanced version of Probability of given number success events in several Bernoulli trials calculator. That calculator outputs probability only for single entered *k*. Our calculator will display probabilities in a table, for each *k* from zero to *n* (number of Bernoullu trials).

Create new calculator. Add two inputs:

Name | Variable | Type | Default value | Note |
---|---|---|---|---|

Number of Bernoulli trials | n | Number | 10 | |

Success probability | p | Number | 0.5 | Set "Allow decimal digits", set "Number range", set range from 0 to 1 |

Create output table. Hover * table_chart Table* button and click on

*Add output table*menu item. This will bring up the output table editor dialog.

Fill **Name** and **Variable** with *Bernoulli trials* and *table* values respectively. Leave **Description** empty. The Table columns part of the dialog consists of two parts: column editor on the top and list of columns at the bottom, below button *Add*. For new table, it is empty.

Now fill the information for the first table column like this:

Field name | Value | Meaning |
---|---|---|

Variable |
k |
Name of Javascript variable, which is used to hold input value |

Column Name |
Number of successes |
Column name as displayed in column header |

Column appearance |
Show on diagram |
Marks this column as x-axis of the chart |

Type |
Number |
The type of the output |

Leave all other fields with their default values and press *Add* to add second column.

Fill the information for second table column

Field name | Value | Meaning |
---|---|---|

Variable |
p |
Name of Javascript variable, which is used to hold input value |

Column Name |
Probability |
Column name as displayed in column header |

Column appearance |
Show as line |
Marks this column as source of line chart |

Type |
Number |
The type of the output |

Number of decimal digits |
3 |
number of decimal digits displayed in this column by default |

Leave all other fields with their default values and press *Add* to add second column.

Note how list of columns now displays added columns. You can edit and delete it if you need to, using links in the list.

Press *OK* to close output table editor.

Now we need to add code into *Calculate* function.

Let's first create code to populate table.

```
for(var i = 0; i <= n; ++i) {
var rec = table.AddNewRecord();
rec.k = i;
rec.p = 0.0;
}
```

Note how resulting table now filled with zeroes

At this moment, we need to reuse existing calculator. To do this, click *Algorithm* button on toolbar. In search window, enter "Probability of k" and click *Search*. Click on search result (it should be "Probability of k success events in n Bernoulli trials").

This will generate sample call in your code

```
//Probability of k success events in n Bernoulli trials
var result = Planetcalc.Calculate4145( {
//----- Inputs -----
"p":0.2 //Event probability (Number)
,"n":10 //Number of independent trials (Number)
,"k":2 //Number of success events (Number)
} );
//----- Outputs -----
//result.P - Probability (Number)
```

Insert it inside cycle. Finally your code should look like this

```
for(var i = 0; i <= n; ++i) {
var result = Planetcalc.Calculate4145( {"p":p, "n":n,"k":i} );
var rec = table.AddNewRecord();
rec.k = i;
rec.p = result.P;
}
```

You are ready to publish.

Click on *Preview* button. By default you should see table and chart with different probabilities of k successes. If everything is working as expected, *Publish* the calculator. After publishing, calculator will receive persistent address and will be available for other users of the site. You can also embed it in articles. In our case, you can see it right below:

#### Similar calculators

- • Bernoulli trials table
- • Probability of given number success events in several Bernoulli trials
- • Binomial distribution, probability density function, cumulative distribution function, mean and variance
- • Negative Binomial Distribution. Probability density function, cumulative distribution function, mean and variance
- • Geometric Distribution. Probability density function, cumulative distribution function, mean and variance
- • Math section ( 236 calculators )

## Comments