Jerry Cain and Eric Roberts

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Collection Classes Eric Roberts CS 106B January 16, 2013 (Part 2: Maps, Sets, and Lexicons)
CS 898N – Advanced World Wide Web Technologies Lecture 14: JavaScript Chin-Chih Chang
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
Introduction to JavaScript for Python Programmers
WEB DESIGN AND PROGRAMMING Introduction to Javascript.
Functions in C++ Eric Roberts CS 106B January 9, 2013.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Midterm 2 Review. What does it mean to “declare a variable” in JavaScript? Write code to declare a variable with a name of your own choosing, in Javascript.
Eric Roberts and Jerry Cain
Web Database Programming Using PHP
Multidimensional Arrays
Mechanics of Functions
Collision Theory and Logic
Jerry Cain and Eric Roberts
CST 1101 Problem Solving Using Computers
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
Chapter 6 JavaScript: Introduction to Scripting
CS1022 Computer Programming & Principles
Learning to Program D is for Digital.
Collision Theory and Logic
Web Design II Flat Rock Community Schools
Eric Roberts and Jerry Cain
Web Database Programming Using PHP
(Part 2: Maps, Sets, and Lexicons)
Department of Computer Science,
Barb Ericson Georgia Institute of Technology May 2006
Functions in C++ Eric Roberts CS 106B January 7, 2015.
Eric Roberts and Jerry Cain
Scope, Objects, Strings, Numbers
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
JavaScript.
Chapter 7 - JavaScript: Introduction to Scripting
Introduction to the C Language
Objectives Create an array Work with array properties and methods
Jerry Cain and Eric Roberts
Your 1st Programming Assignment
Mechanics of Functions
Introduction to pseudocode
JavaScript: Introduction to Scripting
WEB PROGRAMMING JavaScript.
CS190/295 Programming in Python for Life Sciences: Lecture 6
INFO/CSE 100, Spring 2005 Fluency in Information Technology
slides created by Marty Stepp
T. Jumana Abu Shmais – AOU - Riyadh
Coding Concepts (Basics)
Web DB Programming: PHP
slides courtesy of Eric Roberts
slides courtesy of Eric Roberts
Multidimensional Arrays
slides courtesy of Eric Roberts
Let’s all Repeat Together
slides courtesy of Eric Roberts
Loops and Arrays in JavaScript
JavaScript CS 4640 Programming Languages for Web Applications
Tutorial 10: Programming with javascript
Using Script Files and Managing Data
JavaScript: Introduction to Scripting
Chapter 7 - JavaScript: Introduction to Scripting
Chapter 7 - JavaScript: Introduction to Scripting
Javascript Chapter 19 and 20 5/3/2019.
Chapter 17 JavaScript Arrays
Introduction to JavaScript
[Based in part on SWE 432 and SWE 632 materials by Jeff Offutt, GMU]
JavaScript CS 4640 Programming Languages for Web Applications
Chapter 7 - JavaScript: Introduction to Scripting
Presentation transcript:

Jerry Cain and Eric Roberts Objects as Maps Jerry Cain and Eric Roberts CS 106J May 22, 2017

The Concept of a Map One of the most important applications of JavaScript objects uses them to associate pairs of data values. In computer science, the resulting data structure is called a map. Maps associate a simple data value called a key (most often a string) with a value, which is usually larger and more complex. Examples of maps exist everywhere in the real world. A classic example is a dictionary. The keys are the words, and the values are the corresponding definitions. A more contemporary example is the World-Wide Web. In this example, the keys are the URLs, and the values are the contents of the corresponding pages.

Exercise: Maps in the Real World Talk with your tablemates and come up with examples from the real world that fit the map paradigm.

Maps and JavaScript Objects In the context of CS 106J, the most obvious example of a map is the JavaScript object, which implements precisely the map concept. The keys are strings, and the values are arbitrary JavaScript values. map[key] If the key is defined in the map, this selection returns the value. If no definition has been supplied, the selection returns the constant undefined. When you use an object as a map, you supply the key as a string expression using the square-bracket notation, as in map[key] = value; Map selections are assignable, so that you can set the value associated with a key by executing an assignment statement:

