Lesson 2: Input and Variables

Slides:



Advertisements
Similar presentations
1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight.
Advertisements

The Web Warrior Guide to Web Design Technologies
Lesson 4: Formatting Input Data for Arithmetic
JavaScript 101 Lesson 01: Writing Your First JavaScript.
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.
CIS101 Introduction to Computing Week 10 Spring 2004.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
CIS101 Introduction to Computing Week 09. Agenda Hand in Resume project Your questions Introduction to JavaScript This week online Next class.
CIS101 Introduction to Computing Week 10. Agenda Your questions Final exam and final project CIS101 Student Survey Class presentations: Your Mad Libs.
JavaScript 101 Lesson 5: Introduction to Events. Lesson Topics Event driven programming Events and event handlers The onClick event handler for hyperlinks.
CIS101 Introduction to Computing Week 12 Spring 2004.
CIS101 Introduction to Computing Week 09 Spring 2004.
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
Introduction to scripting
Adding JavaScript (<script tag>)
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Programming.
Intro to Programming and JavaScript. What is Programming? Programming is the activity of creating a set of detailed instructions (Program) that when carried.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Chapter 4 JavaScript and Dynamic Web pages. Objectives Static Web pages Dynamic Web pages JavaScript Variables Assignments. JavaScript Functions –(prompt(“”,””)
Objective Static vs. Dynamic Web pages. Variables. Assignments. JavaScript Hierarchy of Objects JavaScript Functions (prompt(“”,””) Document.write(“”)
1 JavaScript in Context. Server-Side Programming.
Introduction to JavaScript Basharat Mahmood, Department of Computer Science, CIIT, Islamabad, Pakistan. 1.
Dynamic Web Pages & JavaScript. Dynamic Web Pages Dynamic = Change Dynamic Web Pages are web pages that change. More than just moving graphics around.
DHTML AND JAVASCRIPT Genetic Computer School LESSON 5 INTRODUCTION JAVASCRIPT G H E F.
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
1 JavaScript
JavaScript Programming Unit #1: Introduction. What is Programming?
1 A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 4 JavaScript and.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
XP Tutorial 8 Adding Interactivity with ActionScript.
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.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
JavaScript 1 COE 201- Computer Proficiency. Introduction JavaScript scripting language ▫Originally created by Netscape ▫Facilitates disciplined approach.
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Expressions and Data Types Professor Robin Burke.
JavaScript 101 Lesson 6: Introduction to Functions.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
Programming Web Pages with JavaScript
CHAPTER 10 JAVA SCRIPT.
Chapter 6 JavaScript: Introduction to Scripting
Programming Fundamental
Chapter 13 - JavaScript/JScript: Introduction to Scripting
* Lecture # 7 Instructor: Rida Noor Department of Computer Science
Variables, Expressions, and IO
Introduction to Scripting
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
JavaScript.
Intro to PHP & Variables
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Exercises on JavaScript & Revision
Topics Introduction to File Input and Output
WEB PROGRAMMING JavaScript.
INFO/CSE 100, Spring 2005 Fluency in Information Technology
T. Jumana Abu Shmais – AOU - Riyadh
INFO/CSE 100, Spring 2006 Fluency in Information Technology
For this assignment, copy and past the XHTML to a notepad file with the .html extension. Then add the code I ask for to complete the problems.
Tutorial 10: Programming with javascript
JavaScript: Introduction to Scripting
Topics Introduction to File Input and Output
This is an introduction to JavaScript using the examples found at the CIS17 website. In previous examples I specified language = Javascript, instead of.
JavaScript 101 Lesson 8: Loops.
Intro to Programming (in JavaScript)
Presentation transcript:

Lesson 2: Input and Variables JavaScript 101 Lesson 2: Input and Variables

Lesson Topics How to include data in your script by using a variable How to declare (create) a variable How to name a variable (rules for identifiers) How to assign a value to a variable using = (assignment)

Lesson Topics (cont.) How to use the prompt statement to collect information from Web page visitor How to display (output) the contents (value) of a variable About string formatting methods How to combine strings using + operator (string concatenation)

Using Variables Programs mostly collect, evaluate, and process information Programs need to include information (data) Programming languages use variables to represent information or data JavaScript variables can represent numbers, strings (character data), and Boolean(logical) values in JavaScript

Declaring Variables First step in including variables in your program is a declaration Declaration creates a variable Example: var myName; “var” is a keyword that indicates that this is a variable declaration Keyword (see Intro) has a defined meaning in JavaScript

Variables need a name The name of a variable is called an identifier A legal identifier in JavaScript is governed by the following rules: The first character must be a letter or an underscore(_) The remaining characters may be numbers, letters, and underscore

Assigning Values to Variables The equal sign, =, is called the assignment operator in JavaScript and it is used to assign values to variables myName = “Fred”; Values are always copied from right to left

Using prompt and variables The JavaScript statement prompt asks Web visitors a question and records (saves) their answer Example: var visitorName = prompt(What is your name?”,”Enter your name here”); (see p. 2-3 to see how this statement appears)

Syntax of prompt statement Syntax: var varname=prompt(“your question”,”default entry”); varname stores answer from visitor “your question” is what program asks the visitor “default entry” is answer that program will save if visitor doesn’t input a response (i.e. visitor just hits enter)

Displaying a Variable Variables save useful information for your program To display information saved in a variable use document.write with the variable’s name (no quote marks) Example: var myName = “Sam”; document.write(myName); This displays Sam in a Web document Emphasize that is displays Sam, not “myName”

String Formatting Methods JavaScript has string formatting methods that alter appearance of text var sentence = “An Example”; document.write(sentence.bold()); //displays the string in bold document.write(sentence.italics()); //displays the string in italics

Concatenating Strings Concatenation is an operation that combines strings (puts them together) The + operator is used to combine strings var part1 = “This sentence ”; var part2= “has 2 pieces” var sentence = part1 + part2;

In the lab This lab uses variables and the prompt method Open Notepad and create a new HTML document named lesson0201.html Enter the code on p. 2-6 exactly as you see it Save the file and open it using either Internet Explorer or Netscape Add modifications/changes described on p. 2-7

Mad Lib Next example is a JavaScript program that writes a simple Mad Lib Mad Lib is a game where potentially humorous story is written down, with blanks in the place of some important words Before reading story, storyteller asks other to fill in the blanks without knowing the context Then resulting story is read

JavaScript Mad Lib Save code from previous exercise Create a new document named lesson0202.html Will use variables, prompt, and concatenation to create a JavaScript Mad Lib Enter the code on p. 2-8 Once your program is running, add modifications (p. 2-8)

Lesson Summary How to declare variables JavaScript rules for identifiers Used assignment operator (=) to assign value to a variable Used the prompt method to ask a visitor a question and record their response

Lesson Summary cont. Combined strings (concatenation and + operator Displayed the value of a variable with document.write Used string formatting methods