JavaScript: The First Parts Part Four Douglas Crockford Yahoo! Inc.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight.
COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Computer Science 103 Chapter 3 Introduction to JavaScript.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
The Programming Discipline Professor Stephen K. Kwan 2010 Things you need to know (learn) for developing large computer programs.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
1 Chapter 5 File Objects and Looping Statements (Some slides have been modified from their original format)
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
THE BIG PICTURE. How does JavaScript interact with the browser?
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
JavaScript: Control Structures September 27, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel,
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
Control Structures Repetition or Iteration or Looping Part II.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Debugging Xin Tong. GDB GNU Project debugger Allows you to see what is going on `inside' another program while it executes or crashed. (Faster than printing.
Repetition and Iteration ANSI-C. Repetition We need a control instruction to allows us to execute an statement or a set of statements as many times as.
Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.
JavaScript: The First Parts Part Three Douglas Crockford Yahoo! Inc.
JavaScript: The First Parts Part Eight Douglas Crockford Yahoo! Inc.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops.
Chapter 2 Murach's JavaScript and jQuery, C2© 2012, Mike Murach & Associates, Inc.Slide 1.
IS2802 Introduction to Multimedia Applications for Business Lecture 4: JavaScript, Loops, and Conditional Statements Rob Gleasure
JavaScript: The First Parts Part Two Douglas Crockford Yahoo! Inc.
CMSC 150 LOOPS CS 150: Fri 20 Jan Representing DNA AGTCCAGTGTCAA.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Repetition Looping. Types for while do..while for The for loop allows you to iterate through a variable for a specific range of values. You must supply.
Learning Javascript From Mr Saem
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
FOP: While Loops.
Repeating the Execution of Statements
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Loop Structures.
JavaScript Syntax and Semantics
JavaScript: Control Statements I
Incrementing ITP © Ron Poet Lecture 8.
Barb Ericson Georgia Institute of Technology Dec 2009
Iteration with While You can say that again.
Using variables, for..loop and functions to help organize your code
Basketball Drill Programming Alternatives
DEBUGGING JAVA PROGRAMS USING ECLIPSE DEBUGGER
The Web Wizard’s Guide To JavaScript
The Internet 11/29/11 Functions
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Lecture 1. Program Surgery
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
Python While Loops.
Presentation transcript:

JavaScript: The First Parts Part Four Douglas Crockford Yahoo! Inc.

Review var value = { i: 1, v: 5, x: 10, l: 50, c: 100, d: 500, m: 1000 };

Review var i, letter, result = 0; string = string.toLowerCase(); for (i = 0; i < string.length; i += 1) { letter = string.charAt(i); result += value[letter]; }

Review var i, letter, next, result = 0; string = string.toLowerCase(); for (i = 0; i < string.length; i += 1) { letter = string.charAt(i); next = string.charAt(i + 1); if (next === '') { result += value[letter]; } else if (value[next] > value[letter]) { result -= value[letter]; } else { result += value[letter]; }

Review var i, letter, next, result = 0; string = string.toLowerCase(); for (i = 0; i < string.length; i += 1) { letter = string.charAt(i); next = string.charAt(i + 1); if (next === '' || value[next] <= value[letter]) { result += value[letter]; } else { result -= value[letter]; }

Expressions and Variables result = table[i][n % 10] + result; subtable = table[i]; digit = n % 10; roman = subtable[digit]; result = roman + result;

Expressions and Variables Generally, small programs are better than large programs. Simple programs are better than complicated programs. Keep intermediate results in variables If they are used more than once If it makes the program more legible If it is useful for debugging.

Looping JavaScript does not have a loop statement. loop { body } It would execute the body repeatedly forever.

break JavaScript has a break statement. It can cause an exit from a loop. loop { statements if ( expression ) { break; } statements } Execution resumes with the first statement after the loop.

while while ( condition ) { body } loop { if (! condition ) { break; } body }

for for ( initialize ; condition ; increment ) { body } initialize ; loop { if (! condition ) { break; } body increment ; }

do do { body } while ( condition ); loop { body if (! condition ) { break; }

loop Since JavaScript does not have a loop statement, if you need to write a loop that exits from the middle, use while (true) { statements if ( expression ) { break; } statements }

factor var factor = function (n) { var candidate = 2, result = []; while (candidate <= n) { if (n / candidate === Math.floor(n / candidate)) { result.push(candidate); n = n / candidate; } else { if (candidate === 2) { candidate = 3; } else { candidate += 2; } result.push(n); return result; }

factor var factor = function (n) { var candidate = 2, quotient, result = []; while (candidate <= Math.sqrt(n)) { quotient = n / candidate; if (quotient === Math.floor(quotient)) { result.push(candidate); n = quotient; } else { if (candidate === 2) { candidate = 3; } else { candidate += 2; } result.push(n); return result; };

var factor = function () { var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 31, 37]; return function (n) { var candidate, p = 1, quotient, result = [], sqrt_n = Math.sqrt(n); candidate = primes[0]; while (candidate <= sqrt_n) { quotient = n / candidate; if (quotient === Math.floor(quotient)) { result.push(candidate); n = quotient; sqrt_n = Math.sqrt(n); } else { if (p < primes.length) { candidate = primes[p]; p += 1; } else { candidate += 2; } result.push(n); return result; }; }();

Assignment Operators a = a + 1; a = a - 1; a = a * 1; a = a / 1; a += 1; a -= 1; a *= 1; a /= 1;

debugger The debugger statement is a programmable breakpoint. if (something interesting is about to happen) { debugger; }

JSLint Code quality tool. It can find undeclared variables and other problems.

Assignment 4 Go to /Templates/ and get bug.txt It contains an html file that contains a program that contains three bugs. Analyze and document the program, adding // comments to describe it workings. Correct the bugs.