CD-41Chapter 6 .Programming Fundamentals, Part I A case (Yahoo web hosting)
Friday, July 27th, 2007CD-41Chapter 6 .Programming Fundamentals, Part I A case in point is adding numbers that may be in the form of text strings. In a simple arithmetic statement that adds two numbers together, you get the expected result: 3 + 3 // result = 6 But if one of those numbers is a string, JavaScript leans toward converting the other value to a string thus turning the plus sign s action from arithmetic addition to joining strings. Therefore, in the statement 3 + 3 // result = 33 the string-ness of the second value prevails over the entire operation. The first value is automatically converted to a string, and the result joins the two strings. Try this yourself in The Evaluator Jr. If I take this progression one step further, look what happens when another number is added to the statement: 3 + 3 + 3 // result = 63 This might seem totally illogical, but there is logic behind this result. The expression is evaluated from left to right. The first plus operation works on two numbers, yielding a value of 6. But as the 6 is about to be added to the 3, JavaScript lets the string-ness of the 3 rule. The 6 is converted to a string, and two string values are joined to yield 63. Most of your concern about data types will focus on performing math operations like the ones here. However, some object methods also require one or more parameters of particular data types. While JavaScript provides numerous ways to convert data from one type to another, it is appropriate at this stage of the tutorial to introduce you to the two most common data conversions: string to number and number to string. Converting strings to numbers As you saw in the last section, if a numeric value is stored as a string as it is when entered into a form text field your scripts will have difficulty applying that value to a math operation. The JavaScript language provides two built-in functions to convert string representations of numbers to true numbers: parseInt() and parseFloat(). There is a difference between integers and floating-point numbers in JavaScript. Integers are always whole numbers, with no decimal point or numbers to the right of a decimal. Floating-point numbers, on the other hand, can have fractional values to the right of the decimal. By and large, JavaScript math operations don t differentiate between integers and floating-point numbers: A number is a number. The only time you need to be cognizant of the difference is when a method parameter requires an integer because it can t handle fractional values. For example, parameters to the scroll() method of a window require integer values of the number of pixels vertically and horizontally you want to scroll the window. That s because you can t scroll a window a fraction of a pixel on the screen. To use either of these conversion functions, insert the string value you wish to convert as a parameter to the function. For example, look at the results of two different string values when passed through the parseInt()function: parseInt( 42 ) // result = 42 parseInt( 42.33 ) // result = 42
We recommend high quality webhost to host and run your jsp application: christian web host services.