XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and...

48
Tutorial 3 New Perspectives on JavaScript, Comprehensive 1 XP Tutorial 3 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar

description

XP Tutorial 3New Perspectives on JavaScript, Comprehensive3 Objectives Work with For loops Work with While loops Loop through the contents of an array Work with If, If... Else, and multiple conditional statements

Transcript of XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and...

Page 1: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 1

XP

Tutorial 3

Working with Arrays, Loops, and Conditional Statements

Creating a Monthly Calendar

Page 2: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 2

XPObjectives

• Create an array• Populate and reference values from an array• Work with array methods

Page 3: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 3

XPObjectives

• Work with For loops• Work with While loops• Loop through the contents of an array• Work with If, If... Else, and multiple

conditional statements

Page 4: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 4

XPObjectives

• Use arrays, loops, and conditional statements to create a table

• Work with break, continue, and label commands

Page 5: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 5

XPIntroducing the Monthly Calendar

• Program requirements– Easily usable on other Web pages– JavaScript code for calendar should be placed in

an external file named calendar.js– calendar.js should be run from a single function– Accessing and displaying monthly calendar table

should require minimal amount of recoding

Page 6: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 6

XPCreating and Formatting the Monthly Calendar

Page 7: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 7

XPThe Calendar Style Sheet

• Classes or id names to be created– Calendar is set in a table with id calendar_table– Cell containing calendar title has id

calendar_head– Seven cells containing days of week abbreviations

belong to class calendar_weekdays– Cells containing dates of month belong to class

calendar_dates– Cell containing current date has id

calendar_today

Page 8: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 8

XPThe Contents of the calendar.css Style Sheet

Page 9: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 9

XPThe calendar() Function

• Initial code for the calendar() functionfunction calendar() {

document.write("<table id='calendar_table'>");

document.write("</table>");}

Page 10: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 10

XPThe calendar() Function

• Main tasks when creating calendar table– Creating the table header row– Creating the table row containing the names of the

days of the week– Creating the rows containing the days of the

month

Page 11: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 11

XPWorking with Arrays

• Array – Collection of data values organized under a single

name– Each data value has a number or index – General form

array[I]

Page 12: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 12

XPCreating and Populating an Array

• To create an arrayvar array = new Array(length);

• Arrays without a defined length can take up more memory

• Creating and populating an arrayvar array = new Array(values);

• Array literalvar array = [values];

Page 13: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 13

XP Creating the monthName Array

Page 14: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 14

XPThe Completed writeCalTitle() Function

Page 15: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 15

XPWorking with Array Length

• JavaScript arrays – Not required to stay at a fixed size– Definition of a value not required for every item

• Sparse arrays– Arrays with several missing or null items

Page 16: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 16

XPReversing an Array

• Arrays – Associated with a collection of methods that allow

you to change their contents, order, and size– Syntax for applying methods

array.method()• Methods for changing order of array items

after array is created– reverse() and sort()

Page 17: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 17

XPSorting an Array

• sort() method – Sorts array items in alphabetical order

• Compare function– Used to sort nontextual data correctly– Compares values of two adjacent items in the

array at a time• Applying compare function to sort() method

array.sort(function)

Page 18: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 18

XPExtracting and Inserting Array Items

• Subarray– A section of an array

• slice() method– Extracts a part of an array– Syntax: array.slice(start, stop)– Can be used to insert new items into an array

array.splice(start, size, values)

Page 19: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 19

XPExtracting and Inserting Array Items

• Most efficient methods to insert or remove items– push()– pop()– unshift() – shift()

Page 20: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 20

XPArray Methods

Page 21: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 21

XPWorking with Program Loops

• Program loop– Set of commands that executes repeatedly until a

stopping condition is met• The For loop

– Counter variable is used to track number of times a set of commands is run

– General structure for (start; continue; update) { commands

}

Page 22: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 22

XPThe For Loop

• Can be nested inside another• Command block

– Collection of commands that is run each time through a loop

– Distinguished by opening and closing curly braces { }

