29 November JavaScript: Arrays and Iterations Functions.

Slides:



Advertisements
Similar presentations
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Advertisements

The Web Warrior Guide to Web Design Technologies
JavaScript Forms Form Validation Cookies. What JavaScript can do  Control document appearance and content  Control the browser  Interact with user.
Video, audio, embed, iframe, HTML Form
1 November Building an Interactive Web Page. Presentations Casey: Current event A J: Education Cherrelle: Advertising.
Chapter 7 © 2001 by Addison Wesley Longman, Inc. 1 Chapter 7 Sebesta: Programming the World Wide Web.
1 The Information School of the University of Washington Oct 30fit review Programming Review INFO/CSE 100, Fall 2006 Fluency in Information Technology.
JavaScript Forms Form Validation Cookies CGI Programs.
25 October Conditionals and Loops. Presentations Brendan: Cyberwarfare Casey: Online shopping (No current event today)
Forms Review. 2 Using Forms tag  Contains the form elements on a web page  Container tag tag  Configures a variety of form elements including text.
3 November Building an Interactive Web Page: HTML Forms.
10 November JavaScript. Presentation Hints What do YOU think makes a good presentation Some of my suggestions Don’t write full sentences on slides Talk,
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
8 November Forms and JavaScript. Types of Inputs Radio Buttons (select one of a list) Checkbox (select as many as wanted) Text inputs (user types text)
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Computer Science 103 Chapter 4 Advanced JavaScript.
Oct 25 1 The Information School of the University of Washington fit control © 2006 University of Washington Control Flow INFO/CSE 100, Fall 2006.
Forms, Validation Week 7 INFM 603. Announcements Try placing today’s example in htdocs (XAMPP). This will allow you to execute examples that rely on PHP.
Lesson 8 Creating Forms with JavaScript
CST JavaScript Validating Form Data with JavaScript.
Programming Games Computer science big ideas. Computer Science jargon. Show virtual dog Homework: [Catch up: dice game, credit card or other form.] Plan.
JavaScript Form Validation
JS: DOM Form Form Object Form Object –The Form object represents an HTML form. –For each instance of a tag in an HTML document, a Form object is created.
The Bean Counter: A JavaScript Program
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
CSC 2720 Building Web Applications HTML Forms. Introduction  HTML forms are used to collect user input.  The collected input is typically sent to a.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
Using Client-Side Scripts to Enhance Web Applications 1.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
1 Forms and Form elements CSD 340 McCoey. 2 Form Object Properties action Usually a call to a server elements encoding method post or get target Methods.
CompSci Today’s topics Java Review Just a bunch of headings meant to trigger questions A contraction of previous notes Upcoming Midterm Exam Reading.
JavaScript - Basic Concepts Prepared and Presented by Hienvinh Nguyen, Afshin Tiraie.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Iteration Chapter Iteration = Repetition 3 Looping Via The for Loop for loop: A block of code that executes a group of statements repeatedly until.
Creating Web Documents catch-up JavaScript slide show tools redirection.
JavaScript, Fourth Edition
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
Lesson 16. Practical Application 1 We can take advantage of JavaScript and the DOM, to set up a form so that the first text box of a form automatically.
Event Handling (the right way). A Simple Web Page Events - Summary The web page looks like this:
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Tutorial 11 1 JavaScript Operators and Expressions.
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
Conditions and More Web Form Fields. Conditions if (then) else Syntax if( ) Boolean expression { { } } instructions if expression is true (then) instructions.
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
HTML Structure II (Form) WEEK 2.2. Contents Table Form.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
Client-side (JavaScript) Validation. Associating a function with a click event – Part 1 Use the input tag’s onclick attribute to associate a function.
HTML Again GUI Items Originally HTML 4 Copyright © Curt Hill.
HTML & teh internets.
JavaScript.
JavaScript functions.
Programming the Web using XHTML and JavaScript
Web Programming– UFCFB Lecture 17
More Selections BIS1523 – Lecture 9.
Conditions and Ifs BIS1523 – Lecture 8.
Event Driven Programming & User Defined Functions
INFO/CSE 100, Spring 2006 Fluency in Information Technology
T. Jumana Abu Shmais – AOU - Riyadh
Tutorial 10: Programming with javascript
Introduction to Programming and JavaScript
JavaScript functions.
CIS 136 Building Mobile Apps
Programming Basics Review
Web Programming and Design
Presentation transcript:

