ABSTRACT

Strings and numbers You have created a game that allows the user to input a numeric value using an input box. Then you run this code:

on (release){ num = num + userInput;

}

You have a num of 14, and a userInput of 10. You expect the num 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 num, 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 ‘10’ to be used as a number. One solution is to make sure Flash realizes to use userInput as a number by using the ‘Number’ method:

on (release){ num = num + 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 numeric quantity, because no other arithmetic operation applies to strings. Therefore:

on (release){ num = num - userInput;

‘–’ operator then the value to the left and right of the operation is a number.