Programming the Web using XHTML and JavaScript

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

Computer Programming w/ Eng. Applications
1 CSC 551: Web Programming Spring 2004 client-side programming with JavaScript  scripts vs. programs  JavaScript vs. JScript vs. VBScript  common tasks.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 63 – Manipulating Data Using Methods – Day 2.
Chapter 3: Expressions and Interactivity. Outline cin object Mathematical expressions Type Conversion and Some coding styles.
The Web Warrior Guide to Web Design Technologies
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
CIS101 Introduction to Computing Week 10 Spring 2004.
CIS101 Introduction to Computing Week 10. Agenda Your questions Final exam and final project CIS101 Student Survey Class presentations: Your Mad Libs.
Chapter 6: User-Defined Functions I
Basic Input/Output and Variables Ethan Cerami New York
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
SYST Web Technologies SYST Web Technologies Lesson 6 – Intro to JavaScript.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Keeping it Neat: Functions and JavaScript Source Files Chapter 7.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
JavaScript, Fourth Edition
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CSCE 102 – Chapter 11 (Performing Calculations) CSCE General Applications Programming Benito Mendoza Benito Mendoza 1 By Benito Mendoza.
1 Server versus Client-Side Programming Server-SideClient-Side.
JavaScript 101 Lesson 3: Variables and Arithmetic.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
JavaScript and Ajax (JavaScript Functions) Week 5 Web site:
Expressions and Data Types Professor Robin Burke.
JavaScript Tutorial. What is JavaScript JavaScript is the programming language of HTML and the Web Programming makes computers do what you want them to.
Arithmetic Expressions
Variables, Operators, and Expressions
Precedence Operators Error Types
Chapter 9: Value-Returning Functions
Chapter 6 JavaScript: Introduction to Scripting
CHAPTER 4 CLIENT SIDE SCRIPTING PART 1 OF 3
Computing and Statistical Data Analysis Lecture 2
CHAPTER 4 CLIENT SIDE SCRIPTING PART 3 OF 3
Java Primer 1: Types, Classes and Operators
2.5 Another Java Application: Adding Integers
Functions Comp 205 Fall ‘17.
JavaScript: Functions
Introduction to Scripting
JavaScript Syntax and Semantics
JavaScript Functions.
Programming the Web using XHTML and JavaScript
PHP Introduction.
JavaScript.
Lecture Set 4 Data Types and Variables
User-Defined Functions
Number and String Operations
T. Jumana Abu Shmais – AOU - Riyadh
Core Objects, Variables, Input, and Output
An Introduction to JavaScript
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Summary of what we learned yesterday
Introducing JavaScript
JavaScript: Introduction to Scripting
Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

Programming the Web using XHTML and JavaScript Chapter 11 Calculations

Calculations Goal: for a rectangle create a calculator that determines the: Area Perimeter Need: Input fields Width Height Output fields Ability to perform calculations Ability to invoke calculation function

Calculations Mathematical operators + Addition - Subtraction * Multiplication / Division

Calculations Math with constants var result1, result2, result3

Calculations Math with variables var result1, result2, result3

Calculations Changes are not retroactive var x, y, z x = 12 y = 5 What’s the value of z at this point? x = 6 Now what’s the value of z?

Calculations Spaces (white space) ignored x=3*4 x = 3*4 x = 3* 4 Equivalent

Calculations Problems: parseFloat() parseInt() Text box values are stored as strings Can’t do mathematical operations on strings Ch11-Ex-00 parseFloat() Converts a string into a real value parseInt() Converts a string into an integer value

Calculations Ch11-Ex-01 var xStr = “75.2”, yStr=“10.1” var xNum=parseFloat(xStr) var yNum=parseFloat(yStr) var z = xNum + yNum Ch11-Ex-01

Calculations Running totals var x x = 0 … x = x + 1

Calculations Remember order of operations in an assignment statement x = x + 1 Do this first Then store result here

Calculations var x … x = 35 x = x + 15 Value of x before the next statement is executed? Value of x just after the previous statement is executed?

Calculations Increment operator ++ x = 4 x = 4 x = x + 1 x++ (result 5) Ch11-Ex-02

Calculations Decrement operator -- x = 4 x = 4 x = x - 1 x-- (result 3)

Calculations When used in an assignment statement… (Almost) Equivalent forms: x++ and ++x x-- and --x When used in an assignment statement…

