Produced by. App Development & Modelling. BSc in Applied Computing. Eamonn de Leastar

Similar documents
Javascript Arrays, Object & Functions

Background. Javascript is not related to Java in anyway other than trying to get some free publicity

JavaScript CS 4640 Programming Languages for Web Applications

JavaScript CS 4640 Programming Languages for Web Applications

Web Programming and Design. MPT Junior Cycle Tutor: Tamara Demonstrators: Aaron, Marion, Hugh

CSC Web Programming. Introduction to JavaScript

Produced by. App Development & Modelling. Eamonn de Leastar

BEFORE CLASS. If you haven t already installed the Firebug extension for Firefox, download it now from

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Client-Side Web Technologies. JavaScript Part I

Web Application Development

JavaScript Functions, Objects and Array

Produced by. Design Patterns. MSc Computer Science. Eamonn de Leastar

COMP284 Scripting Languages Lecture 15: JavaScript (Part 2) Handouts

Introduction to Web Tech and Programming

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Produced by. App Development & Modeling. BSc in Applied Computing. Eamonn de Leastar

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

EXERCISE: Introduction to client side JavaScript

COMS 469: Interactive Media II

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

COMP284 Scripting Languages Lecture 3: Perl (Part 2) Handouts

The course is supplemented by numerous hands-on labs that help attendees reinforce their theoretical knowledge of the learned material.

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Produced by. Web Development. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

Produced by. Mobile Application Development. Eamonn de Leastar

COMP519 Web Programming Lecture 12: JavaScript (Part 3) Handouts

Introduction to JavaScript p. 1 JavaScript Myths p. 2 Versions of JavaScript p. 2 Client-Side JavaScript p. 3 JavaScript in Other Contexts p.

Basics of JavaScript. Last Week. Needs for Programming Capability. Browser as Development Platform. Using Client-side JavaScript. Origin of JavaScript

JavaScript: the language of browser interactions. Claudia Hauff TI1506: Web and Database Technology

Java Classes. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

CS 130(0) JavaScript Lab

UNIT -II. Language-History and Versions Introduction JavaScript in Perspective-

Produced by. Agile Software Development. Eamonn de Leastar

Arrays, Strings and Collections

Session 3: JavaScript - Structured Programming

PHP. Interactive Web Systems

Lecture 3: The Basics of JavaScript. Background. Needs for Programming Capability. Origin of JavaScript. Using Client-side JavaScript

Design Patterns. Produced by. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

Java Control Statements

Javascript. Many examples from Kyle Simpson: Scope and Closures

Algorithms. Produced by. Eamonn de Leastar

Chapter 3 Data Types and Variables

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

JavaScript. IT Engineering I Instructor: Ali B. Hashemi

JavaScript. Why JavaScript? Dynamic Form Validation. Online calculator. IT Engineering I Instructor: Ali B. Hashemi. Interaction

BSc Applied Computing / Forensics / Entertainment Systems/ IOT. Eamonn de Leastar Dr. Brenda Mullally

Virto Office 365 Bulk File Upload for Microsoft SharePoint Online Release User and Installation Guide

This course is designed for web developers that want to learn HTML5, CSS3, JavaScript and jquery.

CISC 1600 Lecture 2.4 Introduction to JavaScript

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

PA3 Design Specification

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

JavaScript: Sort of a Big Deal,

IN Development in Platform Ecosystems Lecture 2: HTML, CSS, JavaScript

Produced by. Web Development. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

JavaScript Lecture 1

Computer Science AP 2017 Summer Assignment Mrs. McFarland

Produced by. Agile Software Development. Eamonn de Leastar

JavaScript: Introduction, Types

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

COMS 469: Interactive Media II

Produced by. Web Development. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

Node.js Training JavaScript. Richard richardrodger.com

CSIS 10B Lab 2 Bags and Stacks

Such JavaScript Very Wow

Java Overview An introduction to the Java Programming Language

Course 2320 Supplementary Materials. Modern JavaScript Best Practices

Produced by. Mobile Application Development. Eamonn de Leastar

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

CS1046 Lab 4. Timing: This lab should take you 85 to 130 minutes. Objectives: By the end of this lab you should be able to:

CGS 3066: Spring 2015 JavaScript Reference

The JavaScript Language

Princess Nourah bint Abdulrahman University. Computer Sciences Department

Midterm Exam. 5. What is the character - (minus) used for in JavaScript? Give as many answers as you can.

Pathologically Eclectic Rubbish Lister

Produced by. Agile Software Development. Eamonn de Leastar

CSCE 120: Learning To Code

c122mar413.notebook March 06, 2013

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

CS1520 Recitation Week 2

CSCE 120: Learning To Code

Bruce Merry. IOI Training Dec 2013

Ruby Objects & Common Methods

Software. Programming Languages. Types of Software. Types of Languages. Types of Programming. Software does something

ArcGIS API for JavaScript: Using Arcade with your Apps. Kristian Ekenes & David Bayer

Project 3 CIS 408 Internet Computing

Javascript Methods. concat Method (Array) concat Method (String) charat Method (String)

CMPT 100 : INTRODUCTION TO

Exercise 1: Basic HTML and JavaScript

Data Types and the main Function

Programming language components

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

A Brief Introduction to Scheme (II)

Transcription:

App Development & Modelling BSc in Applied Computing Produced by Eamonn de Leastar (edeleastar@wit.ie) Department of Computing, Maths & Physics Waterford Institute of Technology http://www.wit.ie http://elearning.wit.ie

Arrays

Creating & Using Arrays Arrays are a type of object that are ordered by the index of each item it contains. The index starts at zero and extends to however many items have been added, which is a property of the array known as the "length" of the array. Similar to objects, an array can be created with the array constructor or the shorthand syntax known as array literal. 3

