Download presentation
Presentation is loading. Please wait.
Published byPaulina Beryl Caldwell Modified over 9 years ago
1
XP Tutorial 2 New Perspectives on JavaScript, Comprehensive1 Working with Operators and Expressions Creating a New Year’s Day Countdown Clock
2
XP Tutorial 2New Perspectives on JavaScript, Comprehensive2 Objectives Work with event handlers Insert a value into a Web form field Create and work with date objects Extract information from date objects
3
XP Tutorial 2New Perspectives on JavaScript, Comprehensive3 Objectives Work with arithmetic, unary, conditional, and logical operators Understand the properties and methods of the Math object Understand how JavaScript works with numeric values Run time-delayed and timed-interval commands
4
XP Tutorial 2New Perspectives on JavaScript, Comprehensive4 Working with onEvent Processing Hector’s vision for Web site –Clock that updates itself every second Function to be created will –Calculate current date and time –Calculate time remaining until New Year’s Day –Run once every second
5
XP Tutorial 2New Perspectives on JavaScript, Comprehensive5 Form Field Names
6
XP Tutorial 2New Perspectives on JavaScript, Comprehensive6 Inserting the NYClock Function
7
XP Tutorial 2New Perspectives on JavaScript, Comprehensive7 Understanding Event Handlers Event –Action that occurs within a Web browser Event handler –Statement that tells browsers what code to run in response to specified event Syntax to insert event handler as an attribute...
8
XP Tutorial 2New Perspectives on JavaScript, Comprehensive8 JavaScript Event Handlers
9
XP Tutorial 2New Perspectives on JavaScript, Comprehensive9 Running JavaScript Commands as Links Syntax content The following code runs the calcTotal() function Calculate total cost
10
XP Tutorial 2New Perspectives on JavaScript, Comprehensive10 Working with Dates Date object –Contains information about a specified date and time –Syntax variable = new Date("month day, year hours:minutes:seconds");
11
XP Tutorial 2New Perspectives on JavaScript, Comprehensive11 Retrieve the Date, Month, and Hour Values Date methods –Used to retrieve information from or modify a date object Syntax to apply the getDate() method DateObject.getDate()
12
XP Tutorial 2New Perspectives on JavaScript, Comprehensive12 Retrieve the Date, Month, and Hour Values Storing value of current year in a variable ThisDate = new Date("February 24, 2007 14:35:05"); thisYear = thisDate.getFullYear();
13
XP Tutorial 2New Perspectives on JavaScript, Comprehensive13 Retrieving the Hour, Minute, and Second Values Methods –DateObject.getSeconds() –DateObject.getMinutes( –DateObject.getHours()
14
XP Tutorial 2New Perspectives on JavaScript, Comprehensive14 Extracting Date and Time Values from Date Objects
15
XP Tutorial 2New Perspectives on JavaScript, Comprehensive15 Setting Date and Time Values for Date Objects
16
XP Tutorial 2New Perspectives on JavaScript, Comprehensive16 Creating a Date and Time Function showDate() function function showDate(dateObj) { thisDate = dateObj.getDate(); thisMonth = dateObj.getMonth()+1; thisYear = dateObj.getFullYear(); return thisMonth + "/" + thisDate + "/" + thisYear; }
17
XP Tutorial 2New Perspectives on JavaScript, Comprehensive17 Creating a Date and Time Function showTime() function function showTime(dateObj) { thisSecond=dateObj.getSeconds(); thisMinute=dateObj.getMinutes(); thisHour=dateObj.getHours(); return thisHour + ":" + thisMinute + ":" + thisSecond; }
18
XP Tutorial 2New Perspectives on JavaScript, Comprehensive18 Calling the showDate() and showTime() Functions
19
XP Tutorial 2New Perspectives on JavaScript, Comprehensive19 Working with Operators Operator symbol – Used to act upon an item or a variable within a JavaScript expression Arithmetic operators –Perform simple mathematical calculations
20
XP Tutorial 2New Perspectives on JavaScript, Comprehensive20 Arithmetic Operators
21
XP Tutorial 2New Perspectives on JavaScript, Comprehensive21 Arithmetic Operators Binary operators –Work with two items in an expression Unary operators –Work on only one item Increment operator –Used to increase the value of an expression by 1
22
XP Tutorial 2New Perspectives on JavaScript, Comprehensive22 Arithmetic Operators Decrement operator –Decreases an item’s value by 1 Negation operator –Changes the sign of (or negates) an item’s value
23
XP Tutorial 2New Perspectives on JavaScript, Comprehensive23 Unary Operators
24
XP Tutorial 2New Perspectives on JavaScript, Comprehensive24 Assignment Operators Used when assigning values to items within a JavaScript statement The following expressions create the same result x = x + y; x += y;
25
XP Tutorial 2New Perspectives on JavaScript, Comprehensive25 Assignment Operators
26
XP Tutorial 2New Perspectives on JavaScript, Comprehensive26 Calculating the Days Left in the Year Function needs to function calcDays(currentDate) { create a date object for January 1 of next year calculate the difference between currentDate and January 1 }
27
XP Tutorial 2New Perspectives on JavaScript, Comprehensive27 Adding Commands to the calcDays() Function
28
XP Tutorial 2New Perspectives on JavaScript, Comprehensive28 Calling the calcDays() Function
29
XP Tutorial 2New Perspectives on JavaScript, Comprehensive29 Working with Math Methods and Constants Math object –Used to perform mathematical tasks and store mathematical values Math methods –Used to perform advanced calculations Syntax for applying a Math method Math.method(expression)
30
XP Tutorial 2New Perspectives on JavaScript, Comprehensive30 Math Methods
31
XP Tutorial 2New Perspectives on JavaScript, Comprehensive31 Math Constants
32
XP Tutorial 2New Perspectives on JavaScript, Comprehensive32 Using the Math.floor() Method
33
XP Tutorial 2New Perspectives on JavaScript, Comprehensive33 Calculating the Hours, Minutes, and Seconds Left in the Year To calculate number of hours left in current day var hours = (days - Math.floor(days))*24; To calculate minutes left in current hour var minutes = (hours - Math.floor(hours))*60; To calculate seconds left in current minute var seconds = (minutes - Math.floor(minutes))*60;
34
XP Tutorial 2New Perspectives on JavaScript, Comprehensive34 Controlling How JavaScript Works with Numeric Values NaN –Not a Number isNaN() –Used to check whether a value is numeric –Syntax: isNaN(value)
35
XP Tutorial 2New Perspectives on JavaScript, Comprehensive35 Specifying the Number Format To control number of digits displayed by browser value.toFixed(n) Applying the toFixed() function testValue = 2.835; testValue.toFixed(0) // returns "3" testValue.toFixed(1) // returns "2.8" testValue.toFixed(2) // returns "2.84"
36
XP Tutorial 2New Perspectives on JavaScript, Comprehensive36 Numeric Functions and Methods
37
XP Tutorial 2New Perspectives on JavaScript, Comprehensive37 Working with Conditional Operators Conditional operator –Tests whether a certain condition is true or not –Syntax (condition) ? trueValue : falseValue Comparison operator –Compares value of one expression to another
38
XP Tutorial 2New Perspectives on JavaScript, Comprehensive38 Comparison Operators
39
XP Tutorial 2New Perspectives on JavaScript, Comprehensive39 Logical Operators
40
XP Tutorial 2New Perspectives on JavaScript, Comprehensive40 Running Timed Commands Time-delayed command –Run after a particular amount of time has passed –Syntax setTimeout("command", delay);
41
XP Tutorial 2New Perspectives on JavaScript, Comprehensive41 Running Commands at Specified Intervals Timed-interval command –Instructs browser to run same command repeatedly at specified intervals –Syntax setInterval("command",interval); –Method to stop command clearInterval();
42
XP Tutorial 2New Perspectives on JavaScript, Comprehensive42 Performing Special Mathematical Tasks Math.floor(), Math.ceil(), and Math.round() methods –Used to round values to the next highest, next lowest, and nearest integer No Math methods for –Rounding values to decimals
43
XP Tutorial 2New Perspectives on JavaScript, Comprehensive43 Generating Random Numbers Math.random() method –Generates random values Generating random number between 0 and 10 var randValue = 10*Math.random(); Generating random number from 20 to 30 var randValue = 20 + 10*Math.random();
44
XP Tutorial 2New Perspectives on JavaScript, Comprehensive44 Tips for Working with Operators and Expressions Use unary and assignment operators to create compact code When performing advanced mathematical calculations –Use constants from the Math object Never apply mathematical operations to text strings
45
XP Tutorial 2New Perspectives on JavaScript, Comprehensive45 Tips for Working with Operators and Expressions In case of logical error involving a mathematical operation –Use the isFinite() and isNaN() functions Use the toFixed() method to –Format numeric output
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.