Calculations If operator follows variable then x = 5 y = x++ Assignment first Increment second x = 5 y = x++ Result: y = 5, x = 6

Calculations If operator precedes variable then x = 5 y = ++x Increment first Assignment second x = 5 y = ++x Result: y = 6, x = 6

Calculations In a nutshell: Always use the x++ form Unless you know the ++x form is what you really want If you use the ++x form comment why!

Calculations Combination assignment operators x += 5 (x = x + 5)

Calculations Problem: decrement operator has specific use in XHTML syntax Can’t use in JavaScript and stay compliant with XHTML Solution: put JavaScript code in an external JavaScript file (filename.js) Include only JavaScript, not <script> elements Call by <script src=“filename.js” type=“text/javasacript”> </script>

Calculations Precedence of operations (order of operations) Increment and decrement Multiplication and division Addition and subtraction Combination Left to right for multiples of the same level of operation Use parentheses to override Hint: use parentheses on any complex operation

Calculations 9 + 2 * 4 9 + ( 2 * 4 ) 9 + 8 17

Calculations 9 + 2 * 4 - 7 * 8 9 + ( 2 * 4 ) – ( 7 * 8 ) 9 + 8 – ( 7 * 8 ) 9 + 8 – 56 17 – 56 -39

Calculations (9 + 2) * 4 - 7 * 8 (9 + 2) * 4 – ( 7 * 8 ) (9 + 2) * 4 – ( 7 * 8 ) (9 + 2) * 4 – 56 (11 * 4) – 56 44 – 56 -12

Calculations Is +, -, *, and / all JavaScript can do for math? Those are the four primitive functions Whole library of advanced functions available

Calculations The Math object Math methods: Ch11-Ex-03 Math.methodname(…) Math methods: Square root – Math.sqrt(x) Power – Math.pow(x,n) Round – Math.round(x) [if .5 round up, else down] Floor – Math.floor(x) [down to next integer] Book is wrong on this one (e.g. floor(-5.2) is -6 PI – Math.PI [as in Math.PI*radius] Random – Math.random() [value between 0 and 1] …and many more… Ch11-Ex-03

Returning Values from Functions Scope of a variable Global – can be used anywhere in the source document Local – can be used only within a specific function

Returning Values from Functions

Returning Values from Functions Fun with values: What happens to the second variable? Ch11-Ex-04

Returning Values from Functions <head> <title>Ch11-Ex-05</title> <script type="text/javascript"> var firstAnswer = 93.7 var secondAnswer = "New York" alert("Answer #1 is "+firstAnswer) alert("Answer #2 is "+secondAnswer) function testVar() { var secondAnswer="Florida" alert("Answer #2 in testVar is "+secondAnswer) } </script> </head> <body> testVar() </body> Known only within the testVar function

Returning Values from Functions Declaring the variable (using var) makes it local Local variables can’t be changed outside their function That means other programming team members won’t write code that changes your variables Ch11-Ex-05

Returning Values from Functions Rules of Thumb Use var keyword to make a variable local Declare all global variables in <head> section Declaring a variable without using var: Makes it global No matter where the declaration occurs Variables are known only to the web page where they are used i.e., you can’t use variables from one page, load a new page, and expect those variable values to still be there – they won’t be

Returning Values from Functions Similar to using parameters except… The function itself has a value x = 2 x = sqrt(4) The sqrt function is equivalent to the number 2 when called with a parameter of 4

Returning Values from Functions So far we’ve used functions to do things and store the results in variables Now, we’re going to make the functions themselves have value

Returning Values from Functions Declaration: function calcAvg(n1, n2, n3) { var average average = (n1 + n2 + n3) / 3 alert(“The average is “+average) } Call: calcAvg(1, 2, 3)

Returning Values from Functions Declaration: function calcAvg(n1, n2, n3) { var average average = (n1 + n2 + n3) / 3 return average } Call: x = calcAvg(1, 2, 3)

Returning Values from Functions Declaration: function calcAvg(n1, n2, n3) { return (n1 + n2 + n3) / 3 } Call: x = calcAvg(1, 2, 3) Ch11-Ex-06

Finally, the calculator Goal: create a calculator that determines the area and perimeter of a rectangle Need: Input fields for width and length Output fields for area and perimeter Ability to perform calculations Ability to invoke calculation function Example: Ch11-Ex-07 Note: example made with “help” from SharePoint

Assignment See Assignments Web Page Appearance Technical correctness of code Proper results