WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (there is an audio component to this eLesson) © Dr.

Slides:



Advertisements
Similar presentations
Introducing JavaScript
Advertisements

WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Decision Making with Control Structures and Statements (non-audio version) © Dr. David.
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
The Web Warrior Guide to Web Design Technologies
IT151: Introduction to Programming
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (there is an audio component to this eLesson) © Dr.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Using Data Types (No Audio Component) © Dr. David C. Gibbs
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
SUNY Morrisville-Norwich Campus-Week 12 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
XP Tutorial 1 New Perspectives on JavaScript, Comprehensive1 Introducing JavaScript Hiding Addresses from Spammers.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Forms in JavaScript (NON-audio version) © Dr. David C. Gibbs
Introduction to scripting
Programming Games Computer science big ideas. Computer Science jargon. Show virtual dog Homework: [Catch up: dice game, credit card or other form.] Plan.
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Data Types and Operators (NON Audio Version) © Dr. David C. Gibbs
Lecture Note 3: ASP Syntax.  ASP Syntax  ASP Syntax ASP Code is Browser-Independent. You cannot view the ASP source code by selecting "View source"
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Introduction to JavaScript 11 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 14.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
JavaScript – Part II Data Types and Operations George Mason University June 3, 2010.
1 WDMD 170 – UW Stevens Point WDMD 170 Internet Languages eLesson: CSS Introduction to Style Sheets (NON-audio version) © Dr. David C. Gibbs
Tutorial 2 Variables and Objects. Working with Variables and Objects Variables (or identifiers) –Values stored in computer memory locations –Value can.
Contents 1 WDMD 170 – UW Stevens Point WDMD 170 Internet Languages e-Lesson: Introduction to WDMD 170 (there is an audio component to this e-Lesson) ©
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
1 JavaScript in Context. Server-Side Programming.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
JavaScript Tutorial 1 - Introduction to JavaScript WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Introduction to JavaScript (NON.
C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Decision Making with Control Structures and Statements (non-audio version) © Dr. David.
Introduction to programming in the Java programming language.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: HTML/XHTML Tables (NON-audio version) © Dr. David C. Gibbs
JavaScript Tutorial 1 - Introduction to JavaScript WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Introduction to JavaScript – A First.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
CS346 Javascript -3 Module 3 JavaScript Variables.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
JavaScript, Fourth Edition
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Working with Windows (No audio component) © Dr. David C. Gibbs
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
IS2802 Introduction to Multimedia Applications for Business Lecture 3: JavaScript and Functions Rob Gleasure
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (NON-Audio version) © Dr. David C. Gibbs WDMD.
Creating Web Documents: JavaScript Ftp / file management: review Introduction to JavaScript Sources Homework: start review for midterm, work on Project.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 2: Introduction to IS2803 Rob Gleasure
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
JavaScript 101 Lesson 6: Introduction to Functions.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Chapter 6 JavaScript: Introduction to Scripting
Functions Comp 205 Fall ‘17.
Introduction to Scripting
JavaScript Syntax and Semantics
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
JavaScript.
Data Types, Identifiers, and Expressions
WEB PROGRAMMING JavaScript.
T. Jumana Abu Shmais – AOU - Riyadh
Tutorial 10: Programming with javascript
JavaScript: Introduction to Scripting
CIS 136 Building Mobile Apps
Presentation transcript:

WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (there is an audio component to this eLesson) © Dr. David C. Gibbs NOTE: click on the “Slide Show” icon in the lower right of the screen to hear the audio track WDMD 170 – UW Stevens Point

2 Tutorial 2 Variables, Functions, and Events Section A – Working with Variables and Functions

WDMD 170 – UW Stevens Point 3 Tutorial 2A Topics Section A – Working with Variables and Functions –How to declare and use variables –How to define and call functions and “returns” functions –About built-in JavaScript functions –About variable scope

WDMD 170 – UW Stevens Point 4 Variables and Variable Names Variables – also known as identifiers –Values stored in computer memory locations –Value can vary over time –Cannot use reserved words as variables reserved Words or Keywords are part of the JavaScript language syntax –Variable name example: employeeName

WDMD 170 – UW Stevens Point 5

6

7 Variable Declaration and Defining Variables –To create: Use keyword var to declare the variable Use the assignment operator to assign the variable a value –Declare, then assign value (initialize) var employeeName; employeeName = “Ricky”; –Declare and assign variable in a single statement Called “declare and define” var employeeName = “Ricky”;

WDMD 170 – UW Stevens Point 8 Use of Variables Variables –Once created: May be changed at any point in the program –Use variable name and assignment operator employeeName = “Althea”;

WDMD 170 – UW Stevens Point 9 Syntax Rules for Variables Cannot use reserved words & spaces Must begin with one of the following: –Uppercase or lowercase ASCII letter –Dollar sign ($) or underscore (_) Can use numbers, but not as first character Variables are case-sensitive –e.g. employeeName and EmployeeName are DIFFERENT!

WDMD 170 – UW Stevens Point 10 Variable Naming Convention Naming conventions –Use underscore or capitalization to separate words of an identifier employee_first_name employeeFirstName (called “camel” back naming)

WDMD 170 – UW Stevens Point 11

WDMD 170 – UW Stevens Point 12

WDMD 170 – UW Stevens Point 13 Functions Defining Custom Functions –Function: Individual statements grouped together to form a specific procedure Allows you to treat the group of statements as a single unit Must be contained between and tags Must be formally composed (function definition)

WDMD 170 – UW Stevens Point 14 Defining a Function: Function Components Defining Custom Functions –A function definition consists of three parts: Reserved word function followed by the function name (identifier) Parameters required by the function, contained within parentheses following the name –Parameters  variables used within the function –Zero or more may be used Function statements, delimited by curly braces { }

WDMD 170 – UW Stevens Point 15 Defining a Function: the syntax The syntax for defining a function: function name_of_function(parameters) { statements; }

WDMD 170 – UW Stevens Point 16

WDMD 170 – UW Stevens Point 17 Function Definition Example function print_company_name(company1,company2,company3) { document.writeln(company1); document.writeln(company2); document.writeln(company3); } reserved wordparametersidentifier

WDMD 170 – UW Stevens Point 18 Calling or Invoking a Function Calling Functions –Function invocation or call Statement including function name followed by a list of arguments in parentheses Parameter of function definition takes on value of argument passed to function in function call –for example print_company_name("IBM", "Apple", "Microsoft");

WDMD 170 – UW Stevens Point 19 Calling Functions Code placement –Functions must be created (defined) before called rendered by browser before –Function definition Place in section –Function call (invocation) Place in section

WDMD 170 – UW Stevens Point 20 eTask 1 Copy and paste the text of the function definition into the head of an HTML document. Copy and paste the function invocation into the body. Remember to use tags in both cases! Save your file as “FirstFunctionExample.htm” in your Tutorial02 folder. Preview the code. Does it display the company names properly? //function definition function print_company_name(company1,company2,company3) { document.writeln(company1); document.writeln(company2); document.writeln(company3); } //function invocation print_company_name("IBM", "Apple", "Microsoft");

WDMD 170 – UW Stevens Point 21 Roles of Functions A function can return nothing –Just performing some task (as in the previous example – wrote out company names) A function can return a value –Perform a calculation and return the result var returnValue = functionCall(parm1,parm2); –A return statement must be added to function definition

WDMD 170 – UW Stevens Point 22 eTask 2 Here’s a function to calculate the area of a rectangle. Copy and paste the definition and invocation as before. (remember - tags! Save the file as “areaRectangleFn.htm”. //function definition function areaRectangle(length, width) { var area;// local variable area = length * width; //calculate return area;// return the value to the calling variable } //function invocation var area = areaRectangle(7, 5); document.write(area);

WDMD 170 – UW Stevens Point 23 Types of Functions There are 2 types of functions; –those that do something –those that “return” something Those that do are usually just called “functions”. Those that return are called “return-type” or “returns” functions.

WDMD 170 – UW Stevens Point 24 eTask 3 Refer to the instructions on pages 55-6 (Gosselin). Create the file TwoFunctionsProgram.htm. Preview the.htm file. Compare your results with that of page 57!

WDMD 170 – UW Stevens Point 25 Built-in JavaScript Functions Functions defined as part of the JavaScript language Function call identical to the custom functions

WDMD 170 – UW Stevens Point 26

WDMD 170 – UW Stevens Point 27 Built-In Function Example var myVar, check_myVar; myVar = “two"; check_myVar = isNaN(myVar); document.write(check_myVar); Displays “true”. isNaN() stands for “is not a number”. It is a returns function that gives back true if the contents of its single parameter is NOT a valid number, and false if it is a valid number.

WDMD 170 – UW Stevens Point 28 eTask 4 Copy and paste the text of the previous slide into the body of an HTML document. Remember to use tags. Save your file as “using_IsNaN.htm”. View the results. You should get “true”. Copy the three line sequence (not the var declarations) three more times (in the same file). Change the assignment statement to myVar to 2, “2”, and “2b”, respectively. NOTE: If you want your results written on separate lines, add the.. tags, or, better yet, modify the write statement to this: document.write(check_myVar + " "); Do the results make sense? If they don’t, ask the question in your eReview group!]

WDMD 170 – UW Stevens Point 29 Variable Scope Defines where in the program a declared variable can be used –Global variable Declared outside a function and is available to all parts of the program var keyword optional –Local variable Declared inside a function and is only available within the function it is declared –Global and local variables can use same identifier

WDMD 170 – UW Stevens Point 30 Assignment – Scope of Variables Type in the JavaScript program found on pages Save the file as scopeOfJSVars.htm. View the program. (After getting the error message, you can comment out the offending line.) Add the following questions to the body of the HTML document (and then provide answers immediately underneath). 1.Why did “localVariable” cause an error? 2.What is it about “secondGlobalVariable” that makes it global even though it is declared within the function? 3.Can a global variable and a local variable have the same name? If not, why not? If so, how does the program determine which to use? Complete the exercise and attach the file in a post to your eReview discussion group. Describe any difficulties you might have had in completing the problem. Please do not copy it to your web page until one week later.

WDMD 170 – UW Stevens Point 31 Assignment Exercise #8 page 80 (Gosselin, Tutorial 02A) Complete the exercise and attach the file in a post to your eReview discussion group. Describe any difficulties you might have had in completing the problem. Please do not copy it to your web page until one week later.

WDMD 170 – UW Stevens Point 32 End of eLesson Jump to the Beginning of this eLesson WDMD 170 Course Schedule D2L Courseware Site