Presentation is loading. Please wait.

Presentation is loading. Please wait.

10 – Java Script (3) Informatics Department Parahyangan Catholic University.

Similar presentations


Presentation on theme: "10 – Java Script (3) Informatics Department Parahyangan Catholic University."— Presentation transcript:

1 10 – Java Script (3) Informatics Department Parahyangan Catholic University

2  Shifting Elements  works like popping an element, but instead of removing the last element, shift() method removes the first element and shifts all other elements forward.  works like dequeue in Queue data structure  Example: var fruits = ["Banana", "Orange", "Apple", "Mango"]; var removed = fruits.shift(); //fruits = ["Orange", "Apple", "Mango"] //removed = “Banana”

3  Unshifting Elements  The unshift() method adds a new element at the beginning of an array and shifts the old elements backward  Example: var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.unshift("Lemon"); //fruits = ["Lemon", "Banana", "Orange", "Apple", "Mango"]

4  Deleting Elements  Changes an element to undefined  This leaves a “hole” in an array  Example: var fruits = ["Banana", "Orange", "Apple", "Mango"]; delete fruits[1]; //fruits = ["Banana", undefined, "Apple", "Mango"]

5  Splicing an Array  The splice() method can be used to add new items or remove some items to/from an array  Example: var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.splice(2, 1, "Lemon", "Kiwi"); //fruits = ["Banana", "Orange", "Lemon", "Kiwi", "Mango"] where to splice how many elements should be removed new elements to be inserted

6  Slicing an Array  The slice() method slices out a piece of an array into a new array  Example: var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; var citrus = fruits.slice(1, 3); //citrus = ["Orange", "Lemon“] starting indexending index (not included in the result)

7  Searching in an Array  The indexOf() method searches the array for an element and return its position (first to be found)  The lastIndexOf() method searches the array for and element, starting at the end, and returns its position  Example: var foo = ["H", "E", "L", "L", "O"]; var firstL = foo.indexOf("L");//2 var lastL = foo.lastIndexOf("L");//3

8  A regular expression is a sequence of characters that forms a search pattern.  The search pattern can be used for text search and text replace operations.

9  Syntax: /pattern/modifiers;  Example:  w3school is a pattern (to be used in search)  i is a modifier (modifies the search to be case-insensitive) var patt = /w3schools/i;

10  In Java Script, RE usually used for search() and replace() string methods  Example: var str = "Visit W3Schools"; var n = str.search(/w3schools/i); //n = 6 var str = "Visit W3Schools!"; var n = str.search("W3Schools"); //n = 6 var str = "Visit W3Schools!"; var n = str.search(“w3schools"); //n = -1

11  Example: var str = "Visit Microsoft!"; var res = str.replace(/microsoft/i, "W3Schools"); //res = "Visit W3Schools!" var str = "Visit Microsoft!"; var res = str.replace("Microsoft", "W3Schools"); //res = "Visit W3Schools!" var str = "Visit Microsoft!"; var res = str.replace(”microsoft", "W3Schools"); //res = "Visit Microsoft!"

12  Modifiers  i perform case-insensitive matching  g perform a global match (find all the matches rather than stopping after the first match)  m perform multiline matching (find first occurrences in every line)

13  Patterns: Brackets are used to find a range of characters  [abc] find any characters which is a, b or c  [^abc] find any characters which is not a, not b, and not c  [a-z] find any characters between a and z (inclusive)  [^a-z] find any characters which is not a, b,..., z (inclusive)

14  Quantifiers:  n+ matches any string that contains at least one n  n* matches any string that contains zero or more occurrences of n  n? matches any string that contains zero or one occurrences of n

15  The test() method is a RE method. It searches a string for a pattern, and returns true or false, depending on the result.  Example: var patt = /free/; patt.test("The best things in life are free!"); //returns true since the string has “free” in it var patt = /frees/; patt.test("The best things in life are free!"); //returns false since the string doesn’t has “frees” in it

16  Example: var patt = /frees*/; patt.test("The best things in life are free!"); //returns true since the string has “free” in it. Note that s* means zero s is permitable var patt = /frees+/; patt.test("The best things in life are free!"); //returns false since the string has “frees” in it var patt = /fre?!/; patt.test("The best things in life are free!"); //returns false since e?! means zero or one e then followed by a “!”. In this string there is 2 “e”s


Download ppt "10 – Java Script (3) Informatics Department Parahyangan Catholic University."

Similar presentations


Ads by Google