Presentation 7: JavaScript continued Control structures and functions Fundamentals of Web-Centric Development.

Slides:



Advertisements
Similar presentations
4 Control Statements: Part 1.
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to XHTML Programming the World Wide Web Fourth edition.
Advanced Piloting Cruise Plot.
1
Feichter_DPG-SYKL03_Bild-01. Feichter_DPG-SYKL03_Bild-02.
Slide 1 Insert your own content. Slide 2 Insert your own content.
© 2008 Pearson Addison Wesley. All rights reserved Chapter Seven Costs.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Chapter 1 The Study of Body Function Image PowerPoint
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
ALGEBRA Number Walls
Algebraic Expressions
UNITED NATIONS Shipment Details Report – January 2006.
1 RA I Sub-Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Casablanca, Morocco, 20 – 22 December 2005 Status of observing programmes in RA I.
Exit a Customer Chapter 8. Exit a Customer 8-2 Objectives Perform exit summary process consisting of the following steps: Review service records Close.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Year 6 mental test 10 second questions
Excel Functions. Part 1. Introduction 2 An Excel function is a formula or a procedure that is performed in the Visual Basic environment, outside the.
Solve Multi-step Equations
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
Turing Machines.
PP Test Review Sections 6-1 to 6-6
Chapter 17 Linked Lists.
EU market situation for eggs and poultry Management Committee 20 October 2011.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Security Panel Application Introducing.
2 |SharePoint Saturday New York City
Green Eggs and Ham.
IP Multicast Information management 2 Groep T Leuven – Information department 2/14 Agenda •Why IP Multicast ? •Multicast fundamentals •Intradomain.
1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight.
VOORBLAD.
Name Convolutional codes Tomashevich Victor. Name- 2 - Introduction Convolutional codes map information to code bits sequentially by convolving a sequence.
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
1 RA III - Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Buenos Aires, Argentina, 25 – 27 October 2006 Status of observing programmes in RA.
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
© 2012 National Heart Foundation of Australia. Slide 2.
LO: Count up to 100 objects by grouping them and counting in 5s 10s and 2s. Mrs Criddle: Westfield Middle School.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
1 of 31 Images from Africa. 2 of 31 My little Haitian friend Antoine (1985)
1 of 32 Images from Africa. 2 of 32 My little Haitian friend Antoine (1985)
Model and Relationships 6 M 1 M M M M M M M M M M M M M M M M
25 seconds left…...
Januar MDMDFSSMDMDFSSS
Analyzing Genes and Genomes
Chapter 9 Interactive Multimedia Authoring with Flash Introduction to Programming 1.
Types of selection structures
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Essential Cell Biology
Intracellular Compartments and Transport
PSSA Preparation.
Essential Cell Biology
Immunobiology: The Immune System in Health & Disease Sixth Edition
1 Chapter 13 Nuclear Magnetic Resonance Spectroscopy.
Energy Generation in Mitochondria and Chlorplasts
Murach’s OS/390 and z/OS JCLChapter 16, Slide 1 © 2002, Mike Murach & Associates, Inc.
CpSc 3220 Designing a Database
User Defined Functions Lesson 1 CS1313 Fall User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough #1.
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
Presentation transcript:

Presentation 7: JavaScript continued Control structures and functions Fundamentals of Web-Centric Development

Ingeniørhøjskolen i Århus Slide 2 af 15 This presentation Overview of JavaScript’s support of –Variables –Operators –Control structures –Functions Different examples Comments to students –From previous programming experience you will know most of it –Ex. In this course it is presumed that you now about algorithms recursion, pseudo code, control structures, loop in general and boolean expressions etc

Ingeniørhøjskolen i Århus Slide 3 af 15 Variable Loosely-typed language Types –Number, Boolean, String, Function, Object Variable declared by –var [= ] –Notice: no type to the var Scope –Global and local –Like other typical programming language (Nearest scope) –Local variables leaves scope and disappears

Ingeniørhøjskolen i Århus Slide 4 af 15 Operators Arithmetic's +, -, *, /, %, +, +=, … Equations ==, !=, NOTICE NEVER USE “=“ in equations Boolean && = AND, || = OR, ! = NOT Strings Concatenating of strings ”alfa”+”beta” = ”alfabeta”

