Week#2 Day#1 Java Script Course

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
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Information Technology Center Hany Abdelwahab Computer Specialist.
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
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.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Using Client-Side Scripts to Enhance Web Applications 1.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
CPS120: Introduction to Computer Science Decision Making in Programs.
JavaScript Adding active content to websites. Goals Understand structure of JavaScript Understand rules of coding Add active content to WebPages Add functions.
4.4 JavaScript (JS) Deitel Ch. 7, 8, 9, JavaScript & Java: Similarities JS (JavaScript) is case-sensitive Operators –arithmetic: unary +, unary.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
JavaScript, Fourth Edition
IS2802 Introduction to Multimedia Applications for Business Lecture 3: JavaScript and Functions Rob Gleasure
Pertemuan 5 IT133 Pengembangan Web JavaScript. What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting.
Working with Loops, Conditional Statements, and Arrays.
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
Tutorial 11 1 JavaScript Operators and Expressions.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.
JavaScript and Ajax (Control Structures) Week 4 Web site:
Working with the Window Object JavaScript considers the browser window an object, which it calls the window object.
JavaScript and Ajax (JavaScript Functions) Week 5 Web site:
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.
>> Introduction to JavaScript
Chapter 10 Programming Fundamentals with JavaScript
CHAPTER 10 JAVA SCRIPT.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
REPETITION CONTROL STRUCTURE
CHAPTER 4 CLIENT SIDE SCRIPTING PART 3 OF 3
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Loop Structures.
Week#3 Day#1 Java Script Course
JavaScript Syntax and Semantics
Scripts & Functions Scripts and functions are contained in .m-files
Chapter 6: Conditional Statements and Loops
Expressions and Control Flow in JavaScript
CHAPTER 4 CLIENT SIDE SCRIPTING PART 2 OF 3
The structure of computer programs
JavaScript and Ajax (Expression and Operators)
Arrays, For loop While loop Do while loop
JavaScript Selection Statement Creating Array
Chapter 10 Programming Fundamentals with JavaScript
Objectives Create an array Work with array properties and methods
WEB PROGRAMMING JavaScript.
CS190/295 Programming in Python for Life Sciences: Lecture 6
T. Jumana Abu Shmais – AOU - Riyadh
Coding Concepts (Basics)
CIS 16 Application Development Programming with Visual Basic
Introduction to Problem Solving and Control Statements
CS105 Introduction to Computer Concepts
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
CISC124 Labs start this week in JEFF 155. Fall 2018
Chapter 4: Control Structures I (Selection)
JavaScript CS 4640 Programming Languages for Web Applications
JavaScript: Introduction to Scripting
Javascript Chapter 19 and 20 5/3/2019.
Web Programming– UFCFB Lecture 13
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
CIS 136 Building Mobile Apps
Web Programming and Design
CS105 Introduction to Computer Concepts JavaScript
Intro to Programming (in JavaScript)
Presentation transcript:

Week#2 Day#1 Java Script Course

www.jsclass.wordpress.com

Interacting With The User.

Alert, Confirm and Prompt An alert() is a way of giving the user some information, and then they have to respond to it. It belongs to window object. Syntax: alert(" alert_text") Example: alert(“Welcome to JavaScript Course”); The look of the message box depends on the browser.

confirm() The confirm() window displays a confirmation dialog box to the viewer, who must then click OK or Cancel to proceed. A confirm box is often used if you want the user to verify or accept something. Syntax: confirm("Question") Example: confirm("The result of 2+2 is 4?");

Prompt() A Prompt() box is used if you want the user to input some information. Using this window, you can do things based on what the viewer enters into the text box at the prompt. Syntax: prompt("Message", DefaultValue) Example: prompt("What is your name?", "")

Comparisons. > < != >= <= == Operator Meaning Example      Equal      3 == 8   result FALSE !=      Not Equal      3 != 8   result TRUE >       Greater than      3 > 8   result FALSE <       Less than      3 < 8   result TRUE >= Greater than or Equal      3 >= 8   result FALSE <= Less than or Equal      3 <= 8   result TRUE

If you don’t control your mind, someone else will! CONTROL STRUCTURES If you don’t control your mind, someone else will!

CONDITIONALS

Conditionals The ability to make different things happen depending on different circumstances These Statements alter sequential execution of program statements JavaScript uses the following conditional statements: Simple if statement if … else statement Switch Case statement

The “if” Statement The general form of this statement is: If(condition) { The code to be executed if true } Condition can be any logical expression If “Condition” returns TRUE, the code in the curly braces will be executed, and if the “Condition” returns FALSE, the execution passes over to the following statements.

