Java Script 101 Lesson 4 Formatting Input Data













- Slides: 13

Java. Script 101 Lesson 4: Formatting Input Data for Arithmetic

Lesson Topics n n n Data types String data versus numeric data How input data (from the prompt method) is stored as a string Why you need to format input data for arithmetic How to use built in Java. Script functions to format input data for arithmetic (parse. Int, parse. Float, and eval)

Data Types n n n Data type is a category of information used by a programming language Identifies the type (kind) of information a program can represent Java. Script has three basic data types: n n n String Numeric Boolean

String data vs. numeric data n n n String data is used to input and output information Numeric data can carry out arithmetic All information in a computer is stored using just 0 s and 1 s n n Inside the computer, strings and numbers use different patterns to store information Need to change a string pattern into a number pattern before computer can execute arithmetic

String data versus Numeric data n n When the prompt method is used to collect data from a Web page visitor, information input is a string Information in the form of a string must be formatted as a number before it can be used for arithmetic

How to convert strings to numbers n Use these Java. Script methods n n n The parse. Float() method The parse. Int() method The eval() method

The parse. Float() Method n n Syntax: var number=parse. Float(string 1); parse. Float takes the value stored in string 1 and translates it to a decimal format and stores the number in the variable number

The parse. Int() Method n n Syntax: var whole. Number=parse. Int(string 1): parse. Float takes the value stored in string 1 and translates it to a decimal format and stores the number in the variable number

The eval() Method n n n The eval() method evaluates a numeric expression in the form of a string and returns its value Syntax: var result=eval(string 1); Where string 1 is a numeric expression in string format

In the lab n n Use Java. Script methods to convert user input from string format to numeric format and then carry out arithmetic operations Open Notepad and create a new HTML document named lesson 0401. html Enter the code on p. 4 -6 exactly as you see it Save the file and open it using either Internet Explorer or Netscape

Student Modifications n Modify the code on p. 4 -6 to prompt users to enter the age of their dog, using parse. Float(), convert the dog’s age to human years using the following formula dog. To. Human. Years = ((dog. Age-1) * 7) + 9 n Do other conversions, from cat years (cats live about 20 years) to human years. Look on the internet for other possibilities

Lesson Summary n n n Data types String data versus numeric data Input data from the prompt method stores is a string Data in string format cannot be used for arithmetic Java. Script methods to convert strings into numbers After conversion, arithmetic can be carried out

Lesson Summary (cont. ) n n n The parse. Float method, which converts a string to a decimal number The parse. Int method, which converts a string to an integer The eval method, which converts an expression in the form of a string into a numeric value