Creating Arrays... Array Constructor var foo = new Array; Array Literal var bar = []; 4

Creating Arrays with Dimensions Array Constructor var foo = new Array(100); Array Literal var bar = []; 5

Using Arrays... Insertion into arrays can be via: [] notation (like Java) Using push which appends to the end of the array var foo = []; foo.push("a"); foo.push("b"); alert( foo[ 0 ] ); // => a alert( foo[ 1 ] ); // => b alert( foo.length ); // => 2 foo.pop(); alert( foo[ 0 ] ); // => a alert( foo[ 1 ] ); // => undefined alert( foo.length ); // => 1 6

Definition of an Array Arrays are zero-indexed, ordered lists of values. They are a handy way to store a set of related items of the same type (such as strings), though in reality, an array can include multiple types of items, including other arrays. To create an array, either use the object constructor or the literal declaration, by assigning the variable a list of values after the declaration // A simple array with constructor var myarray1 = new Array( "hello", "world" ); // literal declaration, the preferred way var myarray2 = [ "hello", "world" ]; The literal declaration is generally preferred. See the Google Coding Guidelines for more information. 7

Push If the values are unknown, it is also possible to declare an empty Array, and add elements either through functions or through accessing by index: 'push' is a function that adds an element on the end of the array and expands the array respectively. You also can directly add items by index. Missing indices will be filled with 'undefined'. // Creating empty arrays and adding values var myarray = []; // adds "hello" on index 0 myarray.push("hello"); // adds "world" on index 1 myarray.push("world"); // adds "!" on index 2 myarray[ 2 ] = "!"; 8

Missing Indices Missing indices will be filled with 'undefined'. If the size of the array is unknown, 'push' is far more safe. You can both access and assign values to array items with the index // Leaving indices var myarray = []; myarray[ 0 ] = "hello"; myarray[ 1 ] = "world"; myarray[ 3 ] = "!"; console.log( myarray ); // [ "hello", "world", undefined, "!" ]; // Accessing array items by index var myarray = [ "hello", "world", "!" ]; console.log( myarray[2] ); // "!" 9

Array Methods and Properties 10

length The.length property is used to determine the amount of items in an array. You will need the.length property for looping through an array: // Length of an array var myarray = [ "hello", "world", "!" ]; console.log( myarray.length ); // 3 // For loops and arrays - a classic var myarray = [ "hello", "world", "!" ]; for ( var i = 0; i < myarray.length; i = i + 1 ) { console.log( myarray[i] ); } Except when using for/in loops: // For loops and arrays - alternate method var myarray = [ "hello", "world", "!" ]; for ( var i in myarray ) { console.log( myarray[ i ] ); } 11

concat Concatenate two or more arrays with.concat: // Concatenating Arrays var myarray = [ 2, 3, 4 ]; var myotherarray = [ 5, 6, 7 ]; // [ 2, 3, 4, 5, 6, 7 ] var wholearray = myarray.concat( myotherarray ); 12

join.join creates a string representation of the array. Its parameter is a string that works as a separator between elements (default separator is a comma): // Joining elements var myarray = [ "hello", "world", "!" ]; console.log( myarray.join(" ") ); // "hello world!"; console.log( myarray.join() ); // "hello,world,!" console.log( myarray.join("") ); // "helloworld!" console.log( myarray.join("!!") ); // "hello!!world!!!"; 13

pop.pop removes the last element of an array. It is the opposite method of.push: // pushing and popping var myarray = []; myarray.push( 0 ); // [ 0 ] myarray.push( 2 ); // [ 0, 2 ] myarray.push( 7 ); // [ 0, 2, 7 ] myarray.pop(); // [ 0, 2 ] 14

reverse The elements of the array are in reverse order after calling this method: // reverse var myarray = [ "world", "hello" ]; // [ "hello", "world" ] myarray.reverse(); 15

shift Removes the first element of an array. With.pop and.shift, you can simulate a queue: // queue with shift() and pop() var myarray = []; myarray.push( 0 ); // [ 0 ] myarray.push( 2 ); // [ 0, 2 ] myarray.push( 7 ); // [ 0, 2, 7 ] myarray.shift(); // [ 2, 7 ] 16

foreach In modern browsers it is possible to traverse through arrays with a.foreach method, where you pass a function that is called for each element in the array. The function takes up to three arguments: Element - The element itself. Index - The index of this element in the array. // native foreach function printelement( elem ) { console.log( elem ); } myarray = [ 1, 2, 3, 4, 5 ]; // prints all elements to the console myarray.foreach( printelement ); Array - The array itself. All of these are optional, but you will need at least the 'element' parameter in most cases 17

Element & Index Parameters function printelementandindex( elem, index ) { console.log( "Index " + index + ": " + elem ); } // prints "Index 0: 1" "Index 1: 2" "Index 2: 3"... myarray.foreach( printelementandindex ); 18

Exercise 5.4 In your current project (called js-lab-2) create a file called 'arrays.js' in the js folder. Create a HTML file to load this script. In arrays.js do the following: Declare an array called 'todolist'. Initialise this array to contain 5 strings, which describe some simple tasks (make up some). write a for loop then logs each of these tasks to the console Use google chrome developers tools -> Console to verify that it works as expected. Implement one more experiment: pass the entire array to a single call to console.log Observe the result. 19

Exercise 5.5 Take the last code fragment - foreach above, and incorporate into your arrays.js file. In Chrome, load the html page and single step through each of the methods. 20

Except where otherwise noted, this content is licensed under a Creative Commons Attribution-NonCommercial 3.0 License. For more information, please see http:// creativecommons.org/licenses/by-nc/3.0/