Skip to main content
Kinetic Community

Mathematical Functions

How to impliment some Mathematical javascript functions within a Service Item.

Usage

In this Service Item I have built four different Arthmetic Operators.  The Service Item will show you how to Add, Subtract, Multiply, and Divide the Answers entered into the Service Item. 

For this Service Item I needed to have questions that will hold onto the "numbers" entered by the user:

Add Value1

Add Value2

These are the questions that will be used by the Addition function (Add Value1 + Add Value2).

Sub Value1

Sub Value2

These are the questions that will be used by the Subtraction function (Sub Value1 - Sub Value2).

Multiply Value1

Multiply Value2

These are the questions that will be used by the Multiplication function (Multiply Value 1 * Multiply Value 2).

Numerator

Denominator

These are the questions that will be used by the Division function (Numerator/Denominator).

I also needed a way to call the custom Javascript functions which is why I added the buttons:

Add Button - On Click calls the addValue(); function

Subtract Button - On Click calls the subValue(); function

Multiplication Button - On Click calls the multiplyValue(); function

Division Button - On Click calls the divideValue(); function

The addValue(), subValue(), multiplyValue(), and divideValue() functions are all defined in the CustomHeaderContent.  Please note that each of these fucntions is using the KD.utils.Action.getQuestionValue and the KD.utils.Action.setQuestionValue functions in order to perform the calculations.  The syntax of these functions is listed below:

<script type="text/javascript">

function addValue() {

     var addValue1 = parseFloat(KD.utils.Action.getQuestionValue('Add Value1'));
     if (!(addValue1)) {
         var addValue1 = 0;

     }
     var addValue2 = parseFloat(KD.utils.Action.getQuestionValue('Add Value2'));
     if (!(addValue2)) {
         var addValue2 = 0;

     }

     var addValue = (addValue1 + addValue2);

     KD.utils.Action.setQuestionValue('Add Answer', addValue);

}

function subValue() {

     var subValue1 = parseFloat(KD.utils.Action.getQuestionValue('Sub Value1'));
     if (!(subValue1)) {
         var subValue1 = 0;

     }
     var subValue2 = parseFloat(KD.utils.Action.getQuestionValue('Sub Value2'));
     if (!(subValue2)) {
         var subValue2 = 0;

     }

     var subValue = (subValue1 - subValue2);

     KD.utils.Action.setQuestionValue('Subtract Answer', subValue);

}

function multiplyValue() {

     var multiplyValue1 = parseFloat(KD.utils.Action.getQuestionValue('Multiply Value1'));
     if (!(multiplyValue1)) {
         var multiplyValue1 = 0;

     }
     var multiplyValue2 = parseFloat(KD.utils.Action.getQuestionValue('Multiply Value2'));
     if (!(multiplyValue2)) {
         var multiplyValue2 = 0;

     }

     var multiplyValue = (multiplyValue1 * multiplyValue2);

     KD.utils.Action.setQuestionValue('Multiply Answer', multiplyValue);

}

function divideValue() {

     var Numerator = parseFloat(KD.utils.Action.getQuestionValue('Numerator'));
     if (!(Numerator)) {
         var Numerator = 1;

     }
     var Denominator = parseFloat(KD.utils.Action.getQuestionValue('Denominator'));
     if (!(Denominator)) {

         alert('Only Chuck Norris can Divide by 0');
               return false;
     
     }

     var divideValue = (Numerator / Denominator);

     KD.utils.Action.setQuestionValue('Division Answer', divideValue);

}

</script>

 

I have exported and attached a copy of the Service Item to this Article.  Please feel free to download and use this Service Item.

You may also want to Note what will happen if the end user tries to divide by 0 which we all know is impossible.

 

Example

Give an example usage of the solution.