29 November JavaScript: Arrays and Iterations Functions

Final Project Teams Select teams Few minutes to get you started

Review: Forms and JavaScript

HTML Additions Using forms (in body) … Using JavaScript (in head or body) … code …

Types of Inputs Radio Buttons <input type=“radio” name=“name for all buttons” value=“value to be assigned” [checked]> Checkbox <input type=“checkbox” name=“name for this option” [checked]> Text box <input type=“text” name=“name for this field” [value=“initial value”]>

Types of Inputs Select List <select name=“name for this list” size=“number-of-entries-to-show”> Text to be displayed … Buttons <input type=“button” value=“name to be displayed” onclick=“what to do when the button is clicked”>

Example A form that does nothing

JavaScript in HTML Need to indicate the use of JavaScript Two ways: …

JavaScript Defining a variable Numeric, string, boolean Var name = value; Assignment statement Name = expression;

Using statements in forms An example: Shots: <input name="shots“ onclick="num_shots= 1" type="radio"> 1 <input name="shots" onclick="num_shots = 2" type="radio"> 2

Compound Statements Conditional statements Iterations (doing it more than once) Functions (an algorithm that can be executed more than once) All need compound statements – multiple statements that can be processed as one { stmt; stmt; stmt; }

Conditional Statements Executes the statement only if a condition is true Format: if (boolean) statement; /* if purchase made, increase total purchase price and the number of items bought */ if (purchased = true) { total = total + price; items = items + quantity; }; Executes the first statement if a condition is true or the second if it’s false Format: if (boolean) statement; else statement; /* take the absolute value */ if (n < 0) abs_val = !n; else abs_val = n;

Arrays and Iterations

Arrays Arrays are variables with dimensions Why are they interesting? Dealing with a table of values Translations Series of different values Processing a stretch of elements Strings Series of numbers

Arrays in JavaScript Declaration var name = new Array (num-elems); Examples var ZodiacSigns = new Array (12); Or can initialize Var HalfZodiacSigns = new Array (“Aries”, “Taurus”, “Gemini”, “Cancer”, “Leo”, “Virgo”);

Referencing Arrays Array element can be referenced any place that a constant or variable can be used Form: array [ index ] Array is the name Index is which element of the array you want Can be a constant, variable, or expression Must evaluate to an integer between 0 and one less than the array size Example MySign = HalfZodiacSigns[0]; What value does MySign have?

Iterations: Doing it more than once If I want to process all of the elements in an array, I need to tell the computer to do something for each element: Form that we will use for ( itervar = 0; itervar < endval ; itervar++) { Statement-list } var++ is a shorthand that is equivalent to var = var + 1;

What it does for ( i = 0 ; i < n ; i++) { Statement-list } 1. i = 0; 2. execute statement-list 3. i = i + 1; 4. if i < n go to #2

Iteration Example var numbers = new Array (10); var j; /* initialize an array to 1 to 10 */ for ( j=0; j<10; j++) { numbers[j] = j+1; }

Loops with conditionals var values = new Array (10); var results = new Array (10); var j; for ( j=0; j<10; j++) { if (values[j] < 0) results[j] = -j; else results[j] = j; }

Recognizing events in forms Primary one: onclick Others: onfocus, onmouseover, … Attribute within an tag The command that is in that field is executed when the button is clicked on

Functions as Actions Just like a mathematical function Executes its description f(x) = 2x: f(2) = 4, f(3) = 6 Indicated by () following the name Value passed is called the parameter Replace the formal parameter with the value passed

A Simple JavaScript Function Invocation: f(2); Definition: function f(x) { result = 2*x; }

Head Section Material in head section known everywhere All function definitions go there Variables needed everywhere go there Surrounded by …

Learning by Example onclick="buy_latte()“ Function is buy_latte Can pass it a value to work with in () Will also have access to everything within the forms Referenced by document.form_name.input_name.value

Let’s look at some examples Class example (error was missing () in callfunction invocation) Class example