       $.each $target, (index, radio) ->
         $(radio).prop("checked", $(radio).attr('data-label-value') == result).trigger('change')
 
+    # If the type of the calculation result doesn't match the element type, or is null/undefined, clear the target
     else
       if elementType == 'checkbox'
         $target.prop("checked", false).trigger('change')


    $conditionElement.val()

  updateCalculation: (rule) ->
    parameters = {}
    $target = $('[data-question-id="' + rule.question_id + '"]').find('.form-control')
    $targetRepeater = $target.closest(".form-container-repeater")
    targetType = $('[data-question-id="' + rule.question_id + '"]').data('element-type')

    $.each rule.conditions, (index, condition) ->

      if $targetRepeater.length > 0 && $('[data-question-id="' + condition.question_id + '"]').closest(".form-container-repeater").length > 0
        $question = $targetRepeater.find('[data-question-id="' + condition.question_id + '"]')
      else
        $question = $('[data-question-id="' + condition.question_id + '"]')

      elementType = $question.data('element-type')
      columnName = $question.data('api-column-name')
      $repeater = $question.closest(".form-container-repeater")

      if $repeater.length > 0 && $targetRepeater.length == 0
        entries = []
        $.each $question, (index, entry) ->
          value = self.getNativeValue($(entry).find('.form-control'), elementType)
          entries.push value
        parameters[columnName] = entries

      else
        $conditionElement = $question.find('.form-control')
        value = self.getNativeValue($conditionElement, elementType)
        parameters[columnName] = value

    self.interpreter = new Interpreter(rule.script, (interpreter, globalObject) ->
      outputWrapper = (result) -> self.setCalculationResult($target, result, targetType)
      interpreter.setProperty(globalObject, 'setResult', interpreter.createNativeFunction(outputWrapper))
      $.each parameters, (key, value) -> interpreter.setProperty(globalObject, '$' + key, interpreter.nativeToPseudo(value))
    )

    self.interpreter.run()

  setCalculationResult: ($target, pseudoResult, elementType) ->
    result = self.interpreter.pseudoToNative(pseudoResult)

    if elementType == 'checkbox' && typeof result == 'boolean'
      $target.prop("checked", result).trigger('change')

      else
        $target.val('').trigger('change')

  # converts the string value taken from a given input to a native JS value that we can pass to the interpreter
  getNativeValue: ($input, elementType) ->
    stringValue = self.getValue($input)

