chapter  10
Debugging
Pages 17

You have an oldValue of 14 and a userInput of 10. You expect the newValue to be set to 24, instead it is set to 1410. Why? Simple really, userInput is a string, all Input Boxes store data as a string. The operation ‘+’ can be applied to strings, so Flash converts oldValue, 14, into the string ‘14’ and ‘adds’ it to the string ‘10’. The string operation ‘+’ is concatenation; the string to the left of the operator and the string to the right are joined together. This is not the method that was intended; in your code you wanted the string ‘14’ to be used as a number. The solution is to make sure Flash realizes to use userInput as a number by using the ‘Number’ method:

on (release) { newValue = oldValue + Number(userInput);

The result will now be a number, 24, as you intended. If the combination operation were any other arithmetic operation then Flash would have realized that you intended to use userInput as a numerical quantity, because no other arithmetic operation applies to strings. Therefore:

on (release) { newValue = oldValue – userInput;

}

would give the result 4, if oldValue was 14 and userInput was 10, with no requirement to force userInput to be a number. Flash ‘knows’ that if you are using the ‘–’ operator then the value to the left and right of the operation is a number.