Cscityueduhkhelena 4 program structure iii 19 data

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

View Full Document Right Arrow Icon
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("100") parseInt("3.14 meters") parseInt("0xFF") parseInt("FF",16) parseInt("11",16) parseInt("eleven") parseInt("$13.50") gives 100 gives 3 parseInt is for integers, so contents since ".14.." 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("10.4") 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 <head>..</head>, <body>..</body> 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 (>, =, !=, >=, &&, ||, !, ..) (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