Lesson 4: Formatting Input Data for Arithmetic

Slides:



Advertisements
Similar presentations
Chapter 7 JavaScript: Introduction to Scripting. Outline Simple Programs Objects and Variables Obtaining User Input with prompt Dialogs – –Dynamic Welcome.
Advertisements

1 COMM 1213 H1 COMP 4923 X1 JavaScript 1 (Readings: Ch. 10, 11 Knuckles)
Operators and Expressions Operators are symbols that produce a result based on some rules. Examples: +, -, =, > and
The Web Warrior Guide to Web Design Technologies
 2005 Pearson Education, Inc. All rights reserved Introduction.
CIS101 Introduction to Computing Week 12. Agenda Your questions Solutions to practice text Final HTML/JavaScript Project Copy and paste assignment JavaScript:
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Introduction to Scripting.
JavaScript 101 Lesson 01: Writing Your First JavaScript.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
CIS101 Introduction to Computing Week 10 Spring 2004.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
CIS101 Introduction to Computing Week 09. Agenda Hand in Resume project Your questions Introduction to JavaScript This week online Next class.
CIS101 Introduction to Computing Week 10. Agenda Your questions Final exam and final project CIS101 Student Survey Class presentations: Your Mad Libs.
JavaScript 101 Lesson 5: Introduction to Events. Lesson Topics Event driven programming Events and event handlers The onClick event handler for hyperlinks.
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
JavaScript, Third Edition
CIS101 Introduction to Computing Week 12 Spring 2004.
CIS101 Introduction to Computing Week 09 Spring 2004.
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
 2004 Prentice Hall, Inc. All rights reserved. Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
WEB DESIGN AND PROGRAMMING Introduction to Javascript.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
CSC318 – DYNAMIC WEB APPLICATION DEVELOPMENT
Programming.
The Web Wizard’s Guide To JavaScript Chapter 6 Working with Dates and Times.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Lesson Interactive Scripts JavaScript statements must go inside a script container, usually placed in the head section of the HTML document, but.
CS346 Javascript -3 Module 3 JavaScript Variables.
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
Variables and Data Types Data (information we're going to store) – Numbers – Text – Dates What types of data can JavaScript process? How do we store it?
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
Web111a_chapt08.ppt HTML: Section 8 JavaScript CGI Programs (Server Side programs) Common Gateway Interface Run on server Communicate with user across.
JavaScript Assignment Statements Expressions Global Functions CST 200 JavaScript.
JavaScript 101 Lesson 3: Variables and Arithmetic.
JavaScript 1 COE 201- Computer Proficiency. Introduction JavaScript scripting language ▫Originally created by Netscape ▫Facilitates disciplined approach.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
JavaScript 101 Introduction to Programming. Topics What is programming? The common elements found in most programming languages Introduction to JavaScript.
Data Types and Conversions, Input from the Keyboard If you can't write it down in English, you can't code it. -- Peter Halpern If you lie to the computer,
Expressions and Data Types Professor Robin Burke.
JavaScript 101 Lesson 6: Introduction to Functions.
INTERNET APPLICATIONS CPIT405 JavaScript Instructor: Rasha AlOmari
Chapter 6 JavaScript: Introduction to Scripting
“Under the hood”: Angry Birds Maze
1-1 Logic and Syntax A computer program is a solution to a problem.
Data Types and Conversions, Input from the Keyboard
Project 9 Creating Pop-up Windows, Adding Scrolling Messages, and Validating Forms.
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Variables, Expressions, and IO
14 A Brief Look at JavaScript and jQuery.
Chapter 7 - JavaScript: Introduction to Scripting
JavaScript: Introduction to Scripting
WEB PROGRAMMING JavaScript.
Programming Right from the Start with Visual Basic .NET 1/e
Lesson 2: Input and Variables
Selection Statements.
The Internet 11/15/11 Handling Data in JavaScript
Variables Kevin Harville.
We are starting JavaScript. Here are a set of examples
Tutorial 10: Programming with javascript
JavaScript: Introduction to Scripting
Chapter 7 - JavaScript: Introduction to Scripting
Chapter 7 - JavaScript: Introduction to Scripting
The Web Wizard’s Guide To JavaScript
JavaScript 101 Lesson 8: Loops.
Murach's JavaScript and jQuery (3rd Ed.)
Chapter 7 - JavaScript: Introduction to Scripting
Presentation transcript:

Lesson 4: Formatting Input Data for Arithmetic JavaScript 101 Lesson 4: Formatting Input Data for Arithmetic

Lesson Topics 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 JavaScript functions to format input data for arithmetic (parseInt, parseFloat, and eval)

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

String data vs. numeric data String data is used to input and output information Numeric data can carry out arithmetic All information in a computer is stored using just 0s and 1s 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 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 Use these JavaScript methods The parseFloat() method The parseInt() method The eval() method

The parseFloat() Method Syntax: var number=parseFloat(string1); parseFloat takes the value stored in string1 and translates it to a decimal format and stores the number in the variable number

The parseInt() Method Syntax: var wholeNumber=parseInt(string1): parseFloat takes the value stored in string1 and translates it to a decimal format and stores the number in the variable number

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

In the lab Use JavaScript 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 lesson0401.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 Modify the code on p. 4-6 to prompt users to enter the age of their dog, using parseFloat(), convert the dog’s age to human years using the following formula dogToHumanYears = ((dogAge-1) * 7) + 9 Do other conversions, from cat years (cats live about 20 years) to human years. Look on the internet for other possibilities

Lesson Summary 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 JavaScript methods to convert strings into numbers After conversion, arithmetic can be carried out

Lesson Summary (cont.) The parseFloat method, which converts a string to a decimal number The parseInt 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