Cos 381 Day 4. © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Agenda Exam 1 Corrected –3 A’s, 3 B’s and 2 C’s I have looked at all the assignment.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

JS1-1 Introduction to JavaScript (JavaScript 2) Xingquan (Hill) Zhu
Introduction to C Programming
COS 381 Day 19. Agenda  Assignment 5 Posted Due April 7  Exam 3 which was originally scheduled for Apr 4 is going to on April 13 XML & Perl (Chap 8-10)
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Chapter 6 © 2002 by Addison Wesley Longman, Inc. 1 Chapter 6 Sebesta: Programming the World Wide Web.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
25-Jun-15 JavaScript Language Fundamentals II. 2 Exception handling, I Exception handling in JavaScript is almost the same as in Java throw expression.
Cos 381 Day 4. © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Agenda Questions? Assignment 1 due January 31 (TUESDAY) Discussions on JAVASCRIPT.
Tutorial 14 Working with Forms and Regular Expressions.
CS 299 – Web Programming and Design Overview of JavaScript and DOM Instructor: Dr. Fang (Daisy) Tang.
Introduction to C Programming
Cos 381 Day 7. © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Agenda Assignment 2 Posted –Program a web-based Version of Soduku using JavaScript.
Tutorial 14 Working with Forms and Regular Expressions.
 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.
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
Chapter 4 © 2003 by Addison-Wesley, Inc Overview of JavaScript - Originally developed by Netscape, as LiveScript - Became a joint venture of Netscape.
CP476 Internet Computing JavaScript Client-Side Programming 1 1. What is JavaScript –Another script language for both client-side and sever-side programming.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
1 Overview JavaScript (Recommended Reading : Programming the World Wide Web, 4 th Edition, Robert W. Sebesta, Chapters 4, 5 – available at Steely Library)
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 4 The Basics of JavaScript. © 2006 Pearson Addison-Wesley. All rights reserved Overview of JavaScript - Originally developed by Netscape,
Chapter 4 © 2010 by Addison Wesley Longman, Inc Overview of JavaScript - Originally developed by Netscape, as LiveScript - Became a joint venture.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
Lecture 9 The Basics of JavaScript Boriana Koleva Room: C54
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
DHTML1-1 JavaScript and HTML Documents Xingquan (Hill) Zhu
1 Introduction to Web Application Introduction to Java Script.
Overview A regular expression defines a search pattern for strings. Regular expressions can be used to search, edit and manipulate text. The pattern defined.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
CS346 Regular Expressions1 Pattern Matching Regular Expression.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
Chapter 4 © 2005 by Addison Wesley Longman, Inc Overview of JavaScript - Originally developed by Netscape, as LiveScript - Became a joint venture.
Overview of JavaScript Developed by Netscape, as LiveScript Became a joint venture of Netscape and Sun in 1995, renamed JavaScript Now standard of the.
JavaScript, Fourth Edition
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
JS1-1 Introduction to JavaScript (JavaScript 1) Xingquan (Hill) Zhu
Programming for WWW (ICE 1338) Lecture #5 Lecture #5 July 7, 2004 In-Young Ko iko.AT. icu.ac.kr Information and Communications University (ICU) iko.AT.
Chapter 4 © 2014 by Pearson Education Overview of JavaScript - Originally developed by Netscape by Brendan Eich, as LiveScript - Became a joint venture.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Standard Types and Regular Expressions CS 480/680 – Comparative Languages.
Chapter 4 © 2013 by Pearson Overview of JavaScript - Originally developed by Netscape, as LiveScript - Became a joint venture of Netscape and Sun.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Chapter 4 © 2009 by Addison Wesley Longman, Inc Pattern Matching - JavaScript provides two ways to do pattern matching: 1. Using RegExp objects.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Arrays and Lists. What is an Array? Arrays are linear data structures whose elements are referenced with subscripts. Just about all programming languages.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Basic Objects. Math Object  Math.cos( x ), x in radians  Math.sqrt ( x )  Math.pow ( x, y )  Math.ceil( x )  etc.
4.1 Overview of JavaScript
4.1 Overview of JavaScript
Chapter 6 JavaScript: Introduction to Scripting
Chapter 4 Client-Side Programming: the JavaScript Language
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
JavaScript Syntax and Semantics
Chapter 8 JavaScript: Control Statements, Part 2
WEB PROGRAMMING JavaScript.
The Basics of JavaScript
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
JavaScript CS 4640 Programming Languages for Web Applications
- Regular expressions:
4.1 Overview of JavaScript
Chapter 8 JavaScript: Control Statements, Part 2
JavaScript CS 4640 Programming Languages for Web Applications
Presentation transcript:

Cos 381 Day 4

