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

Slides:



Advertisements
Similar presentations
Tonight’s JavaScript Topics 1 Conditional Statements: if and switch The Array Object Looping Statements: for, while and do-while.
Advertisements

Objectives Using functions to organize PHP code
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Tutorial 16 Working with Dynamic Content and Styles.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
JavaScript Friend or Foe?. History of Java and JavaScript Oak and Coffee? ECMAscript European Computer Manufacturers Association JScript?
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Decisions, Loops, and Arrays Achmad Arwan, S.Kom.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
CISC474 - JavaScript 03/02/2011. Some Background… Great JavaScript Guides: –
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
CIS 375—Web App Dev II JavaScript II.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
CPS120 Introduction to Computer Science Iteration (Looping)
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Decision Making with Control Structures and Statements (non-audio version) © Dr. David.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
XP Tutorial 16 New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with Dynamic Content and Styles Creating a Dynamic Table of Contents.
L OO P S While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
JavaScript, Fourth Edition
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Matlab tutorial course Lesson 4: Writing your own functions: programming constructs
CPS120 Introduction to Computer Science Iteration (Looping)
XP Tutorial 2 New Perspectives on JavaScript, Comprehensive1 Working with Operators and Expressions Creating a New Year’s Day Countdown Clock.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
Working with Loops, Conditional Statements, and Arrays.
CSI 3125, Preliminaries, page 1 Control Statements.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
1 CS428 Web Engineering Lecture 13 Flow Control & Loops (JavaScript - III)
JavaScript and Ajax (Control Structures) Week 4 Web site:
JavaScript, Sixth Edition
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
XP Tutorial 10 New Perspectives on JavaScript, Comprehensive 1 Working with Dynamic Content and Styles Creating a Dynamic Table of Contents.
Loops BIS1523 – Lecture 10.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
JavaScript: Control Statements.
Week#2 Day#1 Java Script Course
JavaScript: Control Statements I
Objectives Create an array Work with array properties and methods
Chapter 8 JavaScript: Control Statements, Part 2
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
2.6 The if/else Selection Structure
CSC215 Lecture Control Flow.
CIS 136 Building Mobile Apps
How to allow the program to know when to stop a loop.
Chapter 8 JavaScript: Control Statements, Part 2
Presentation transcript:

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

XP Tutorial 3New Perspectives on JavaScript, Comprehensive2 Objectives Create an array Populate and reference values from an array Work with array methods

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

XP Tutorial 3New Perspectives on JavaScript, Comprehensive4 Objectives Use arrays, loops, and conditional statements to create a table Work with break, continue, and label commands

XP Tutorial 3New Perspectives on JavaScript, Comprehensive5 Introducing 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

XP Tutorial 3New Perspectives on JavaScript, Comprehensive6 Creating and Formatting the Monthly Calendar

XP Tutorial 3New Perspectives on JavaScript, Comprehensive7 The 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

XP Tutorial 3New Perspectives on JavaScript, Comprehensive8 The Contents of the calendar.css Style Sheet

XP Tutorial 3New Perspectives on JavaScript, Comprehensive9 The calendar() Function Initial code for the calendar() function function calendar() { document.write(" "); }

XP Tutorial 3New Perspectives on JavaScript, Comprehensive10 The 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

XP Tutorial 3New Perspectives on JavaScript, Comprehensive11 Working with Arrays Array –Collection of data values organized under a single name –Each data value has a number or index –General form array[I]

XP Tutorial 3New Perspectives on JavaScript, Comprehensive12 Creating and Populating an Array To create an array var array = new Array(length); Arrays without a defined length can take up more memory Creating and populating an array var array = new Array(values); Array literal var array = [values];

XP Tutorial 3New Perspectives on JavaScript, Comprehensive13 Creating the monthName Array

XP Tutorial 3New Perspectives on JavaScript, Comprehensive14 The Completed writeCalTitle() Function

XP Tutorial 3New Perspectives on JavaScript, Comprehensive15 Working 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

XP Tutorial 3New Perspectives on JavaScript, Comprehensive16 Reversing 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()

XP Tutorial 3New Perspectives on JavaScript, Comprehensive17 Sorting 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)

XP Tutorial 3New Perspectives on JavaScript, Comprehensive18 Extracting 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)

