Building Simulations with Javascript

As an organization required to publish analytical reports, we inherit the responsibility of using data to create simulations or models. In this article I will discuss examples of applying real data and using Javascript to create a simulation.

Why Javascript? To answer this question, you must first eliminate any inhibitions you may have regarding Javascript, especially used in this capacity. Logically, the customer base will dictate the environment for which we must develop in. In our IT organizational environment, we could only be sure of a few things:

There isn't a guarantee of any runtime environment; Java or Dot Net.

Nearly all of the users do NOT have administrative privileges.

If the users have a working computer (Linux or Windows), they have a web browser.

There is an emphasis on rapid application development.

And finally, some users do not have network access.

The Application Model

Because the simulation is viewed in a web browser, there are inherited skills required; HTML and CSS. But in our case, we are going to discuss the role of Javascript. The simulation consists of four primary components:

A - Global Variables

B - Calculation Variables

C - Calculations

D - Results

(A & B) --> C = D

We will now examine each component below.

Global Variables

Because the simulation is written in Javascript, it is important to remember that the variables used will be held in memory during the user's interaction. Refreshing the browser will equate to flushing the variables from memory. To publish the simulation results in various manners (data stream, summary, charts, etc.), the values of the global variables cannot change during the current simulation.

The example below initializes three global variables; gblA, gblB, gblC. When the form button is clicked the values of the variables will be populated from the user's input.

Though very elementary, this is the scope of our global variables. The calculation variables are somewhat more complicated.

Calculation Variables

The entire simulation project would become static, in a fixed condition, if we didn't employ user input. Furthermore, the results have unrealistic meaning if we didn't apply meaningful data. This is the purpose of the calculation variables.

To apply these variables, an understanding of statistics would be of critical importance. But for the sake of demonstration, view the example below.