© 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Agenda Exam 1 Corrected –3 A’s, 3 B’s and 2 C’s I have looked at all the assignment one but have not finished the grading –Did not see any big issues Assignment 2 Posted –Program a web-based Version of Soduku using JavaScript and DOM – to learn how to playhttp:// –Due Monday February 20 –You don't have enough JavaScript and DOM instruction to finish but you have enough to get started –We will cover the tie between DOM and JavaScript next week. »Chap 5 & 6

© 2006 Pearson Addison-Wesley. All rights reserved Control Statements (continued) 3. Compound Expressions - The usual operators: &&, ||, and ! - The Boolean object has a method, toString, to allow them to be printed ( true or false ) - Selection Statements - - The usual if-then-else (clauses can be either single statements or compound statements)

© 2006 Pearson Addison-Wesley. All rights reserved Control Statements (continued) - Switch switch (expression) { case value_1: // value_1 statements case value_2: // value_2 statements … [default: // default statements] } - The statements can be either statement sequences or compound statements { statement 1; statement 2; …. statement N; } - The control expression can be a number, a string, or a Boolean - Different cases can have values of different types 

© 2006 Pearson Addison-Wesley. All rights reserved Control Statements (continued) - Loop statements while ( control_expression ) statement or cmpnd for ( init ; control ; increment ) statement or cmpnd - init can have declarations, but the scope of such variables is the whole script  do statement or compound while ( control_expression ) 4.7 Object Creation and Modification - Objects can be created with new - The most basic object is one that uses the Object constructor, as in var myObject = new Object(); - The new object has no properties - a blank object - Properties can be added to an object, any time

© 2006 Pearson Addison-Wesley. All rights reserved Object Creation and Modification (continued) var myAirplane = new Object(); myAirplane.make = "Cessna"; myAirplane.model = "Centurian"; - Objects can be nested, so a property could be itself another object, created with new - Properties can be accessed by dot notation or in array notation, as in var property1 = myAirplane["model"]; delete myAirplane.model; - Another Loop Statement - for ( identifier in object ) statement or compound for (var prop in myAirplane) document.write(myAirplane[prop] + " ");

© 2006 Pearson Addison-Wesley. All rights reserved Arrays - Objects with some special functionality - Array elements can be primitive values or references to other objects - Length is dynamic - the length property stores the length -Array objects can be created in two ways, with new, or - by assigning an array literal var myList = new Array(24, "bread", true); var myList2 = [24, "bread", true]; var myList3 = new Array(24); -The length of an array is the highest subscript towhich an element has been assigned, plus 1 myList[122] = "bitsy"; // length is 123 -Because the length property is writeable, you can set it to make the array any length you like, as in myList.length = 150; 

© 2006 Pearson Addison-Wesley. All rights reserved Arrays (continued) - Array methods: - join – e.g., var listStr = list.join(", "); - reverse - sort – e.g., names.sort(); - Coerces elements to strings and puts them in alphabetical order - concat – e.g., newList = list.concat(47, 26); - slice listPart = list.slice(2, 5); listPart2 = list.slice(2); - toString - Coerce elements to strings, if necessary, and catenate them together, separated by commas (exactly like join(", ") ) - push, pop, unshift, and shift 

© 2006 Pearson Addison-Wesley. All rights reserved Functions - function function_name ( [formal_parameters] ) { -- body – } - Return value is the parameter of return - If there is no return, or if the end of the function is reached, undefined is returned - If return has no parameter, undefined is returned -Functions are objects, so variables that reference them can be treated as other object references - If fun is the name of a function, ref_fun = fun;... ref_fun(); /* A call to fun */ - We place all function definitions in the head of the HTML document - All variables that are either implicitly declared or explicitly declared outside functions are global - Variables explicitly declared in a function are local

© 2006 Pearson Addison-Wesley. All rights reserved Functions (continued) - Parameters are passed by value, but when a reference variable is passed, the semantics are pass-by-reference - There is no type checking of parameters, nor is the number of parameters checked (excess actual parameters are ignored, excess formal parameters are set to undefined ) - All parameters are sent through a property array, arguments, which has the length property  - There is no clean way to send a primitive by reference - One dirty way is to put the value in an array and send the array’s name function by10(a) { a[0] *= 10; }... var listx = new Array(1);... listx[0] = x; by10(listx); x = listx[0];

© 2006 Pearson Addison-Wesley. All rights reserved Functions (continued) - To sort something other than strings into alphabetical order, write a function that performs the comparison and send it to the sort method - The comparison function must return a negative number, zero, or a positive number to indicate whether the order is ok, equal, or not function num_order(a, b) {return a - b;} - Now, we can sort an array named num_list with: num_list.sort(num_order); 4.10 An Example ->

© 2006 Pearson Addison-Wesley. All rights reserved Constructors - Used to initialize objects, but actually create the properties function plane(newMake, newModel, newYear){ this.make = newMake; this.model = newModel; this.year = newYear; } myPlane = new plane("Cessna","Centurnian", "1970"); - Can also have method properties function displayPlane() { document.write("Make: ", this.make, " "); document.write("Model: ", this.model, " "); document.write("Year: ", this.year, " "); } - Now add the following to the constructor: this.display = displayPlane;

© 2006 Pearson Addison-Wesley. All rights reserved Pattern Matching - JavaScript provides two ways to do pattern matching: 1. Using RegExp objects 2. Using methods on String objects - Simple patterns - Two categories of characters in patterns: a. normal characters (match themselves) b. metacharacters (can have special meanings in patterns--do not match themselves) \ | ( ) [ ] { } ^ $ * + ?. - A metacharacter is treated as a normal character if it is backslashed - period is a special metacharacter - it matches any character except newline

© 2006 Pearson Addison-Wesley. All rights reserved Pattern Matching (continued) search( pattern ) - Returns the position in the object string of the pattern (position is relative to zero); returns -1 if it fails var str = "Gluckenheimer"; var position = str.search(/n/); /* position is now 6 */ - Character classes - Put a sequence of characters in brackets, and it defines a set of characters, any one of which matches [abcd] - Dashes can be used to specify spans of characters in a class [a-z] - A caret at the left end of a class definition means the opposite [^0-9]

© 2006 Pearson Addison-Wesley. All rights reserved Pattern Matching (continued) - Character class abbreviations Abbr. Equiv. Pattern Matches \d [0-9] a digit \D [^0-9] not a digit \w [A-Za-z_0-9] a word character \W [^A-Za-z_0-9] not a word character \s [ \r\t\n\f] a whitespace character \S [^ \r\t\n\f] not a whitespace character - Variables in patterns are interpolated - Quantifiers - Quantifiers in braces QuantifierMeaning { n } exactly n repetitions { m,} at least m repetitions { m, n } at least m but not more than n repetitions

© 2006 Pearson Addison-Wesley. All rights reserved Pattern Matching (continued) - Quantifiers (continued) - Other quantifiers (just abbreviations for the most commonly used quantifiers) - * means zero or more repetitions e.g., \d* means zero or more digits - + means one or more repetitions e.g., \d+ means one or more digits - ? Means zero or one e.g., \d? means zero or one digit /\d\.\d\d/ matches? /\D\d\D/ matches? /\w\w\w/ matches? /xy{4}z/ matches?

© 2006 Pearson Addison-Wesley. All rights reserved Pattern Matching (continued) - Anchors - The pattern can be forced to match only at the left end with ^ ; at the end with $ e.g., /^Lee/ matches "Lee Ann" but not "Mary Lee Ann" /Lee Ann$/ matches "Mary Lee Ann", but not "Mary Lee Ann is nice" - The anchor operators ( ^ and $ ) do not match characters in the string--they match positions, at the beginning or end - Pattern modifiers - The i modifier tells the matcher to ignore the case of letters /oak/i matches "OAK" and "Oak" and …

© 2006 Pearson Addison-Wesley. All rights reserved Pattern Matching (continued) - Pattern modifiers (continued) - The x modifier tells the matcher to ignore whitespace in the pattern (allows comments in patterns) - Other Pattern Matching Methods of String replace( pattern, string ) - Finds a substring that matches the pattern and replaces it with the string ( g modifier can be used) var str = "Some rabbits are rabid"; str.replace(/rab/g, "tim"); str is now "Some timbits are timid" $1 and $2 are both set to "rab"

© 2006 Pearson Addison-Wesley. All rights reserved Pattern Matching (continued ) match( pattern ) - The most general pattern-matching method - Returns an array of results of the pattern- matching operation - With the g modifier, it returns an array of the substrings that matched - Without the g modifier, first element of the returned array has the matched substring, the other elements have the values of $1, … var str = "My 3 kings beat your 2 aces"; var matches = str.match(/[ab]/g); - matches is set to ["b", "a", "a"] split( parameter ) "," and /,/ both work 

© 2006 Pearson Addison-Wesley. All rights reserved Debugging JavaScript - IE6 - Select Internet Options from the Tools menu - Choose the Advanced tab - Uncheck the Disable script debugging box - Check the Display a notification about every script error box - Now, a script error causes a small window to be opened with an explanation of the error - NS7 - - Select Tasks, Tools, and JavaScript Console - A small window appears to display script errors - Remember to Clear the console after using an error message – avoids confusion - NS7 - Select Tools, Web Development, JavaScript Console