1 / 24

JavaScript: The First Parts Part Three

JavaScript: The First Parts Part Three. Douglas Crockford Yahoo! Inc. Review. average_array([1, 2, 3, 4]) // 2.5. Review. average_array([1, 2, 3, 4]) // 2.5 average_array([1, 2, 3, 4, 5]) // 3 average_array([1, 2]) // 1.5. Review. var average_array = function (array) {

redford
Download Presentation

JavaScript: The First Parts Part Three

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JavaScript:The First PartsPart Three Douglas Crockford Yahoo! Inc.

  2. Review average_array([1, 2, 3, 4]) // 2.5

  3. Review average_array([1, 2, 3, 4]) // 2.5 average_array([1, 2, 3, 4, 5]) // 3 average_array([1, 2]) // 1.5

  4. Review var average_array = function (array) { var i, total = 0; for (i = 0; i < array.length; i = i + 1) { total += array[i]; } return total / array.length; };

  5. Values Variables Expressions Branching Loops Functions Recursion Arrays Objects Trees Learn to Program

  6. Values • Booleans • Numbers • Strings • null • undefined

  7. Strings • Textual values, a sequence of 0 or more characters. • Unicode: Able to represent all of the characters of all languages. • Internally, each character is represented as a 16-bit number. • A string containing nothing is called the empty string.

  8. String Literals • Wrapped in "..." or '...'. • No line breaks. • Escapement with \. "\"" === '"'

  9. String Operators • + does concatenation 'c' + 'a' + 't' === 'cat' • The .charAt(index) method can get a character. 'cat'.charAt(1) === 'a' • The .length member gives the number of characters in the string. 'cat'.length === 3 • The .toLowerCase() method can produce a lowercase copy. 'CAT'.toLowerCase() === 'cat'

  10. http://jsmvhs.crockford.com/yellowbox.html • a = '"' • b = 'C' + 'A' + 'T' • b.length • c = b.toLowerCase() • d = c.charAt(0) • e = c.charAt(1) • f = c.charAt(3) • c = 'bad ' + c • h = '1' + 2 + 3

  11. Scope • Parameters and variables defined inside of a function are not visible outside of the function. • Functions can be created inside of other functions. • The parameters and variables of an outer function are visible to the inner function.

  12. http://jsmvhs.crockford.com/roman.html View : Page Source

  13. var roman = function () { var table = [ ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'], ['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'], ['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM'] ]; return function (n) { var result = '', i; for (i = 0; i < table.length; i += 1) { result = table[i][n % 10] + result; n = Math.floor(n / 10); } for (i = 0; i < n; i += 1) { result = 'M' + result; } return result; }; }();

  14. Debugger • Click on the bug icon on the bottom bar. • Enable / apply script debugging. • Click on the script tab. • Drag the top edge of the debugger window up to get a larger view. • Click on the line number 24 producing a red breakpoint.

  15. Debugger • Type in 1666 and press enter. • Run • Step Over • Step Into • Step Out • Close

  16. Object • A container of name value/pairs. • Objects are a useful way to collect and organize information.

  17. Object literal var my_data = { first_name: 'Douglas', last_name: 'Crockford', height: 74 }; my_data['height'] === my_data.height var name = my_data.first_name + ' ' + my_data.last_name;

  18. http://jsmvhs.crockford.com/yellowbox.html • o = {} • c = 'cat' • o[c] = 'Muldoon' • o['dog'] = 'Mutt' • o.cat • o[c] • o.dog • o.v = 5 • o.x = 10

  19. Branching • Execute part of a function depending on the value of an expression. • if (expression) { • // executed only if the expression is true • }

  20. Branching • if (expression) { • // executed only if the expression is true • } else { • // executed only if the expression is false • }

  21. Branching • if (expression1) { • // executed only if expression1 is true • } else if (expression2) { • // executed only if expression2 is true • } else if (expression3) { • // executed only if expression3 is true • } else { • // executed only if all of the expressions were false • }

  22. Branching • if (expression1) { • if (expression2) { • // executed only if expression1 • // and expression2 are true • } else { • // executed only if the expression1 is true • // and expression2 is false • } • } else { • if (expression3) { • // executed only if expression1 is false • // and expression3 is true • } else { • // executed only if the expression1 is false • // and expression3 is false • } }

  23. Branching • var min_array = function (array) { • var i, value = Infinity; • for (i = 0; i < array.length; i = i + 1) { • if (array[i] < value) { • value = array[i]; • } • } • return value; • };

  24. Assignment 3 • Write program deroman that takes a string and produces a number. • Use roman.html as a template. • Use .toLowerCase() to convert the string to lower case. • Use an object to map the Roman letters to number values. • If the value of a character is less than the value of the next character, then subtract its value from the result. • Otherwise, add its value to the result. Always add the last character's value.

More Related