XP Tutorial 3New Perspectives on JavaScript, Comprehensive19 Extracting and Inserting Array Items Most efficient methods to insert or remove items –push() –pop() –unshift() –shift()

XP Tutorial 3New Perspectives on JavaScript, Comprehensive20 Array Methods

XP Tutorial 3New Perspectives on JavaScript, Comprehensive21 Working 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 }

XP Tutorial 3New Perspectives on JavaScript, Comprehensive22 The 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 { }

XP Tutorial 3New Perspectives on JavaScript, Comprehensive23 Counter Values in the For loop

XP Tutorial 3New Perspectives on JavaScript, Comprehensive24 For 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] }

XP Tutorial 3New Perspectives on JavaScript, Comprehensive25 Creating the writeDayNames() Function

XP Tutorial 3New Perspectives on JavaScript, Comprehensive26 The 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 syntax while (continue) { commands }

XP Tutorial 3New Perspectives on JavaScript, Comprehensive27 The Do/While Loop Test to determine whether to continue to loop is made after the command block is run Structure do { commands } while (continue);

XP Tutorial 3New Perspectives on JavaScript, Comprehensive28 Working with Conditional Statements Conditional statement –Runs a command or command block only when certain conditions are met If statement –Most common conditional statement

XP Tutorial 3New Perspectives on JavaScript, Comprehensive29 The Initial daysInMonth()

XP Tutorial 3New Perspectives on JavaScript, Comprehensive30 Calculating Leap Years

XP Tutorial 3New Perspectives on JavaScript, Comprehensive31 The If Statement Syntax if (condition) { commands } Modulus operator –Returns the integer remainder after dividing one integer by another

XP Tutorial 3New Perspectives on JavaScript, Comprehensive32 Nesting If Statements General structure if (thisYear % 4 == 0) { if statement for century years }

XP Tutorial 3New Perspectives on JavaScript, Comprehensive33 The Complete daysInMonth() Function

XP Tutorial 3New Perspectives on JavaScript, Comprehensive34 The If...Else Statement General structure if (condition) { commands if true } else { commands if false }

XP Tutorial 3New Perspectives on JavaScript, Comprehensive35 Using Multiple Else...If Statements General structure if (condition 1) { first command block } else if (condition 2) { second command block } else if (condition 3) { third command block }... else { default command block }

XP Tutorial 3New Perspectives on JavaScript, Comprehensive36 The Switch Statement Syntax switch (expression) { case label1: commands1 break; case label2: commands2 break; case label3: commands3 break;... default: default commands }

XP Tutorial 3New Perspectives on JavaScript, Comprehensive37 Creating 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

XP Tutorial 3New Perspectives on JavaScript, Comprehensive38 Building the Monthly Calendar

XP Tutorial 3New Perspectives on JavaScript, Comprehensive39 Setting the First Day of the Month

XP Tutorial 3New Perspectives on JavaScript, Comprehensive40 Placing the First Day of the Month Loop to create blank table cells document.write(" "); for (var i=0; i < weekDay; i++) { document.write(" "); }

XP Tutorial 3New Perspectives on JavaScript, Comprehensive41 The While loop for Adding the Calendar Days

XP Tutorial 3New Perspectives on JavaScript, Comprehensive42 The Final calendar() Function

XP Tutorial 3New Perspectives on JavaScript, Comprehensive43 Managing 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;

XP Tutorial 3New Perspectives on JavaScript, Comprehensive44 The continue Command Stops processing commands in current iteration of loop and jumps to next iteration Example var total = 0; for (var i=0; i < data.length; i++) { if (data[i]==null) continue; // continue with the next iteration total += data[i]; }

XP Tutorial 3New Perspectives on JavaScript, Comprehensive45 Statement 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

XP Tutorial 3New Perspectives on JavaScript, Comprehensive46 Tips for Arrays, Program Loops, and Conditional 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

XP Tutorial 3New Perspectives on JavaScript, Comprehensive47 Tips for Arrays, Program Loops, and Conditional 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

XP Tutorial 3New Perspectives on JavaScript, Comprehensive48 Tips for Arrays, Program Loops, and Conditional Statements Avoid using break and continue statements to –Cut off loops unless necessary –Instead Set break conditions in the conditional expression for a loop