Ingeniørhøjskolen i Århus Slide 5 af 15 Control structures Like other programming languages: –If-else if (<>) { … } else { … } –Switch-case switch ( ) { case : … break; … default: … }

Ingeniørhøjskolen i Århus Slide 6 af 15 Loops More control structures –For loop for (var counter=0; counter<list.length; counter++) { … use a counter. counter for indexing an array. } –While loop while (list.next()) { … } –Do/while loop (at least one loop) do { … } while { … }

Ingeniørhøjskolen i Århus Slide 7 af 15 Functions Used for: –Structuring and encapsulating functionality –Event handling (more when DHTML) –Example: calculations –Example: validating of user input HTML element <FORM.. Declaration: function oneAddition (x, y) { var sum = x+y; return(sum); } Invoking: var sumRetur = oneAddition(3, 4) ; Objects following presentation.

Ingeniørhøjskolen i Århus Slide 8 af 15 Examples Some Examples using functions and control structures in XHTML

Ingeniørhøjskolen i Århus Slide 9 af 15 Average.html 1 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 " Class Average Program <!-- 14 var total, // sum of grades 15 gradeCounter, // number of grades entered 16 gradeValue, // grade value 17 average, // average of all grades 18 grade; // grade typed by user // Initialization Phase 21 total = 0; // clear total 22 gradeCounter = 1; // prepare to loop // Processing Phase 25 while ( gradeCounter <= 10 ) { // loop 10 times // prompt for input and read grade from user 28 grade = window.prompt( "Enter integer grade:", "0" ); // convert grade from a string to an integer 31 gradeValue = parseInt( grade ); // add gradeValue to total 34 total = total + gradeValue; 35 The while loop will execute the statements in the body of the loop until the value of gradeCounter equals 10. Prompt for the user input a grade.Convert input to an integer. Add new grade to total.

Ingeniørhøjskolen i Århus Slide 10 af 15 Average.html Program Output 36 // add 1 to gradeCounter 37 gradeCounter = gradeCounter + 1; 38 } // Termination Phase 41 average = total / 10; // calculate the average // display average of exam grades 44 document.writeln( 45 " Class average is " + average + " " ); 46 // --> Click Refresh (or Reload) to run the script again Increment the counter. Calculate the average of the grades input by the user. Write the result to the XHTML document.

Ingeniørhøjskolen i Århus Slide 11 af 15 ForCounter.html 1 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 " Counter-Controlled Repetition <!-- 14 // Initialization, repetition condition and 15 // incrementing are all included in the for 16 // structure header. 17 for ( var counter = 1; counter <= 7; ++counter ) 18 document.writeln( "<p style = \"font-size: " + 19 counter + "ex\">XHTML font size " + counter + 20 "ex " ); 21 // --> InitializationRepetition conditionIncrementing

Ingeniørhøjskolen i Århus Slide 12 af 15 Program Output

Ingeniørhøjskolen i Århus Slide 13 af 15 BreakTest.html 1 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 " Using the break Statement in a for Structure <!-- 16 for ( var count = 1; count <= 10; ++count ) { 17 if ( count == 5 ) 18 break; // break loop only if count == document.writeln( "Count is: " + count + " " ); 21 } document.writeln( 24 "Broke out of loop at count = " + count ); 25 // --> When the value of variable count equals 5, the break statement causes program control to proceed to the first line outside the for loop.

Ingeniørhøjskolen i Århus Slide 14 af 15 Functions Examples –Calculations –FORM Data validating (DHTML)

Ingeniørhøjskolen i Århus Slide 15 af 15 SquareInt.htm 1 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 " A Programmer-Defined square Function <!-- 14 document.writeln( 15 " Square the numbers from 1 to 10 " ); // square the numbers from 1 to for ( var x = 1; x <= 10; ++x ) 19 document.writeln( "The square of " + x + " is " + 20 square( x ) + " " ); // The following square function's body is executed 23 // only when the function is explicitly called // square function definition 26 function square( y ) 27 { 28 return y * y; 29 } 30 // --> Calling function square and passing it the value of x.Variable y gets the value of variable x. The return statement passes the value of y * y back to the calling function.