INFO/CSE 100, Spring 2005 Fluency in Information Technology

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

14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,
1 The Information School of the University of Washington Oct 30fit review Programming Review INFO/CSE 100, Fall 2006 Fluency in Information Technology.
The Information School of the University of Washington Oct 23 fit functions 1 Functions / If Else INFO/CSE 100, Fall 2006 Fluency in Information.
Chapter 20 Thinking Big: Functions. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Anatomy of a Function Functions are packages for.
1 The Information School of the University of Washington Oct 27fit arrays © 2006 University of Washington Arrays INFO/CSE 100, Fall 2006 Fluency.
The Information School of the University of Washington Dec 8fit review1 Final Exam Review INFO/CSE 100, Fall 2006 Fluency in Information Technology.
29 November JavaScript: Arrays and Iterations Functions.
JavaScript, Third Edition
The Information School of the University of Washington Oct 20fit programming1 Programming Basics INFO/CSE 100, Fall 2006 Fluency in Information Technology.
Oct 25 1 The Information School of the University of Washington fit control © 2006 University of Washington Control Flow INFO/CSE 100, Fall 2006.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Test 2 Part a You have 20 minutes Don’t forget to put your name on the test Closed book No computers Do your own work.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Week 9 PHP Cookies and Session Introduction to JavaScript.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Using Client-Side Scripts to Enhance Web Applications 1.
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
JavaScript - Basic Concepts Prepared and Presented by Hienvinh Nguyen, Afshin Tiraie.
JavaScript, Fourth Edition
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
Tutorial 11 1 JavaScript Operators and Expressions.
CGS 3066: Web Programming and Design Spring 2016 Programming in JavaScript.
JavaScript Tutorial. What is JavaScript JavaScript is the programming language of HTML and the Web Programming makes computers do what you want them to.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Bill Tucker Austin Community College COSC 1315
Build in Objects In JavaScript, almost "everything" is an object.
CGS 3066: Web Programming and Design Spring 2017
Java Script Introduction. Java Script Introduction.
Chapter 10 Programming Fundamentals with JavaScript
CHAPTER 10 JAVA SCRIPT.
Chapter 6 JavaScript: Introduction to Scripting
CHAPTER 4 CLIENT SIDE SCRIPTING PART 3 OF 3
JavaScript is a programming language designed for Web pages.
Overview: Programming Concepts
JavaScript: Functions
JavaScript Functions.
JavaScript Syntax and Semantics
JavaScript: Functions.
JavaScript.
INFO/CSE 100, Spring 2005 Fluency in Information Technology
INFO/CSE 100, Spring 2006 Fluency in Information Technology
Object-Oriented Programming: Classes and Objects
Chapter 10 Programming Fundamentals with JavaScript
INFO/CSE 100, Spring 2005 Fluency in Information Technology
Chapter 8 JavaScript: Control Statements, Part 2
INFO/CSE 100, Spring 2006 Fluency in Information Technology
Introduction to JavaScript for Python Programmers
Document Object Model (DOM)
INFO/CSE 100, Spring 2006 Fluency in Information Technology
INFO/CSE 100, Spring 2005 Fluency in Information Technology
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
JavaScript What is JavaScript? What can JavaScript do?
INFO/CSE 100, Spring 2006 Fluency in Information Technology
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
JavaScript What is JavaScript? What can JavaScript do?
JavaScript Basics What is JavaScript?
INFO/CSE 100, Spring 2005 Fluency in Information Technology
Introducing JavaScript
JavaScript: Introduction to Scripting
Javascript Chapter 19 and 20 5/3/2019.
CIS 136 Building Mobile Apps
Programming Basics Review
INFO/CSE 100, Spring 2006 Fluency in Information Technology
Web Programming and Design
CGS 3066: Web Programming and Design Spring 2016
Presentation transcript:

INFO/CSE 100, Spring 2005 Fluency in Information Technology Digital Information INFO/CSE 100, Spring 2005 Fluency in Information Technology http://www.cs.washington.edu/100 4/16/2019 fit100-19-review © 2005 University of Washington

Readings and References Fluency with Information Technology Chapters 9, 11 18-21 4/16/2019 fit100-19-review © 2005 University of Washington

fit100-19-review © 2005 University of Washington Variables In Real Life A variable is a "container" for information you want to store The name of the variable stays the same, but the value associated with that name can change That’s why it’s called a “variable”! 4/16/2019 fit100-19-review © 2005 University of Washington

Variables In Programming Program variables have names and values Names (also called identifiers) generally start with a letter and can contain letters, numbers, and underscore characters “_” Names are case sensitive Values can be numbers, strings, boolean, etc change as the program executes No Spaces! No punctuation characters other than underscore Variable names should usually start with a lowercase letter. 4/16/2019 fit100-19-review © 2005 University of Washington

