Functions and Arrays. Predefined Functions eval(condition) –Evaluates (executes) JavaScript syntax –Eval returns an undefined value parseInt(string) and.

Slides:



Advertisements
Similar presentations
JavaScript Chapter 7 Working with Strings. String Operations (Chap 2) var colors = red blue; var colors = red blue; var sizes = small large; var sizes.
Advertisements

Intro to Javascript CS Client Side Scripting CS380 2.
Java
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Strings in Java 1. strings in java are handled by two classes String &
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
1 Working with String Duo Wei CS110A_ Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
Java Strings in 10 minutes
Session 8 JavaScript/Jscript: Objects Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Arrays, Slices, and more. Structs  A struct is a collection of fields  The keyword type is used to declare the type of the struct  Syntax: type Vertex.
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()
String class  Construct a string  String str = new String(“welcome”);  Char[] charr = {‘G’, ‘o’, ‘o’, ‘d’};  String mes = new String(charr);  A full.
25-Jun-15 JavaScript Language Fundamentals II. 2 Exception handling, I Exception handling in JavaScript is almost the same as in Java throw expression.
AdvWeb-1/14 DePaul University SNL 262 Advanced Web Page Design Chapt 6 - Strings & Arrays Instructor: David A. Lash.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 12 - JavaScript: Objects Outline 12.1 Introduction 12.2 Thinking About Objects 12.3 Math Object.
Chapter 5 new The Do…Loop Statement
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.
Scripting Languages.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
Native JavaScript in Hyperion Intelligence Unleashing the Power of Object- Oriented Programming.
1 JavaScript: Objects and Object Models October 25, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel,
String Object.  Conundrum: I would like people to have more time on the revisions…  … but I don't have a lot of time myself for more grading  A1 Revision.
JavaScript Functions & Objects. JavaScript Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded URI.
CMPS 211 JavaScript Topic 2 Functions and Arrays.
Using Client-Side Scripts to Enhance Web Applications 1.
A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University ©2011 Pearson Prentice Hall ISBN Chapter 15 JavaScript.
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.
4.4 JavaScript (JS) Deitel Ch. 7, 8, 9, JavaScript & Java: Similarities JS (JavaScript) is case-sensitive Operators –arithmetic: unary +, unary.
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Representing Characters in a computer Pressing a key on the computer a code is generated that the computer can convert into a symbol for displaying or.
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
Internet & World Wide Web How to Program, 5/e. 2.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
JavaScript. JavaScript is the programming language of HTML and the Web. Easy to learn One of the 3 languages of all developers MUST learn: 1. HTML to.
Unit 12 JavaScript Arrays Instructor: Brent Presley.
CSC Programming I Lecture 9 September 11, 2002.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming strings.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Sahar Mosleh California State University San MarcosPage 1 Object Base Programming.
© 2006 Lawrenceville Press Slide 1 Chapter 6 The Post-Test Do…Loop Statement  Loop structure that executes a set of statements as long as a condition.
JavaScript: Objects 1 © by Pearson Education, Inc. All Rights Reserved.
JavaScript: Objects 1 © by Pearson Education, Inc. All Rights Reserved.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
ECA 225 Applied Interactive Programming ECA 225 Applied Online Programming javascript arrays.
Declaring variables The type could be: int double char String name is anything you want like lowerCaseWord.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
JavaScript and Ajax (JavaScript Arrays) Week 5 Web site:
Strings Robin Burke IT 130. Outline Objects Strings methods properties Basic methods Form validation.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Chapter 5 Murach's JavaScript and jQuery, C1© 2012, Mike Murach & Associates, Inc.Slide 1.
Basic Objects. Math Object  Math.cos( x ), x in radians  Math.sqrt ( x )  Math.pow ( x, y )  Math.ceil( x )  etc.
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
Java Script: Objects (Chapter 12 in [2]). 2 Outline Introduction Introduction Thinking About Objects Thinking About Objects Math Object Math Object String.
Strings, Characters and Regular Expressions
The relational operators
ARRAYS MIT-AITI Copyright 2001.
Perl Variables: Array Web Programming.
Tonga Institute of Higher Education
JavaScript: Objects.
String Processing 1 MIS 3406 Department of MIS Fox School of Business
String Processing 1 MIS 3406 Department of MIS Fox School of Business
Exam Prep.
Switch, Strings, and ArrayLists in Java
What We Want To Do User enters: Mary Smith
Presentation transcript:

Functions and Arrays

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

Predefined Functions escape(string) –Returns the ascii code of the string unescape(string) –String must be ascii code –Returns the character version

Arrays Data Structures

Defining Arrays myArray = new Array (“red”, “green”, “blue”, “yellow”); myArray = [“red”, “green”, “blue”, “yellow”] element value: redgreen blue yellow element index:

array.length Length is a property of an array element value: redgreen blue yellow element index: length is?

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]

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]

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]

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.

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

Load one array with ‘Fun’. Load another array with ‘ctions’. Try concatenating them together.

Write a program that prompts for character strings and stores them in an array. Print the contents of of the array and the length.

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

Strings are not arrays

But strings can have lengths! string1 = “strings are easy” document.write(“The length of string 1 is “ + string1.length); What is the length?

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));

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

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?

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

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”?

substr() - method string1 = “strings are easy”; start = 1; length = 4; newString = string1.substr(start, length); newString will contain “trin”

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)

toLowerCase() - method string1 = “Strings Are Easy”; string2 = string1.toLowerCase(); string2 will have “strings are easy” in it

toUpperCase() - method string1 = “Strings Are Easy”; string2 = string1.toUpperCase(); string2 will have “STRINGS ARE EASY” in it.

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.