INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.

Slides:



Advertisements
Similar presentations
CSCI 6962: Server-side Design and Programming Input Validation and Error Handling.
Advertisements

Java Programming Strings Chapter 7.
Session 8 JavaScript/Jscript: Objects Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Lecture 3 Term 2 23/1/12. Arrays You can create an array with the Array() constructor and the new operator. There are three ways to invoke the Array()
Javascript Client-side scripting. Up to now  We've seen a little about how to control  content with HTML  presentation with CSS  Javascript is a language.
Databases Lab 5 Further Select Statements. Functions in SQL There are many types of functions provided. The ones that are used most are: –Date and Time.
1 JavaScript/Jscript 6 Objects. 2 Introduction Up till now –JavaScript used to illustrate basic programming concepts JavaScript can also –Manipulate every.
JavaScript Objects Objects – JavaScript is an object-based language Has built-in objects we will use – You can create your own objects We concentrating.
1 CS101 Introduction to Computing Lecture 38 String Manipulations (Web Development Lecture 13)
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Ten String Manipulation and Menus.
2 Alerts and the If/Else Conditional Statement CONTINUED There's No Right Way to Do It There are, literally, a million ways to write any given script.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
Scripting Languages.
Variables & Data types Flash ActionScript 3.0 Introduction to Thomas Lövgren, Flash developer
UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS.
 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.