The "if ... else" statement The general form of "if ... else" instruction is: If(condition) { The code to be executed if TRUE } else { The code to be executed if FALSE

If…else Example

The “Switch Case” Statement The switch statement is a multi-way branch statement. This statement requires only one argument, which is then checked with number of case options.

The Syntax of switch case switch (expression) { case value1:     code executed if expression = value1     break case value2:     code executed if expression = value2     break case value3:     code executed if expression = value3     break default :     code executed if expression is other then value1, value2 or value3 }

Loops

Loops. Used to perform the same action or set of actions repeatedly until some condition is met. There are 3 kinds of loops: For Loop. While Loop. Do While Loop

The for Loop. Is used when you know how many times the script should run. For loops need a initial expression condition and update expression. Syntax:

for Loop. Other variations of for loop include: for …… in : loops through an element of array or properties of an object. for each… in: used to iterate between object properties. Let us try some code:

Break and Continue Some times a loop is used to perform a task like searching for specific string. Once the desired item is found, there is no need to continue looping. In this case, we use the break statement.

Look at this example.

Continue Statement. Some times you may want to skip execution of statement in a loop if certain condition is met. But still want to continue looping. Java script provides continue statement to handle this task.

While Loop. Some times it is impossible to determine exactly how many times a program will need to perform action. This is where while loop comes in handy. while loop continues executing statements as long as some condition is true or until that condition is false. Let us try some code…

While loop. Some errors you may encounter…

While loop.

While loop: Simple grading system.

Add some validation

Do while loop Do.. While is while’s cousin. Difference is: While loop’s content may never execute depending on the condition’s evaluation. Do… while loop contents will always execute at least one. Do.. While is called post-test. While loop is called pre-test. Syntax:

Do while example

DO NOT DO LIKE THIS.

Home work

Week#2 Day#2 Java Script Course

ARRAYS

Arrays. The Array object is used to store multiple values in a single variable name. Each stored value becomes an array element. The index is used to retrieve any element of the array. Three ways to declare an array: Using new keyword to create array object. Dense Arrays. Array Literals.

Creating an Array Object Syntax: In java Script: you do not have to define array size and data type. But it is recommended to specify array size at declaration time. Var arrayName= new array(arraySize); Example: Index: 0 --- 3

Storing Data in Array If you want to access an Array element:

Other Types Dense Arrays: you declare and initialize the array elements at the same time. Array Literal: list of expressions enclosed in brackets.

Using for loop with arrays.

Array Methods. Reverse(): reverses the order of elements in an array. Concat (): allows to combine two or more arrays in to one. Syntax: firstArray.concat(array1, array2, ….); Reverse(): reverses the order of elements in an array. Syntax: arrayName.reverse(); Sort(): sorts the elements of an array lexigraphically Syntax: arrayName.sort();

Array Methods. Slice(): returns an extracted segment of an array as a new array. Syntax: arrayName.slice(Bindex, Eindex); Push(): adds elements to the end of the array. Syntax: arrayName.push(elem1,elem2,…); Pop(): removes the last element of the array. Syntax: arrayName.pop();

FUNCTIONS

What is a Function? A function is a block of predefined programming statements whose execution is deferred until the function is “called.” In other words, a function is a predefined block of code that doesn’t execute until you “call” it. Defer = put off to a later time; postpone.

You call a function by invoking its name, along with any required or optional parameters. Function parameters, also known as arguments, are data values or data references that you pass to a function

Syntax function functionName() { code to be executed } With Parameters function functionName(var1, var2, ...) {     code to be executed }

Calling Functions Without arguments: functionName(); With arguments: functionName(arg1, arg2, ...);

Types of Functions In JavaScript, there are basically two types of functions: Predefined functions User-defined functions

Predefined functions They are further divided into 2, they are: functions predefined by the JavaScript language but not associated with any particular object. Examples: parseInt(), isNaN(). functions predefined by the JavaScript language and associated with objects that have also been predefined by the language. Examples: document.write(), and someDate.getDate().

User-defined functions They are also of two types: functions defined by a programmer, often called user-defined functions. Examples: helloWorld(), greetVisitor(), etc. functions defined by a programmer and associated with a particular object, usually a user-defined object. Example: student.setGrade().

Let Us See Some Examples

Methods for Changing Case toLowerCase() and toUpperCase() Example: var myName = “Warsame" document.write(myName.toUpperCase(), "<BR>") document.write(myName, "<BR>")

setTimeout() and setInterval() The window method setTimeout lets you call a function after a specified number of milliseconds have elapsed. In essence, it allows you to set a timer that calls a JavaScript statement or function after a certain period of time. Syntax: setTimeout("statement", numMilliseconds) OR setTimeout("functionName", numMilliseconds)

clearTimeout The creator of JavaScript anticipated that you might want to turn off a setTimeout after you started it, that is, that you might want to stop it before it executed. So he created setTimeout to return an integer representing the timeout’s unique identification. By assigning the returned ID to a variable, you can control the time-out, stopping it if necessary.

Syntax for saving a time-out’s unique ID: timerName = setTimeout("functionName", numMilliseconds) Syntax for using clearTimeout: clearTimeout("timerName")

.js .html

setInterval() The window method, setInterval differs from its cousin and predecessor setTimeout in that it repeatedly calls a statement or function every so many milliseconds. Syntax: setInterval("statement", numMilliseconds) or setInterval("functionName", numMilliseconds)

See You Next Week Insha Allah