Using Maps in an Application Before going on to create new applications of maps, it seems worth going through the example from the text, which uses a map to associate three-letter airport codes with their locations. The association list is stored in a text file that looks like this: ATL=Atlanta, GA, USA ORD=Chicago, IL, USA LHR=London, England, United Kingdom HND=Tokyo, Japan LAX=Los Angeles, CA, USA CDG=Paris, France DFW=Dallas/Ft Worth, TX, USA FRA=Frankfurt, Germany . The AirportsCodes.js program shows how to read this file into a JavaScript object.

Symbol Tables Programming languages make internal use of maps in several contexts, of which one of the easiest to recognize is a symbol table, which keeps track of the correspondence between variable names and their values. The SymbolTable.js application included in the example programs for this lecture implements a simple test of a symbol table that reads lines from the console, each of which is one of the following commands: A simple assignment statement of the form var = number. A variable alone on a line, which displays the variable’s value. The command list, which lists all the variables. The command quit, which exits from the program. The sample run on the next slide illustrates the operation of the SymbolTable.js program.

Sample Run of SymbolTable.js JavaScript Console pi = 3.14159 > e = 2.71828 > x = 2 > pi > 3.14159 > x 2 list > e = 2.71828 pi = 3.14159 x = 2 x = 42 > a = 1.5 > list > a = 1.5 e = 2.71828 pi = 3.14159 x = 42 quit >

Reading from the Console Implementing the SymbolTable application requires two new features that you have not yet seen: Reading a line from the console. 1. Iterating through all the keys in the map to implement list. 2. Reading a line from the console is more difficult in JavaScript than it is in most languages, simply because JavaScript depends on an interaction model based on events and callback functions. The best strategy for reading a line of user input uses the console.requestInput method in the following form: console.requestInput(prompt, callback); If you need to read lines in a loop, the easiest way is to include another requestInput call in the callback function.

Iterating Through Keys in an Object One of the common operations that clients need to perform when using a map is to iterate through the keys. JavaScript supports this operation using an extended form of the for statement, which has the following form: for (var key in map) { var value = map[key]; . . . code to work with the individual key and value . . . } In JavaScript, this extended form of the for loop can process the keys in any order.

The FindStateCodes.js Program function FindStateCodes() { var map = readMap("StateCodes.txt"); if (map === null) { console.log("The StateCodes.txt file is missing."); } else { var callback = function(code) { if (code !== "") { var state = map[code]; if (state === undefined) { console.log(code + " is not a legal state code."); console.log(state); } console.requestInput("Enter code: ", callback); };

The readMap Function /* * Reads the specified file and returns a map between keys and * values. Entries appear as key=value lines in the data file. */ function readMap(filename) { var lines = File.readLines(filename); if (lines === undefined) return null; var map = { }; var line = lines.shift(); while (line !== undefined) { var equals = line.indexOf("="); if (equals === -1) { console.log("Missing = in " + line); return null; } var key = line.substring(0, equals).trim(); var value = line.substring(equals + 1).trim(); map[key] = value; line = lines.shift(); return map; function FindStateCodes() { var map = readMap("StateCodes.txt"); if (map === null) { console.log("The StateCodes.txt file is missing."); } else { var callback = function(code) { if (code !== "") { var state = map[code]; if (state === undefined) { console.log(code + " is not a legal state code."); console.log(state); } console.requestInput("Enter code: ", callback); };

Maps Can Be Fast Fast If you think about the underlying implementation of maps, your first thought is likely to be that JavaScript looks through a list of keys to find the key in question and returns the corresponding value. That approach takes time proportional to the number of keys. Maps, however, can be implemented much more efficiently than that. As you will learn if you go on to CS 106B, you will learn that maps can be implemented so that the result is delivered almost instantly or, more accurately, so that the time required is constant no matter how many keys there are. To show that this result might in fact be possible, consider the state code example. If the state names are stored in an 26x26 array in which the indices correspond to the first and second letters in the code, finding the name is simply an array access.

The End