Download presentation

Presentation is loading. Please wait.

Published byScott Barner Modified about 1 year ago

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

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

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

4
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]; }

5
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]; }

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

7
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.

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

9
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.

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

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

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

13
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 }

14
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; }

15
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; };

16
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; }; }();

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

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

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

20
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.

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google