4ProgramStructureIII

# Cscityueduhkhelena 4 program structure iii 19 data

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: tring(16) means to convert the value of variable x to hexadecimal. x.toString(2) means to convert it to binary. toFixed: convert a number to a string a specific number of digits to the right of the decimal (rounded). Example: var x=5/3; x.toFixed( ) gives 2 x.toFixed(1) gives 1.7 x.toFixed(2) gives 1.67 (CS1301) Introduction to Computer Programming City Univ of HK / Dept of CS / Helena Wong http://www.cs.cityu.edu.hk/~helena 4. Program Structure III - 19 Data Types Conversions between numbers and strings: parseInt: convert a string to an integer (with an optional radix). Example: parseInt(&quot;100&quot;) parseInt(&quot;3.14 meters&quot;) parseInt(&quot;0xFF&quot;) parseInt(&quot;FF&quot;,16) parseInt(&quot;11&quot;,16) parseInt(&quot;eleven&quot;) parseInt(&quot;\$13.50&quot;) gives 100 gives 3 parseInt is for integers, so contents since &quot;.14..&quot; are ignored. (convert hexadecimal number) gives 255 (convert hexadecimal number) gives 255 gives 17 (cannot convert) gives NaN (cannot convert) gives NaN parseFloat: convert a string to a floating-point value. Example: parseFloat(&quot;10.4&quot;) gives 10.4 (CS1301) Introduction to Computer Programming City Univ of HK / Dept of CS / Helena Wong http://www.cs.cityu.edu.hk/~helena 4. Program Structure III - 20 Summary Semicolons (;): Manual vs Automatic Insertion Location of JavaScript Blocks &lt;head&gt;..&lt;/head&gt;, &lt;body&gt;..&lt;/body&gt; Global variables and functions Names of Variables and Functions Global and local variables with the same name Enclose statements in Braces { and } Assignment Statements ( =, +=, -=, *=, etc..) Data Types and operations Primitive types: Numbers (whole integers, floating-point values, NaN, Infinity) Strings Booleans null and undefined Escape Sequences Conversions between Numbers and Strings Boolean Values and Operators (&gt;, =, !=, &gt;=, &amp;&amp;, ||, !, ..) (CS1301) Introduction to Computer Programming City Univ of HK / Dept of CS / Helena Wong http://www.cs.cityu.edu.hk/~helena 4. Program Structure III - 21...
View Full Document

## This note was uploaded on 03/10/2012 for the course CS 1301 taught by Professor Dr.wong during the Winter '08 term at City University of Hong Kong.

Ask a homework question - tutors are online