Download presentation
Presentation is loading. Please wait.
Published byPreston Tate Modified over 8 years ago
1
Functions and Arrays
2
Predefined Functions eval(condition) –Evaluates (executes) JavaScript syntax –Eval returns an undefined value parseInt(string) and parseFloat(string) –Converts a character string to a number –String must be a valid number or NaN is returned
3
Predefined Functions escape(string) –Returns the ascii code of the string unescape(string) –String must be ascii code –Returns the character version
4
Arrays Data Structures
5
Defining Arrays myArray = new Array (“red”, “green”, “blue”, “yellow”); myArray = [“red”, “green”, “blue”, “yellow”] element value: redgreen blue yellow element index: 0 1 23
6
array.length Length is a property of an array element value: redgreen blue yellow element index: 0 1 23 length is?
7
Array functions reverse () arr = [1, 2, 3, 4] arr.reverse(); arr = [4, 3, 2, 1] sort() arr = [13, -4, 0, 9] arr.sort(); arr = [-4, 0, 9, 13]
8
Array Functions concat() a=[1, 2, 3, 4] b=[5, 6, 7] a.concat(b) = [1, 2, 3, 4, 5, 6, 7] b.concat(a) = [5, 6, 7, 1, 2, 3, 4]
9
Array Functions slice() a=[1, 2, 3, 4, 5, 6] a.slice(0, 4) = [1, 2, 3, 4] a.slice(4) = [5, 6] a.slice(2, -1) = [3, 4, 5]
10
Write a JavaScript program that loads an array with the province codes: PQ, BC, AB, ON. The program should sort the province codes and write the out in alphabetical order. Then the program should write the provinces in reverse order.
11
Write a JavaScript program that loads an array with the string, “Functions”. The program should prompt for the start and the end of a slice function and then print the results
12
Load one array with ‘Fun’. Load another array with ‘ctions’. Try concatenating them together.
13
Write a program that prompts for character strings and stores them in an array. Print the contents of of the array and the length.
14
Arrays - Join Method var product= new Array(5) product[0]="books"; product[1]="cars"; product[2]="dishes"; document.write(product.join() + “ ); books,cars,dishes document.write(product.join(" + ") + " ”); books + cars + dishes document.write(product.join(" ") + " ”); books cars dishes
15
Strings are not arrays
16
But strings can have lengths! string1 = “strings are easy” document.write(“The length of string 1 is “ + string1.length); What is the length?
17
charAt() method Finds the character at a specific index string1 = “strings are easy”; document.write(“the character at index 0 is “ + string1.charAt(0) + “ ”); What will be written? How about? document.write(“at index 4 “ + string1.charAt(4));
18
charCodeAt() method Returns the unicode value of the character at the given index. The first 128 unicodes are the same as the ASCII table string1 = “strings are easy”; string1.charCodeAt(2) will return 114
19
indexOf() - method Searches a string for an occurrence of a character string and returns the index string1 = “strings are easy”; x = string1.indexOf(“s”); x will be 0 x = string1.indexOf(“ar”); X will be 8 How would you find the index of the second s?
20
lastIndexOf() - method Returns the index of the last occurrence of a set of characters in a characters string. string1 = “strings are easy”; x = string1.lastIndexOf(“s”); x will be 14
21
split() - method Creates array elements from a character string. The method cuts up the string based on a character. string1 = “strings are easy”; var arr = string1.split(“ “); arr[0] will be “strings” arr[1] will be “are” arr[2] will be “easy” What will we get if split on the character “s”?
22
substr() - method string1 = “strings are easy”; start = 1; length = 4; newString = string1.substr(start, length); newString will contain “trin”
23
substring() - method string1 = “strings are easy”; start = 1; stop = 4; newString = string1.substring(start, stop); newString will contain “tri” (doesn’t return the character at the stop index)
24
toLowerCase() - method string1 = “Strings Are Easy”; string2 = string1.toLowerCase(); string2 will have “strings are easy” in it
25
toUpperCase() - method string1 = “Strings Are Easy”; string2 = string1.toUpperCase(); string2 will have “STRINGS ARE EASY” in it.
26
Write a program that prompts for a name (first name and last name). The program should separate the first name from the last name and display the first name in lower case and the second name in upper case.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.