Page 23: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 23

XPCounter Values in the For loop

Page 24: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 24

XPFor Loops and Arrays

• For loops – Used to cycle through different values contained

within an array– General structure for accessing each value in

array for (var i=0; i < array.length; i++) { commands involving array[i] }

Page 25: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 25

XPCreating the writeDayNames() Function

Page 26: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 26

XPThe While Loop

• A command block is run as long as a specific condition is met

• Condition does not depend on value of a counter variable

• General syntaxwhile (continue) {

commands}

Page 27: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 27

XPThe Do/While Loop

• Test to determine whether to continue to loop is made after the command block is run

• Structuredo {

commands } while (continue);

Page 28: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 28

XPWorking with Conditional Statements

• Conditional statement – Runs a command or command block only when

certain conditions are met• If statement

– Most common conditional statement

Page 29: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 29

XPThe Initial daysInMonth()

Page 30: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 30

XPCalculating Leap Years

Page 31: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 31

XPThe If Statement

• Syntaxif (condition) { commands}

• Modulus operator – Returns the integer remainder after dividing one

integer by another

Page 32: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 32

XPNesting If Statements

• General structureif (thisYear % 4 == 0) { if statement for century years}

Page 33: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 33

XPThe Complete daysInMonth() Function

Page 34: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 34

XPThe If...Else Statement

• General structureif (condition) { commands if true} else { commands if false}

Page 35: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 35

XPUsing Multiple Else...If Statements

• General structureif (condition 1) { first command block}else if (condition 2) { second command block}else if (condition 3) { third command block}...else { default command block}

Page 36: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 36

XPThe Switch Statement

• Syntaxswitch (expression) {

case label1: commands1 break;

case label2: commands2 break; case label3: commands3 break;... default: default commands}

Page 37: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 37

XPCreating the calendar() Function

• Steps– Calculate day of week in which the month starts– Precede first day of month with blank table cells– Loop through days of the month

• Writing each date in a different table cell• Starting a new table row on each Sunday• Ending the table rows after each Saturday

– Highlight current date in calendar

Page 38: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 38

XPBuilding the Monthly Calendar

Page 39: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 39

XPSetting the First Day of the Month

Page 40: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 40

XPPlacing the First Day of the Month

• Loop to create blank table cellsdocument.write("<tr>");for (var i=0; i < weekDay; i++) {

document.write("<td></td>");}

Page 41: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 41

XPThe While loop for Adding the Calendar Days

Page 42: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 42

XP The Final calendar() Function

Page 43: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 43

XPManaging Program Loops and Conditional Statements

• Break command– Used to terminate any program loop or conditional

statement– Often used to exit a program loop– Syntax: break;

Page 44: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 44

XPThe continue Command

• Stops processing commands in current iteration of loop and jumps to next iteration

• Examplevar total = 0;for (var i=0; i < data.length; i++) { if (data[i]==null) continue; // continue with the next iteration

total += data[i];}

Page 45: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 45

XPStatement Labels

• Labels – Used to identify statements in your code– Often used with break and continue commands to

direct a program to a particular statement• Syntax:

label: statement• Some programmers

– Discourage use of break, continue, and label statements

Page 46: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 46

XPTips for Arrays, Program Loops, andConditional Statements

• Save space in your program code by – Declaring and populating each array in a single

new Array() declaration• Use array methods like

– sort() and reverse() to quickly rearrange contents of arrays

• Use a For loop when – Your loop contains a counter variable

Page 47: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 47

XPTips for Arrays, Program Loops, andConditional Statements

• Use While loop for – More general stopping conditions

• To simplify your code– Avoid nesting too many levels of If statements

• Use Switch statement for – Conditional statements that involve variables with

several different possible values

Page 48: XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.

Tutorial 3 New Perspectives on JavaScript, Comprehensive 48

XPTips for Arrays, Program Loops, andConditional Statements

• Avoid using break and continue statements to – Cut off loops unless necessary– Instead

• Set break conditions in the conditional expression for a loop