JavaScript For...In Statement The for...in statement loops through the elements of an array or through the properties of an object. Syntax for (variable.

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

Pertemuan 12.  Create an array  For...In Statement  Join two arrays - concat()  Put array elements into a string - join()  Literal array - sort()
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
The Web Warrior Guide to Web Design Technologies
آموزش طراحی وب سایت جلسه یازدهم – جاوا اسکریپت 2 تدریس طراحی وب برای اطلاعات بیشتر تماس بگیرید تاو شماره تماس: پست الکترونیک :
Javascript Client-side scripting. Up to now  We've seen a little about how to control  content with HTML  presentation with CSS  Javascript is a language.
Computer Science 103 Chapter 4 Advanced JavaScript.
LOGO Document Object Model (DOM)Document Object Model (DOM) Computer Science & Engineering.
1 JavaScript/Jscript: Arrays. 2 Introduction Arrays –Data structures consisting of related data items (collections of data items) JavaScript arrays are.
1 CS101 Introduction to Computing Lecture 38 String Manipulations (Web Development Lecture 13)
1 CS101 Introduction to Computing Lecture 35 Mathematical Methods (Web Development Lecture 12)
Array Object. he following code creates an Array object called myCars: 1: var myCars=new Array(); // regular array (add an optional integer myCars[0]="Saab";
JavaScript Part 2 – Page 1 of 35CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: JavaScript Part 2.
Scripting Languages.
Loops Doing the same thing over and over and over and over and over and over…
HTML DOM.  The HTML DOM defines a standard way for accessing and manipulating HTML documents.  The DOM presents an HTML document as a tree- structure.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
Arrays – What is it? – Creation – Changing the contents Functions – What is it? – Syntax – How they work – Anonymous functions A quick lesson in Objects.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
String Object.  Conundrum: I would like people to have more time on the revisions…  … but I don't have a lot of time myself for more grading  A1 Revision.
Javascript. Outline Introduction Fundamental of JavaScript Javascript events management DOM and Dynamic HTML (DHTML)
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Javascript core objects. Review question Which of the following is TRUE concerning a variable declared globally? a. The variable cannot be accessed anywhere.
JavaScript Functions & Objects. JavaScript Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded URI.
CHAPTER 4 Java Script อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
Objects.  Java Script is an OOP Language  So it allows you to make your own objects and make your own variable types  I will not be going over how.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
CSC Programming for Science Lecture 7: Math Functions.
Keeping it Neat: Functions and JavaScript Source Files Chapter 7.
School of Computing and Information Systems CS 371 Web Application Programming Client-side.
Basic Data Types Numbers (integer and floating point)‏ Strings (sequences of characters)‏ Boolean values (true/false)‏
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
1 JavaScript
05 – Java Script (1) Informatics Department Parahyangan Catholic University.
JavaScript. JavaScript is the programming language of HTML and the Web. Easy to learn One of the 3 languages of all developers MUST learn: 1. HTML to.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
JavaScript Defined DOM (Document Object Model) General Syntax Body vs. Head Variables Math & Logic Selection Functions & Events Loops Animation Getting.
CIS 3.5 Lecture 2.3 "Introduction to JavaScript".
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
Web Development JavaScript. Introduction to JavaScript.
Hazırlayan:Emin BORANDAĞ 2/17/ Numerik Değerler Kullanımı Integers are considered accurate up to 15 digits. Try it function myFunction() { var x.
JavaScript and Ajax (JavaScript Data Types) Week 2 Web site:
JavaScript: Objects 1 © by Pearson Education, Inc. All Rights Reserved.
Tutorial 11 1 JavaScript Operators and Expressions.
CGS 3066: Web Programming and Design Spring 2016 Programming in JavaScript.
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.
OVERVIEW OF CLIENT-SIDE SCRIPTING
JavaScript and Ajax (JavaScript Environment) Week 6 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.
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.
JavaScript Introduction JavaScript is the most popular scripting language on the internet, and works in all major browsers, such as Internet Explorer,
INTERNET APPLICATIONS CPIT405 JavaScript Instructor: Rasha AlOmari
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
Build in Objects In JavaScript, almost "everything" is an object.
CGS 3066: Web Programming and Design Spring 2017
>> Introduction to JavaScript
Java Script.
14 A Brief Look at JavaScript and jQuery.
JavaScript Objects.
Pertemuan 10 JavaScript.
JavaScript Data Concepts
T. Jumana Abu Shmais – AOU - Riyadh
Pertemuan 13 JavaScript.
Pertemuan 10 JavaScript.
Tutorial 10: Programming with javascript
CGS 3066: Web Programming and Design Spring 2016
Presentation transcript:

JavaScript For...In Statement The for...in statement loops through the elements of an array or through the properties of an object. Syntax for (variable in object) { code to be executed } Note: The code in the body of the for...in loop is executed once for each element/property.

var x; var mycars = new Array(); mycars[0] = "Saab"; mycars[1] = "Volvo"; mycars[2] = "BMW"; for (x in mycars) { document.write(mycars[x] + " "); } Saab Volvo BMW

Events By using JavaScript, we have the ability to create dynamic web pages. Events are actions that can be detected by JavaScript. Every element on a web page has certain events which can trigger a JavaScript. For example, we can use the onClick event of a button element to indicate that a function will run when a user clicks on the button. We define the events in the HTML tags. Examples of events: A mouse click A web page or an image loading Mousing over a hot spot on the web page Selecting an input field in an HTML form Submitting an HTML form A keystroke Note: Events are normally used in combination with functions, and the function will not be executed before the event occurs!

JavaScript Array Object Array Object Properties PropertyDescription constructorReturns the function that created the Array object's prototype lengthSets or returns the number of elements in an array prototypeAllows you to add properties and methods to an object

Array Object Methods MethodDescription concat()Joins two or more arrays, and returns a copy of the joined arrays join()Joins all elements of an array into a string pop()Removes the last element of an array, and returns that element push()Adds new elements to the end of an array, and returns the new length reverse()Reverses the order of the elements in an array shift()Removes the first element of an array, and returns that element slice()Selects a part of an array, and returns the new array sort()Sorts the elements of an array splice()Adds/Removes elements from an array toString()Converts an array to a string, and returns the result unshift()Adds new elements to the beginning of an array, and returns the new length valueOf()Returns the primitive value of an array

var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.write("Original length: " + fruits.length); document.write(" "); fruits.length=5; document.write("New length: " + fruits.length); Original length: 4 New length: 5

var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.write(fruits.push("Kiwi") + " "); document.write(fruits.push("Lemon","Pineapple") + " "); document.write(fruits); 5 7 Banana,Orange,Apple,Mango,Kiwi,Lemon,Pineapple

var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.write(fruits.reverse()); Mango,Apple,Orange,Banana

var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.write(fruits.shift() + " "); document.write(fruits + " "); document.write(fruits.shift() + " "); document.write(fruits); Banana Orange,Apple,Mango Orange Apple,Mango

Insert Special Characters CodeOutputs \'single quote \"double quote \&ampersand \\backslash \nnew line \rcarriage return \ttab \bbackspace \fform feed

Return the length of a string Return the length of a string How to return the length of a string. var txt = "Hello World!"; document.write(txt.length); 12

Style strings Style strings How to style strings. var txt = "Hello World!"; document.write(" Big: " + txt.big() + " "); document.write(" Small: " + txt.small() + " "); document.write(" Bold: " + txt.bold() + " "); document.write(" Italic: " + txt.italics() + " "); document.write(" Fixed: " + txt.fixed() + " "); document.write(" Strike: " + txt.strike() + " "); document.write(" Fontcolor: " + txt.fontcolor("green") + " "); document.write(" Fontsize: " + txt.fontsize(6) + " "); document.write(" Subscript: " + txt.sub() + " "); document.write(" Superscript: " + txt.sup() + " "); document.write(" Link: " + txt.link(" + " "); document.write(" Blink: " + txt.blink() + " (does not work in IE, Chrome, or Safari) ");

The toLowerCase() and toUpperCase() methods The toLowerCase() and toUpperCase() methods How to convert a string to lowercase or uppercase letters. var txt="Hello World!"; document.write(txt.toLowerCase() + " "); document.write(txt.toUpperCase()); hello world! HELLO WORLD!

The indexOf() method The indexOf() method How to return the position of the first found occurrence of a specified value in a string. var str="Hello world!"; document.write(str.indexOf("d") + " "); document.write(str.indexOf("WORLD") + " "); document.write(str.indexOf("world"));

What is an Array? An array is a special variable, which can hold more than one value, at a time. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this: cars1="Saab"; cars2="Volvo"; cars3="BMW";

Create an Array An array can be defined in three ways. The following code creates an Array object called myCars: 1: var myCars=new Array(); // regular array (add an optional integer myCars[0]="Saab"; // argument to control array's size) myCars[1]="Volvo"; myCars[2]="BMW"; 2: var myCars=new Array("Saab","Volvo","BMW"); // condensed array 3: var myCars=["Saab","Volvo","BMW"]; // literal array

Access an Array You can refer to a particular element in an array by referring to the name of the array and the index number. The index number starts at 0. The following code line : document.write(myCars[0]); will result in the following output: Saab

Modify Values in an Array To modify a value in an existing array, just add a new value to the array with a specified index number: myCars[0]="Opel"; Now, the following code line: document.write(myCars[0]); will result in the following output: Opel

Create a Boolean Object The Boolean object represents two values: "true" or "false". The following code creates a Boolean object called myBoolean: var myBoolean=new Boolean(); Note: If the Boolean object has no initial value or if it is 0, -0, null, "", false, undefined, or NaN, the object is set to false. Otherwise it is true (even with the string "false")! All the following lines of code create Boolean objects with an initial value of false: var myBoolean=new Boolean(); var myBoolean=new Boolean(0); var myBoolean=new Boolean(null); var myBoolean=new Boolean(""); var myBoolean=new Boolean(false); var myBoolean=new Boolean(NaN);

var b1=new Boolean( 0); var b2=new Boolean(1); var b3=new Boolean(""); var b4=new Boolean(null); var b5=new Boolean(NaN); var b6=new Boolean("false"); document.write("0 is boolean "+ b1 +" "); document.write("1 is boolean "+ b2 +" "); document.write("An empty string is boolean "+ b3 + " "); document.write("null is boolean "+ b4+ " "); document.write("NaN is boolean "+ b5 +" "); document.write("The string 'false' is boolean "+ b6 +" "); 0 is boolean false 1 is boolean true An empty string is boolean false null is boolean false NaN is boolean false The string 'false' is boolean true

round() round() How to use round(). random() random() How to use random() to return a random number between 0 and 1. max() max() How to use max() to return the number with the highest value of two specified numbers. min() min() How to use min() to return the number with the lowest value of two specified numbers.

document.write(Math.round(0.60) + " "); document.write(Math.round(0.50) + " "); document.write(Math.round(0.49) + " "); document.write(Math.round(-4.40) + " "); document.write(Math.round(-4.60));

//return a random number between 0 and 1 document.write(Math.random() + " "); //return a random integer between 0 and 10 document.write(Math.floor(Math.random()*11));

document.write(Math.max(5,10) + " "); document.write(Math.max(0,150,30,20,38) + " "); document.write(Math.max(-5,10) + " "); document.write(Math.max(-5,-10) + " "); document.write(Math.max(1.5,2.5));

document.write(Math.min(5,10) + " "); document.write(Math.min(0,150,30,20,38) + " "); document.write(Math.min(-5,10) + " "); document.write(Math.min(-5,-10) + " "); document.write(Math.min(1.5,2.5));

Math Object Properties PropertyDescription EReturns Euler's number (approx ) LN2Returns the natural logarithm of 2 (approx ) LN10Returns the natural logarithm of 10 (approx ) LOG2EReturns the base-2 logarithm of E (approx ) LOG10EReturns the base-10 logarithm of E (approx ) PIReturns PI (approx ) SQRT1_2Returns the square root of 1/2 (approx ) SQRT2Returns the square root of 2 (approx )

Math Object Methods MethodDescription abs(x)Returns the absolute value of x acos(x)Returns the arccosine of x, in radians asin(x)Returns the arcsine of x, in radians atan(x)Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians atan2(y,x)Returns the arctangent of the quotient of its arguments ceil(x)Returns x, rounded upwards to the nearest integer cos(x)Returns the cosine of x (x is in radians) exp(x)Returns the value of E x floor(x)Returns x, rounded downwards to the nearest integer log(x)Returns the natural logarithm (base E) of x max(x,y,z,...,n)Returns the number with the highest value min(x,y,z,...,n)Returns the number with the lowest value pow(x,y)Returns the value of x to the power of y random()Returns a random number between 0 and 1 round(x)Rounds x to the nearest integer sin(x)Returns the sine of x (x is in radians) sqrt(x)Returns the square root of x tan(x)Returns the tangent of an angle