REPORTER 22.1

Example 3: Setting a Character Variable According to the Result of a Calculation

Example 3: Setting a character variable according to the result of a calculation

Problem

Input variable = PERCENT
If (abs(PERCENT) < 5.0) then new variable RESULT = 'OK'
otherwise 'not OK'

Solution

JavaScript
var result;

// Get variable value from template
var percent = reporter.currentTemplate.GetVariableValue("PERCENT");

// Check that the variable exist
if (percent == null) throw Error("no PERCENT variable\n");

// Extract number from variable
var p = parseFloat(percent);

// Check that the variable is a valid number
if (isNaN(p)) throw Error("PERCENT " + percent + " is not a valid number\n");

// Check for less than 5
if (Math.abs(p) < 5.0)
result = "OK";
else
result = "not OK";

// Create new variable RESULT
var rvar = new Variable(reporter.currentTemplate, "RESULT",
"is it OK?", result);

Discussion

This uses exactly the same methods as examples 1 and 2. The only difference is that the value used in the Variable constructor is a character string, not a number.

The source code for this example is available here .