Variable Declarations <script type="text/javascript"> var eyeColor; <<< undefined! var eyeColor = "green"; <<< initialized var eyeColor = ""; <<< initilized, empty var eyeColor = "green", hairColor="blonde"; hairColor = "carmel"; </script> Every variable must be declared in a statement -- (ends with a semi-colon). Initializing is setting the initial value! Can be set to empty. Can declare more than one variable at a time, separated by a comma. Assignment is changing the variables value AFTER it has been initialized! 4/16/2019 fit100-19-review © 2005 University of Washington

Basic Data Types in Javascript Numbers: var gasPrice = 2.55; Strings var eyeColor = "hazel green"; Boolean var isFriday = true; var isWeekend = 0; Javascript will figure out the data type: number, string, boolean. Numbers will not be in units. (ex: no dollar sign). Numbers must be quoted Strings can be any length!, including empty; Strings must be quoted (either single or double. << must match! Boolean values can be 1 or 0! 4/16/2019 fit100-19-review © 2005 University of Washington

fit100-19-review © 2005 University of Washington Expressions The right-hand side of an assignment statement can be any valid expression Expressions are “formulas” saying how to manipulate existing values to compute new values balance = balance - transaction; seconds = 60*minutes; message = "Status code is " + codeValue; isFreezing = (temp < 32); 4/16/2019 fit100-19-review © 2005 University of Washington

fit100-19-review © 2005 University of Washington Operators Use operators to build expressions Numeric operators + - * / mean add, subtract, multiply, divide 3 + 3 = 6 String operator + means concatenate strings "3" + "3" = "33" Relational operators < <= == != >= > mean less than, less than or equal to, equal to, not equal to, greater than or equal to, greater than Boolean operators && || ! mean and, or, not + sign is an example of operator overload. It means different things when used with numbers and strings. Numbers are added together like math. Strings are concatenated. 4/16/2019 fit100-19-review © 2005 University of Washington

fit100-19-review © 2005 University of Washington Functions A function is a way to bundle a set of instructions and give them a name so that you can reuse them easily Functions have a specific layout <name>  the function name is an identifier <parameter list>  list of input variables for the function <statements>  the statements do the work function <name> ( <parameter list> ) { <statements> } 4/16/2019 fit100-19-review © 2005 University of Washington

fit100-19-review © 2005 University of Washington Example Function function <name> ( <parameter list> ) { <statements> } template Write a simple function to compute the Body Mass Index when the inputs are in English units (ie, US units) // Calculate Body Mass Index in English units // weight in pounds // height in inches // returns body mass index function bmiE(weightLBS, heightIN) { var heightFt = heightIn / 12; // convert to feet return 4.89 * weightLBS / (heightFt * heightFt); } example 4/16/2019 fit100-19-review © 2005 University of Washington

fit100-19-review © 2005 University of Washington Calling a Function // Calculate Body Mass Index in English units // weight in pounds // height in inches // returns body mass index function bmiE(weightLBS, heightIN) { var heightFt = heightIn / 12; // convert to feet return 4.89 * weightLBS / (heightFt * heightFt); } parameters // call the bmiE function var bmi = bmiE(162, 51); // another function call document.write(bmiE(162, 51)); function calls The parameters are in the function definition! The arguments are included when the function is called. arguments 4/16/2019 fit100-19-review © 2005 University of Washington

fit100-19-review © 2005 University of Washington Global or Local?!? Scope of a variable describes where and when it can be referenced Local variables are only known inside of a function (curly braces) Global variables are know by all the Javascript inside of <script> </script> pairs // Calculate Percentage of Study Hours/Week // time in hours // returns hours var days = 7; function calculateStudyHrs(time) { var totalHrs = 24 * days; return time/totalHrs; } days = global variable time, totalHrs are local variables 4/16/2019 fit100-19-review © 2005 University of Washington

fit100-19-review © 2005 University of Washington Layout of the GUI The layout of the page is controlled with HTML in the body of the page The layout and controls are provided using new tags <form id="buttonForm"> <button type="button" ... <input type="text" … <input type="radio" … <button type="reset" … <body> HTML form layout and specification </body> </html> 4/16/2019 fit100-19-review © 2005 University of Washington

A simple example This GUI has several simple controls. Two buttons to control the results One text field to display the results One pair of radio buttons to control the display One button to reinitialize http://www.cs.washington.edu/education/courses/100/04au/slides/13-gui/gui.html 4/16/2019 fit100-19-review © 2005 University of Washington

fit100-19-review © 2005 University of Washington Form Controls <form> <button type="button" onclick="setResults('good results')">Good Results</button> onclick="setResults('bad results')">Bad Results</button> <b>Result:</b> <input type="text" value="nada" readonly id="resultField"> <br> <input type="radio" name="case" id="radioLC" checked onclick="setResults(document.getElementById('resultField').value)">Lowercase <input type="radio" name="case" id="radioUC" onclick="setResults(document.getElementById('resultField').value)">Uppercase <br><button type="reset">Reset</button> </form> 4/16/2019 fit100-19-review © 2005 University of Washington

Events Cause Processing After drawing a page, the browser sits idle waiting for something to happen … when we give input, we cause events Processing events is the task of a block of code called an event handler The code to execute is identified in the tag using the appropriate attribute There are many event types onClick, onChange, onMouseOver ... 4/16/2019 fit100-19-review © 2005 University of Washington

setResults(resultString) <script type="text/javascript"> function setResults(resultString) { var tempString = resultString; if (document.getElementById("radioLC").checked) { tempString = tempString.toLowerCase(); } else if (document.getElementById("radioUC").checked) { tempString = tempString.toUpperCase(); } document.getElementById("resultField").value = tempString; </script> parameter variable, local variable, if/else statement, field reference, call to toLowerCase() function 4/16/2019 fit100-19-review © 2005 University of Washington

fit100-19-review © 2005 University of Washington The if / else statement The if statement is a conditional statement a conditional expression is evaluated as being true or false the expression is a boolean expression (ie, returns true or false) if the condition is true, then one set of statements is executed if the statement is false, then a different set of statements is executed condition if (<boolean expression>) { <statements> } else { } 4/16/2019 fit100-19-review © 2005 University of Washington

fit100-19-review © 2005 University of Washington Examples if (count == 0) { ready = false; } else { ready = true; count = count-1; } What is the conditional expression? What statements are part of the true block? Which statements are part of the false block? What happens when count is 21? 0? -1? if (pageCount >= 100) { alert("This may take a few minutes."); } What is the conditional expression? What statements are part of the true block? Which statements are part of the false block? What happens when pageCount is 21? 100? 200? 4/16/2019 fit100-19-review © 2005 University of Washington

More if/else Statements if (temp < 32) { if (sky == "cloudy) { alert("Snow is forecast!"); } if (temp < 32 && sky == "cloudy") { Nested Complex condition 4/16/2019 fit100-19-review © 2005 University of Washington

The for loop A counting loop is usually implemented with for var count = 10; update loop control index shorthand for i=i+1 initialize check for limit for (var i=0; i < count; i++) { document.writeln("<br>index value is : "+i); } World Famous Iteration (WFI)! one or more statements in the loop body 4/16/2019 fit100-19-review © 2005 University of Washington

fit100-19-review © 2005 University of Washington i++ is a shortcut for (i=0; i < count; i++) at the end of every pass through the for loop body, do the following: get the value of i increment i store the incremented value Used as it is here, this is the same as writing i = i + 1 4/16/2019 fit100-19-review © 2005 University of Washington

body of loop may not execute at all Notice that depending on the values of the control variables, it is quite possible that the body of the loop will not execute at all check for limit condition itemCount is 0 when we get here, so i<itemCount is immediately false and the loop body is skipped completely var itemCount = 0; ... for (var i=0; i < itemCount; i++) { document.writeln("<br>..processing item "+i); } 4/16/2019 fit100-19-review © 2005 University of Washington

fit100-19-review © 2005 University of Washington Arrays JavaScript (and most other languages) includes arrays as the most basic kind of collection. Simple, ordered collections Special syntax for accessing elements by position JavaScript arrays can be created by the programmer in the script by the system and provided to the script for example, the elements array in the iCCC program 4/16/2019 fit100-19-review © 2005 University of Washington

fit100-19-review © 2005 University of Washington Array Example variable String petNames "Jaba" Array length : 5 String index 0 "Bingo" index 1 index 2 index 3 String index 4 "Jessica" 4/16/2019 fit100-19-review © 2005 University of Washington

JavaScript Indexed Arrays An indexed array is a data type that stores a collection of values, accessible by number the values in the array are called the elements of the array the elements (or values) are accessed by index the index of the first value is 0 the values in the array can be any type usually all the values are the same type but they can be different from one another if necessary 4/16/2019 fit100-19-review © 2005 University of Washington

Array Declaration and Creation Arrays can be created several different ways var petNames = new Array(); 0-length array with no elements in it yet var studentNames = new Array(102); 102-element array, all of which have the value undefined var myList = ["Sally", "Splat", "Google"]; 3-element array initialized with an array literal Arrays have a property that stores the length <array name>.length you can lengthen or shorten an array by setting the length to a new value 4/16/2019 fit100-19-review © 2005 University of Washington

fit100-19-review © 2005 University of Washington Array Element Access Access an array element using the array name and position: <array name> [<position>] Details: <position> is an integer expression. Positions count from zero Update an array element by assigning to it: <array name> [ <position> ] = <new element value> ; myCurrentCarNo = carList.length-1; myCurrentCar = carList[myCurrentCarNo]; carList.length will give me the total number of elements But the highest index is carList.length - 1 4/16/2019 fit100-19-review © 2005 University of Washington