Native JavaScript in Hyperion Intelligence Unleashing the Power of Object- Oriented Programming.
JavaScript: Objects Outline 12.1 Introduction 12.2 Thinking About Objects 12.3 Math Object 12.4 String Object Fundamentals of Characters and Strings.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Javascript core objects. Review question Which of the following is TRUE concerning a variable declared globally? a. The variable cannot be accessed anywhere.
Unit 7 JavaScript Core Objects. Core Objects Core Objects are objects built right into the language. For a complete list of properties and objects available.
JavaScript For...In Statement The for...in statement loops through the elements of an array or through the properties of an object. Syntax for (variable.
JavaScript Functions & Objects. JavaScript Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded URI.
Objects.  Java Script is an OOP Language  So it allows you to make your own objects and make your own variable types  I will not be going over how.
RegExp. Regular Expression A regular expression is a certain way to describe a pattern of characters. Pattern-matching or keyword search. Regular expressions.
© 2012 EMC Publishing, LLC Slide 1 Chapter 7 The Math Class  Includes shared methods that perform common math functions.  Math Class methods include:
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
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.
More on Objects. Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Objects.
CSC Programming for Science Lecture 7: Math Functions.
String Manipulation Chapter 15 This chapter explains the String facilities. You have already seen some of the main methods of the String class.
M. Taimoor Khan Javascript Objects  Every data-type defined in Javascript is an object  It has a class definition for.
Topics Class Basics and Benefits Creating Objects Using Constructors Calling Methods Using Object References Calling Static Methods and Using Static Class.
Keeping it Neat: Functions and JavaScript Source Files Chapter 7.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 13 – JavaScript/Jscript: Objects Outline 13.1Introduction 13.2Thinking About Objects 13.3.
Functions and Arrays. Predefined Functions eval(condition) –Evaluates (executes) JavaScript syntax –Eval returns an undefined value parseInt(string) and.
CS346 Regular Expressions1 Pattern Matching Regular Expression.
EXPRESSION Transformation. Introduction ►Transformations help to transform the source data according to the requirements of target system and it ensures.
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
Advanced JavaScript Topics – Page 1 of 31CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: Advanced JavaScript.
 2001 Deitel & Associates, Inc. All rights reserved. 1 Chapter 18 – JavaScript/Jscript: Objects Outline 18.1Introduction 18.2Thinking About Objects 18.3.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
JS1-1 Introduction to JavaScript (JavaScript 1) Xingquan (Hill) Zhu
CSC 330 E-Commerce Teacher Ahmed Mumtaz Mustehsan Ahmed Mumtaz Mustehsan GM-IT CIIT Islamabad GM-IT CIIT Islamabad CIIT Virtual Campus, CIIT COMSATS Institute.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Chapter 12: Objects CIS 275—Web Application Development for Business I.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming strings.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
INT222 – Internet Fundamentals Week 11: RegExp Object and HTML5 Form Validation 1.
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
Hazırlayan:Emin BORANDAĞ 2/17/ Numerik Değerler Kullanımı Integers are considered accurate up to 15 digits. Try it function myFunction() { var x.
JavaScript and Ajax (JavaScript Data Types) Week 2 Web site:
Tutorial 11 1 JavaScript Operators and Expressions.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 12 - JavaScript: Objects Outline 12.1 Introduction 12.2 Thinking About Objects 12.3 Math Object.
JavaScript Tutorial. What is JavaScript JavaScript is the programming language of HTML and the Web Programming makes computers do what you want them to.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Basic Objects. Math Object  Math.cos( x ), x in radians  Math.sqrt ( x )  Math.pow ( x, y )  Math.ceil( x )  etc.
11 JavaScript: Objects.
Chapter 4 Mathematical Functions, Characters, and Strings
Open Source Server Side Scripting MySQL Functions
SEEM4570 Tutorial 05: JavaScript as OOP
Java Script.
JavaScript: Objects.
BY: SITI NURBAYA ISMAIL FACULTY of COMPUTER and MATHEMATICAL SCIENCES
CIS16 Application Development and Programming using Visual Basic.net
Tutorial 4 JavaScript as OOP Li Xu
VBScript Session 10.
INT222 - Internet Fundamentals
Presentation transcript:

INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE

22 Outline JavaScript Build-in Objects JavaScript Built-in/ Global objects:  String  Array  Date  Math  Number  Boolean  Regular Expression Next Class:  HTML

JavaScript Objects Built-in Objects/ Global Objects  String  Array  Date  Math  Number  Boolean  Regular Expression User-defined Objects 3

4 String Object – property & methods String.length - returns the number of character in a string. String.charAt( index) - returns the character at the specific index. - index starts from 0. - var s = “hello”; alert(s[0]); String.charCodeAt(index) - returns the unicode of the character in place of character. - Unicode provides a unique number for every character, no matter what the platform is.

5 String Object - methods String.indexOf( substring)  returns the position at which the character or string begins  -1 means the character or string is not in the string. String.lastIndexOf( substring )  returns the position at which the last occurrence of your character or string.  -1 means the character or string is not in the string.

6 String Object - methods String.substr(startIndex,subLength)  returns a sub string, starting from startIndex with the length subLength e.g., var s= "abcdefg"; alert(s.substr(2,3)); // cde String.substring(startIndex,y)  returns a sub string, starting from index startIndex (inclusive) ending at index y (not inclusive) e.g., var s= "abcdefg"; alert(s.substring(2,3)); //c

7 String Object - methods String.toLowerCase( )  converts a string to lowercase. String.toUpperCase( )  converts a string to uppercase.

String Object - methods String.replace(srchStr, repStr) – searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced. E.g., var s="Hello all!"; var x=s.replace("all","ALL"); alert(x); Outpu: Hello ALL!

String.trim() – Trim white space from the beginning and the end String.trimLeft() – Trim white space from the left side String.trimRight() – Trim white space from the right side var s = " Good morning "; var n1 = s.length; var n2 = s.trim().length; alert(n1 + "-----"+ n2); 9 String Object - methods

var s= "Hello ALL!"; var len= s.length; alert("Length of " + s + " is " + len); alert("string is: "+s + "\n" + "charAt(2) is: "+ s.charAt(2) + "\n"+ "charCodeAt(6) is: " + s.charCodeAt(6) + "\n"+ "indexOf('l') is: " + s.indexOf('l')+ "\n" + "indexOf('all') is: " + s.indexOf('all')+ "\n" + "lastIndexOf('l') is: " + s.lastIndexOf('l')+ "\n" + "lastIndexOf('all') is: " + s.lastIndexOf('all')+ "\n" + "substr(2,4) is " + s.substr(2,4) + "\n" + "substr(2, 20) is " + s.substr(2,20)+ "\n" + "substring(2,4) is "+ s.substring(2,4) + "\n" + "substring(2,20) is " + s.substring(2, 20) + "\n" + "toLowerCase() is " + s.toLowerCase() + "\n" + "toUpperCase() is " + s.toUpperCase() + " \n" + "replace('ALL ! ', ' all ') is " + s.replace("ALL !", " all ") + "\n "+ "trim() is " + s.trim() );

Summary of String property/ functions var s = “Hello”; s.length s.charAt( index) s.charCodeAt(index) s.indexOf( substring) s.lastIndexOf(substring) s.substr(startIndex,subLength) s.substring(startIndex,y) s.toLowerCase( ) s.toUpperCase( ) s.replace(srchStr, repStr) s.trim() s.trimLeft() s.trimRight() 11

12 Array Object Index starts from 0. When dealing with arrays,  defining an array  assigning values (initializing) to an array  accessing an array  updating an array

13 Array Object Defining an array (1) var name_of_array = new Array(number_of_elements); var product = new Array(5); (2) var product= new Array("books","..","..","..","plates"); (3) var product = [“books”, "..","..","..","plates“]; (4) var product = []; Assigning values (initializing) to an array var product= new Array(5) product[0]="books"; product[1]="cars"; product[2]="cups"; product[3]="dishes"; product[4]="plates";

14 Array Object Accessing an array for (var x=0; x<product.length; x++) { alert(“No. " + x + " is " + product[x]); } Updating an array assign a new value to that occurrenece: product[3]=“cutlery”;

15 Array Object - Property Array.length property  return the number of occurrences in an array (how many items in an array).  alert(“the length of the array is " + product.length);

Array Object - methods Array.concat(arr1,arr2, …, arrN): Joins two or more arrays, and returns a copy of the joined arrays. var array1 = ["a1", "a2"]; var array2 = new Array(); array2[0] = "b0"; array2[1]="b1"; array2[2] = "b2"; var array3=["c1", "c2", "c3"] var con1 = array1.concat(array2, array3); alert(con1); alert(con1[3]); // b1 alert(con1.join()); alert(con1.join(" and "));

17 Array Object - methods Array.join( separator)  Return a string, which joins all the elements of an array, separated by a specified string separator (if none is specified, the default is a comma). product.join(); product.join("*"); product.join(“ and "); product.join(" "); Array.toString(): Converts an array to a string, and returns the result. – var array1 = ["a1", "a2"]; alert(array1.toString()+"..."); // a1, a2…

Array.indexOf(elem): Search the array for an element and returns it's FIRST position, if not found, return -1. – var array1 = ["a1", "a2", "a1"]; alert(array1.indexOf("a1")); //0 Array.lastIndexOf(elem): Search the array for an element, starting at the end, and returns it's position. – var array1 = ["a1", "a2", "a1"]; alert(array1.lastIndexOf("a1")); // 2 Array Object - methods

Array.reverse(): Reverses the order of the elements in an array. – alert(array1.reverse().join()); Array.sort(): Sorts the elements of an array. – var array1 = ["ba1", "ca2", "a3"]; array1.sort(); alert(array1); Descending order ? Array Object - methods Sort & reverse

Array.pop(): Removes the last element of an array, and returns that element – var array1 = ["a1", "a2", "a3"]; var x = array1.pop(); alert(x); // a3 Array.push(new_Element): Adds new elements to the end of an array, and returns the new length. – var array1 = ["a1", "a2", "a3"]; var x = array1.push("a4"); alert(array1); alert(x); // 4 Array Object - methods

Array.shift(): Removes the first element of an array, and returns that element. – var array1 = ["ba1", "ca2", "a3"]; var x = array1.shift(); alert(x); // ba1 Array.unshift():Adds new elements to the beginning of an array, and returns the new length. – var array1 = ["ba1", "ca2", "a3"]; var x = array1.unshift("a5", "a6"); alert(x); // 5 alert(array1); // a5, a6, ba1, ca2, a3 Array Object - methods

Array.slice(start, end): returns the selected elements in an array, as a new array object. selects the elements starting at the given start argument, and ends at, but does not include, the given end argument. Note: The original array will not be changed. – var array1 = ["ba1", "ca2", "a3"]; var x = array1.unshift("a5", "a6"); alert(array1); // a5, a6, ba1, ca2, a3 alert(array1.slice(1,3)); // a6, ba1 Array Object - methods

Summer of Array property/ functions Array.length Array.concat(arr1,arr2, …, arrN) Array.join( separator) Array.indexOf(elem): Array.lastIndexOf(elem): Array.reverse(): Array.sort(): Array.pop(): Array.push(new_Element): Array.shift(): Array.unshift(): Array.slice(start, end):

24 Date Object Getter  getMonth() returns 0 through = January  getDate() returns 1 through 31  getDay() returns 0 through = Sunday  getFullYear() returns 4 digit year  getHours() returns 0 through 23  getMinutes() returns 0 through 59  getSeconds() returns 0 through 59  getMilliseconds() returns 0 through 999  toLocaleString() returns the local date/time as a string.  toGMTString() returns the GMt date/time as a string. GMT: Greenwich Mean Time 

var d = new Date(); alert(d); alert("getMonth() = "+ d.getMonth()); alert("getDate() = "+ d.getDate()); alert("getDay() = "+ d.getDay()); alert("getFullYear() = "+ d.getFullYear()); alert("getHours() = "+ d.getHours()); alert("getTime()" + d.getTime()); alert("toLocaleString() = "+ d.toLocaleString()); alert("toGMTString()" + d.toGMTString()); Example:

26 Date Object Four ways of instantiating a date:  new Date() // current date and time  new Date(milliseconds) //milliseconds since 1970/01/01  new Date(dateString)  new Date(year, month, day, hours, minutes, seconds, milliseconds) today = new Date() var d1 = new Date("October 13, :13:00") var d2 = new Date(79,5,24) var d3 = new Date(79,5,24,11,33,0) var d4 = new Date("2015/1/13 09:32");

27 Date Object setter  setMonth() set month 0 through = January  setDate() set date of month 1 through 31  setDay() set day of week 0 through = Sunday  setFullYear() set 4 digit year  setHours() set hours 0 through 23  setMinutes() set minutes 0 through 59  setSeconds() set seconds 0 through 59  setMilliseconds() set milliseconds 0 through 999

28 Date Object Example: new_date=new Date(); alert(new_date); new_date.setMonth(3); // 0 through 11 alert(new_date); new_date.setDate(10); // 1 through 31 alert(new_date); new_date.setFullYear(1989); // 4 digit year alert(new_date);

29 Date Object Compare two dates: Calculate the difference in Milliseconds var x=new Date(); x.setFullYear(2015,11,25); var today = new Date(); if (today<x) { alert("Today is before this year’s Christmas"); var y = x-today; alert("There are "+ y + "Milliseconds"); var k = y/1000/60/60/24; alert("There are "+ k + "days"); } else { alert("Today is after Christmas"); }

30 Math Object - Properties PropertyDescription EReturns Euler's number (approx ) LN2Returns the natural logarithm of 2 (approx ) LN10Returns the natural logarithm of 10 (approx ) LOG2EReturns the base-2 logarithm of E (approx ) LOG10EReturns the base-10 logarithm of E (approx ) PIReturns PI (approx ) SQRT1_2Returns the square root of 1/2 (approx ) SQRT2Returns the square root of 2 (approx ) alert("Euler's number: " + Math.E);

31 MethodDescription abs(x)Returns the absolute value of x acos(x)Returns the arccosine of x, in radians asin(x)Returns the arcsine of x, in radians atan(x) Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians atan2(y,x)Returns the arctangent of the quotient of its arguments ceil(x)Returns x, rounded upwards to the nearest integer cos(x)Returns the cosine of x (x is in radians) exp(x)Returns the value of E x floor(x)Returns x, rounded downwards to the nearest integer log(x)Returns the natural logarithm (base E) of x max(x,y,z,...,n)Returns the number with the highest value min(x,y,z,...,n)Returns the number with the lowest value pow(x,y)Returns the value of x to the power of y random()Returns a random number between 0 and 1 round(x)Rounds x to the nearest integer sin(x)Returns the sine of x (x is in radians) sqrt(x)Returns the square root of x tan(x)Returns the tangent of an angle Math Object Methods alert("abs: " + Math.abs(-6));

Number Object PropertyDescription MAX_VALUE Returns the largest number possible in JavaScript( e+308 ) MIN_VALUE Returns the smallest number possible in JavaScript (5e-324 ) NEGATIVE_ INFINITY Returns negative infinity (-Infinity) POSITIVE_I NFINITY Represents infinity (returned on overflow) NaNRepresent a “Not-a-Number” value (isNaN()) The Number object is an object wrapper for primitive numeric values. Number objects are created with new Number(). var num = new Number(value); If the value parameter cannot be converted into a number, it returns NaN (Not-a-Number). These properties can only be accessed as Number.MAX_VALUE.

Number Object - methods toFixed(x): – formats a number to a specified number (x) of digits after decimal point. – X is optional. Default is 0 (no digits after the decimal point). var num = new Number( ); var a = num.toFixed(); // 13 var b = num.toFixed(2); // var c = num.toFixed(3); // var d = num.toFixed(6); // alert("= " + a + '\n' + b+ '\n'+c+ '\n'+d);

Number Object - methods toPrecision(x): – formats a number to a specified length (x). – A decimal point and nulls are added (if needed), to create the specified length. var num = new Number( ); var a = num.toPrecision(); // var b = num.toPrecision(2); // 13 var c = num.toPrecision(3); // 13.4 var d = num.toPrecision(10); // alert("= " + a + '\n' + b+ '\n'+c+ '\n'+d);

Number Object - Methods valueOf() :return the primitive value of a Number object. var num = 15; var n = num.valueOf();

Number Object - methods toString(radix): converts a Number object to a string. var num = 15; var a = num.toString() //15 var b = num.toString(2); // 1111 var c = num.toString(8); // 17 var d = num.toString(16); //f

Boolean Object two values: true or false. Create a Boolean object: – var myBoolean=new Boolean(value); – Value is optional. If no initial value, or one of the following: – 0 – -0 – null – "" – false – undefined – NaN The Boolean object is set to false.

Boolean Object - Methods toString(): – Converts a Boolean value to a string, and returns the result. var bool = new Boolean(1); var myvar = bool.toString(); alert(myvar); output: true

Boolean Object - Methods valueOf(): – Returns the primitive value of a Boolean object var bool = new Boolean(null); var myvar = bool.valueOf(); alert(myvar); output: false

Regular expressions: patterns used to match character combinations in strings. Regular expressions are objects. To search in a text, use a pattern to describe what you are searching for. Are used to perform powerful pattern-matching and "search-and-replace" functions on text. 40 Regular Expression

Syntax: var patt=new RegExp(pattern,modifiers); var patt=/pattern/modifiers; – Pattern: specifies the pattern of an expression – Modifiers: specify if a search should be global (g), case- insensitive (i), etc. E.g., – var patt1=new RegExp(“is”, g); – var patt1=/is/g;

42 Regular Expression - Methods RegularExpression.test(string): -Searches a string for a specified value, and returns true or false, depending on the result. var str="Is this all there is?"; var patt=new RegExp("E", "i"); alert("pattern ‘E' is in 'str': " + patt.test(str)); output - pattern ‘E' is in 'str': true var str="Is this all there is?"; var patt=new RegExp("E"); alert("pattern ‘E' is in 'str': " + patt.test(str)); output - pattern ‘E' is in 'str': false

43 Regular Expression - Methods RegularExpression.exec(string): searches a string for a specified value, – returns a result array or null. var str="Is this all there is?"; var patt=new RegExp("is", "i"); alert("pattern ‘is' is in 'str': " + patt.exec(str)); output - pattern ‘is' is in 'str': Is

44 Regular Expression - Methods String.match(pattern): - returns the matching patterns, - if no matching, return null. - without the modifier “g”, returns same as exec(); var str="Is this all there is?"; var patt=/is/g; alert(str.match(patt));  output - is,is var str="Is this all there is?"; var patt1=/is/gi; alert(str.match(patt1));  output – Is, is,is

Regular Expression - Methods String.search(RegularExpression): – search the RegularExpression in the specified string. – Return the index of the first match of the RegularExpression in the string, – Return -1, if no match – E.g., var re = /or/; var s = "Good morning"; alert(s.search(re)); // 6 45

Regular Expression – Special Characters *: matches the preceding character 0 or more times,  /ab*c/ matches “abbbbc” and “ac”, but not “ad” +: matches the preceding character 1 or more times  /ab+c/ matches “abbbc” and “abc”, but not “ac” ?: matches the preceding character 0 or 1 time.  /ab?c/ matches “abc” and “ac” ^: Matches beginning of input $: Matches end of input {n}: Matches exactly n occurrences of the preceding character. N must be a positive integer. More on MDN (Mozilla Developer Network): US/docs/Web/JavaScript/Guide/Regular_Expressions US/docs/Web/JavaScript/Guide/Regular_Expressions 46

Regular Expression - Brackets Brackets are used to find a range of characters

Example var name = prompt("Enter a name"); var nameRegEx = /^[A-Za-z][0-9 A-Za-z \'\-]*$/; if(nameRegEx.test(name)==true) alert("Yes"); else alert("No"); 48

Example var phone = prompt("Enter Phone:"); var phoneRegExp = /^\([0-9]{3}\)[0-9]{3}\-[0-9]{4}$/; if(phoneRegExp.test(phone)==true) {alert("Yes");} else alert("No"); 49

Next Class HTML 50

Thank you!