This data converter provides the opportunity to specify the conversion using JavaScript. This data converter may for instance be useful when working with advanced text manipulation such as URL rewriting, or performing complex calculations.

The input to the converter is available in an implicitly defined INPUT variable. The result of performing the conversion must be assigned to an OUTPUT variable. Helper functions can be defined and called if needed. Note that access to the browser state from the JavaScript is not possible in this converter.

The Convert Using JavaScript data converter is configured using the following properties:

*Script:*- The JavaScript to execute. This may be specified literally or created in several different ways using a Value Selector.
*Description:*- Type in a description to be shown in the list of data converters. If there is no type in a description, one will be generated.

**Example 1:**

To calculate the average of a comma-separated list of numbers, we configure the Convert Using JavaScript data converter with the following script that performs the calculation:

```
var sum = 0;
var numbers = INPUT.split(",");
for (var i = 0; i < numbers.length; i++) {
sum += parseInt(numbers[i]);
}
OUTPUT = sum / numbers.length;
```

The JavaScript reads the list of numbers, e.g. "23,22,25,31,24" from the INPUT variable, splits it at each
comma using the built-in JavaScript `split()`

function, iterates through the numbers to calculate the sum
(note that the `parseInt()`

function is used to convert from string to integer; otherwise we would be concatenating
the strings rather than calculating the sum),
and finally calculates the average and assigns the result to the OUTPUT variable.

In the case where the input to the data converter is the string "23,22,25,31,24", the output of the converter will thus be 25.

**Example 2:**

To calculate the maximum of a comma-separated list of amounts, e.g. "$10.50,$13,$21.75,$7", we configure the Convert Using JavaScript data converter with the following script that performs the calculation:

```
function getNumber(amountWithDollarSign) {
return parseFloat(amountWithDollarSign.substring(1));
}
var amountsWithDollarSigns = INPUT.split(",");
var max = getNumber(amountsWithDollarSigns[0]);
for (var i = 1; i < amountsWithDollarSigns.length; i++) {
max = Math.max(max, getNumber(amountsWithDollarSigns[i]));
}
OUTPUT = max;
```

In the above JavaScript, we have defined a helper function named `getNumber()`

that removes the preceding
dollar sign from the amount and converts the rest into a floating-point number. This function is called repeatedly
in script. The built-in JavaScript function `Math.max()`

used to find the maximum of two numbers;
in each iteration, the highest number found so far is compared with the next number.
Finally, the highest number found is stored in the OUTPUT variable and becomes the output of the data converter.

The following methods are useful when converting String objects. Notice that *strings* are written inside "" whereas *regexp* are written within //. A global *g* attribute at the end of a *regexp* indicates that the method should apply to all matches.

Method | Meaning |
---|---|

string.charAt(n) |
Returns the character with index n. |

string.charCodeAt(n) |
Returns the Unicode value of the character with index n. |

string.concat(value1, value2, ...) |
One or more values are concatenated to string. |

String.fromCharCode(c1, c2, ...) |
Creates a new string from integers specifying the Unicode encodings of the characters. |

string.indexOf(substring)string.indexOf(substring, start) |
Returns the index of substring within string.start specifies the index at which the search should start (0 being the first character in the string and string.length-1 being the last. Default is 0). |

string.lastIndexOf(substring)string.lastIndexOf(substring, start) |
Returns the position of the last occurrence of substring within string.start specifies the index at which the search should start (0 being the first character in the string and string.length-1 (which is default) being the last). |

string.length |
Character length of string. |

string.match(regexp) |
Searches string for matches with with a regular expression regexp. Returns only the first match unless regexp has the global attribute specified (g), by which an array containing all results from the match is returned. |

string.replace(regexp, replacement)string.replace(substring, replacement) |
Searches string for matches with a substring or a regular expression and replaces the first occurrence with replacement. If regexp has the global attribute specified (g), all occurrences are replaced with replacement. |

string.search(regexp) |
Returns the index of the first character of the first match with a regular expression. |

string.slice(start, end) |
Returns a string containing all characters from start through end-1. |

string.split(delimiter, limit) |
delimiter is a string or regular expression which specifies the places at which to split string. Returns an array of strings. The array is no longer than limit |

string.substr(start, length) |
Returns a copy of the substring starting from index start and being of the length length. |

string.substring(from, to) |
Returns the substring starting at position from and ending at to-1. |

string.toLowerCase() |
Returns a copy of string converted to lower case. |

string.toUpperCase() |
Returns a copy of string with all letters converted to upper case. |

The following properties and methods are useful when doing mathematical computations. All angles are measured in radians.

Property/Method | Meaning |
---|---|

Math.E | Returns Euler's number. |

Math.LN10 | Returns the natural logarithm of 10. |

Math.LN2 | Returns the natural logarithm of 2. |

Math.LOG10E | Returns the base-10 logarithm of E. |

Math.LOG2E | Returns the base-2 logarithm of E. |

Math.PI | Returns pi. |

Math.SQRT1_2 | Returns the square root of 1/2. |

Math.SQRT2 | Returns the square root of 2. |

Math.abs(x) |
Returns the absolute value. |

Math.acos(x) |
Computes arc cosine. |

Math.asin(x) |
Returns arc sine. |

Math.atan(x) |
Computes arc tangent. |

Math.atan2(y, x) |
Computes the angle to a point. The input represent the usual (x,y)-coordinates, but the order has been reversed. |

Math.ceil(x) |
Rounds up a number. |

Math.cos(x) |
The cosine function. |

Math.exp(x) |
Takes e to the power of x. |

Math.floor(x) |
Rounds down a number. |

Math.log(x) |
Computes the natural logarithm. |

Math.max(x1, x2, ...) |
Returns the largest of the numbers. |

Math.min(x1, x2, ...) |
Returns the smallest of the numbers. |

Math.pow(x,y) |
Computes x to the power of y |

Math.random() | Returns a random number between 0 and 1 |

Math.round(x) |
Rounds to nearest integer. |

Math.sin(x) |
The sine function. |

Math.sqrt(x) |
Computes the square root. |

Math.tan(x) |
The tangent function. |

It is possible to convert from a Number to a String using String(*number*) and vice versa using Number(*string*). Here are some of the useful methods of the Number object.

Method | Meaning |
---|---|

number.toExponential(digits) |
Specifies the number of digits that will occur after the decimal point. Returns a string representation of number in exponential notation. |

number.toFixed(digits) |
Specifies the number of digits that will occur after the decimal point. Returns a string representation of number that does not use exponential notation. |

number.toPrecision(precision) |
Specifies the number of significant digits. Returns a string representation of number with the specified number of significant digits. |

number.toString(base) |
Returns a string representation of the number using the specified base. |