Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "JavaScript: The First Parts Part Five Douglas Crockford Yahoo! Inc."— Presentation transcript:

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

2 var wordify = function () { var units = [ '', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen' ]; var tens = [ 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety' ]; var illions = [ '', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion', 'decillion' ]; return function (n) { var result = '', i, clump, hundred, ten, unit, word, illion; n = Math.floor(+n); if (!n) { return 'zero'; } for (i = 0; i < illions.length; i += 1) { clump = n % 1000; if (clump) { hundred = Math.floor(n / 100); unit = clump - (hundred * 100); illion = illions[i]; word = units[unit]; if (typeof word !== 'string') { ten = Math.floor(unit / 10); unit = unit % 10; word = units[unit]; if (word) { word = ' ' + word; } word = tens[ten] + word; } if (hundred) { if (word) { word = ' ' + word; } word = units[hundred] + ' hundred' + word; } if (result) { result = ' ' + result; } result = word + result; } n = Math.floor(n / 1000); if (n === 0) { break; } return result; }; }();

3 var wordify = function () { // The wordify function converts a positive integer into English words. // The units array contains the names of the digits 0-9 as well as the // teens which are somewhat irregular in their construction. The other // numbers below one hundred are not included here because they are // regular and easily constructed. var units = [ '', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen' ]; var tens = [ 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety' ]; var illions = [ '', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion', 'decillion' ]; return function (n) { var result = '', i, clump, hundred, ten, unit, word, illion; n = Math.floor(+n); if (!n) { return 'zero'; } for (i = 0; i < illions.length; i += 1) { clump = n % 1000; if (clump) { hundred = Math.floor(n / 100); unit = clump - (hundred * 100); illion = illions[i]; word = units[unit]; if (typeof word !== 'string') { ten = Math.floor(unit / 10); unit = unit % 10; word = units[unit]; if (word) { word = ' ' + word; } word = tens[ten] + word; } if (hundred) { if (word) { word = ' ' + word; } word = units[hundred] + ' hundred' + word; } if (result) { result = ' ' + result; } result = word + result; } n = Math.floor(n / 1000); if (n === 0) { break; } return result; }; }();

4 var wordify = function () { // The wordify function converts a positive integer into English words. // The units array contains the names of the digits 0-9 as well as the // teens which are somewhat irregular in their construction. The other // numbers below one hundred are not included here because they are // regular and easily constructed. var units = [ '', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen' ]; var tens = [ '', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety' ]; var illions = [ '', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion', 'decillion' ]; return function (n) { var result = '', i, clump, hundred, ten, unit, word, illion; n = Math.floor(+n); if (!n) { return 'zero'; } for (i = 0; i < illions.length; i += 1) { clump = n % 1000; if (clump) { hundred = Math.floor(n / 100); unit = clump - (hundred * 100); illion = illions[i]; word = units[unit]; if (typeof word !== 'string') { ten = Math.floor(unit / 10); unit = unit % 10; word = units[unit]; if (word) { word = ' ' + word; } word = tens[ten] + word; } if (hundred) { if (word) { word = ' ' + word; } word = units[hundred] + ' hundred' + word; } if (result) { result = ' ' + result; } result = word + result; } n = Math.floor(n / 1000); if (n === 0) { break; } return result; }; }();

5 var wordify = function () { // The wordify function converts a positive integer into English words. // The units array contains the names of the digits 0-9 as well as the // teens which are somewhat irregular in their construction. The other // numbers below one hundred are not included here because they are // regular and easily constructed. var units = [ '', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen' ]; // The tens array contains the names of the tens. var tens = [ '', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety' ]; var illions = [ '', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion', 'decillion' ]; return function (n) { var result = '', i, clump, hundred, ten, unit, word, illion; n = Math.floor(+n); if (!n) { return 'zero'; } for (i = 0; i < illions.length; i += 1) { clump = n % 1000; if (clump) { hundred = Math.floor(n / 100); unit = clump - (hundred * 100); illion = illions[i]; word = units[unit]; if (typeof word !== 'string') { ten = Math.floor(unit / 10); unit = unit % 10; word = units[unit]; if (word) { word = ' ' + word; } word = tens[ten] + word; } if (hundred) { if (word) { word = ' ' + word; } word = units[hundred] + ' hundred' + word; } if (result) { result = ' ' + result; } result = word + result; } n = Math.floor(n / 1000); if (n === 0) { break; } return result; }; }();

6 // The illions contains the names of the powers of thousand. Note: // the largest safe integer is about 9 quadrillion, so it may be // pointless to pretend to handle numbers up to the decillions. var illions = [ '', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion', 'decillion' ]; return function (n) { var result = '', i, clump, hundred, ten, unit, word, illion; n = Math.floor(+n); if (!n) { return 'zero'; } for (i = 0; i < illions.length; i += 1) { clump = n % 1000; if (clump) { hundred = Math.floor(n / 100); unit = clump - (hundred * 100); illion = illions[i]; word = units[unit]; if (typeof word !== 'string') { ten = Math.floor(unit / 10); unit = unit % 10; word = units[unit]; if (word) { word = ' ' + word; } word = tens[ten] + word; } if (hundred) { if (word) { word = ' ' + word; } word = units[hundred] + ' hundred' + word; } if (result) { result = ' ' + result; } result = word + result; } n = Math.floor(n / 1000); if (n === 0) { break; } return result; }; }();

7 // The illions contains the names of the powers of thousand. Note: // the largest safe integer is about 9 quadrillion, so it may be // pointless to pretend to handle numbers up to the decillions. var illions = [ '', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion', 'decillion' ]; // Return the actual wordify function. It takes a number as its argument. return function (n) { var result = '', i, clump, hundred, ten, unit, word, illion; n = Math.floor(+n); if (!n) { return 'zero'; } for (i = 0; i < illions.length; i += 1) { clump = n % 1000; if (clump) { hundred = Math.floor(n / 100); unit = clump - (hundred * 100); illion = illions[i]; word = units[unit]; if (typeof word !== 'string') { ten = Math.floor(unit / 10); unit = unit % 10; word = units[unit]; if (word) { word = ' ' + word; } word = tens[ten] + word; } if (hundred) { if (word) { word = ' ' + word; } word = units[hundred] + ' hundred' + word; } if (result) { result = ' ' + result; } result = word + result; } n = Math.floor(n / 1000); if (n === 0) { break; } return result; }; }();

8 // The illions contains the names of the powers of thousand. Note: // the largest safe integer is about 9 quadrillion, so it may be // pointless to pretend to handle numbers up to the decillions. var illions = [ '', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion', 'decillion' ]; // Return the actual wordify function. It takes a number as its argument. return function (n) { var result = '', // The string containing the result i, // The loop counter clump, // The number representing 3 digits hundred, // The number in the hundreds place ten, // The number in the tens place unit, // The number in the units place word, // The partial result illion; // The name of the power of thousand n = Math.floor(+n); if (!n) { return 'zero'; } for (i = 0; i < illions.length; i += 1) { clump = n % 1000; if (clump) { hundred = Math.floor(n / 100); unit = clump - (hundred * 100); illion = illions[i]; word = units[unit]; if (typeof word !== 'string') { ten = Math.floor(unit / 10); unit = unit % 10; word = units[unit]; if (word) { word = ' ' + word; } word = tens[ten] + word; } if (hundred) { if (word) { word = ' ' + word; } word = units[hundred] + ' hundred' + word; } if (result) { result = ' ' + result; } result = word + result; } n = Math.floor(n / 1000); if (n === 0) { break; } return result; }; }();

9 // Force n to be an integer. n = Math.floor(+n); if (!n) { return 'zero'; } for (i = 0; i < illions.length; i += 1) { clump = n % 1000; if (clump) { hundred = Math.floor(n / 100); unit = clump - (hundred * 100); illion = illions[i]; word = units[unit]; if (typeof word !== 'string') { ten = Math.floor(unit / 10); unit = unit % 10; word = units[unit]; if (word) { word = ' ' + word; } word = tens[ten] + word; } if (hundred) { if (word) { word = ' ' + word; } word = units[hundred] + ' hundred' + word; } if (result) { result = ' ' + result; } result = word + result; } n = Math.floor(n / 1000); if (n === 0) { break; } return result; }; }();

10 // Force n to be an integer. n = Math.floor(+n); // If n is zero, return the string 'zero'. if (!n) { return 'zero'; } for (i = 0; i < illions.length; i += 1) { clump = n % 1000; if (clump) { hundred = Math.floor(n / 100); unit = clump - (hundred * 100); illion = illions[i]; word = units[unit]; if (typeof word !== 'string') { ten = Math.floor(unit / 10); unit = unit % 10; word = units[unit]; if (word) { word = ' ' + word; } word = tens[ten] + word; } if (hundred) { if (word) { word = ' ' + word; } word = units[hundred] + ' hundred' + word; } if (result) { result = ' ' + result; } result = word + result; } n = Math.floor(n / 1000); if (n === 0) { break; } return result; }; }();

11 // Force n to be an integer. n = Math.floor(+n); // If n is zero, return the string 'zero'. if (!n) { return 'zero'; } // Loop for each of the illions for (i = 0; i < illions.length; i += 1) { clump = n % 1000; if (clump) { hundred = Math.floor(n / 100); unit = clump - (hundred * 100); illion = illions[i]; word = units[unit]; if (typeof word !== 'string') { ten = Math.floor(unit / 10); unit = unit % 10; word = units[unit]; if (word) { word = ' ' + word; } word = tens[ten] + word; } if (hundred) { if (word) { word = ' ' + word; } word = units[hundred] + ' hundred' + word; } if (result) { result = ' ' + result; } result = word + result; } n = Math.floor(n / 1000); if (n === 0) { break; } return result; }; }();

12 // Loop for each of the illions for (i = 0; i < illions.length; i += 1) { // Get the bottom clump of 3 digits. clump = n % 1000; // If the clump is not zero, then the clump will contribute words to the // result. if (clump) { hundred = Math.floor(n / 100); unit = clump - (hundred * 100); illion = illions[i]; word = units[unit]; if (typeof word !== 'string') { ten = Math.floor(unit / 10); unit = unit % 10; word = units[unit]; if (word) { word = ' ' + word; } word = tens[ten] + word; } if (hundred) { if (word) { word = ' ' + word; } word = units[hundred] + ' hundred' + word; } if (result) { result = ' ' + result; } result = word + result; } n = Math.floor(n / 1000); if (n === 0) { break; } return result; }; }();

13 // Loop for each of the illions for (i = 0; i < illions.length; i += 1) { // Get the bottom clump of 3 digits. clump = n % 1000; // If the clump is not zero, then the clump will contribute words to the // result. if (clump) { // Get the most significant of the three digits in the clump. hundred = Math.floor(clump / 100); unit = clump - (hundred * 100); illion = illions[i]; word = units[unit]; if (typeof word !== 'string') { ten = Math.floor(unit / 10); unit = unit % 10; word = units[unit]; if (word) { word = ' ' + word; } word = tens[ten] + word; } if (hundred) { if (word) { word = ' ' + word; } word = units[hundred] + ' hundred' + word; } if (result) { result = ' ' + result; } result = word + result; } n = Math.floor(n / 1000); if (n === 0) { break; } return result; }; }();

14 // Loop for each of the illions for (i = 0; i < illions.length; i += 1) { // Get the bottom clump of 3 digits. clump = n % 1000; // If the clump is not zero, then the clump will contribute words to the // result. if (clump) { // Get the most significant of the three digits in the clump. hundred = Math.floor(clump / 100); // Get the remaining digits in the clump. unit = clump - (hundred * 100); // clump % 100 illion = illions[i]; word = units[unit]; if (typeof word !== 'string') { ten = Math.floor(unit / 10); unit = unit % 10; word = units[unit]; if (word) { word = ' ' + word; } word = tens[ten] + word; } if (hundred) { if (word) { word = ' ' + word; } word = units[hundred] + ' hundred' + word; } if (result) { result = ' ' + result; } result = word + result; } n = Math.floor(n / 1000); if (n === 0) { break; } return result; }; }();

15 // Get the remaining digits in the clump. unit = clump - (hundred * 100); // Get the name of the power of thousand. illion = illions[i]; // Get the name of the unit part and put it into word. // If there isn't a name, then the value of word will be undefined. word = units[unit]; if (typeof word !== 'string') { ten = Math.floor(unit / 10); unit = unit % 10; word = units[unit]; if (word) { word = ' ' + word; } word = tens[ten] + word; } if (hundred) { if (word) { word = ' ' + word; } word = units[hundred] + ' hundred' + word; } if (result) { result = ' ' + result; } result = word + result; } n = Math.floor(n / 1000); if (n === 0) { break; } return result; }; }();

16 // Get the name of the unit part and put it into word. // If there isn't a name, then the value of word will be undefined. word = units[unit]; // If word does not contain a string, then extract the tens digit and // the units digit. if (typeof word !== 'string') { ten = Math.floor(unit / 10); unit = unit % 10; word = units[unit]; // If the unit is not zero, add its name to the word. if (word) { word = ' ' + word; } // Add the tens to the word. word = tens[ten] + word; } if (hundred) { if (word) { word = ' ' + word; } word = units[hundred] + ' hundred' + word; } if (result) { result = ' ' + result; } result = word + result; } n = Math.floor(n / 1000); if (n === 0) { break; } return result; }; }();

17 // If the hundred digit is not zero, then add the hundred's text to the // word, including the word 'hundred' and spaces where needed. if (hundred) { if (word) { word = ' ' + word; } word = units[hundred] + ' hundred' + word; } if (result) { result = ' ' + result; } result = word + result; } n = Math.floor(n / 1000); if (n === 0) { break; } return result; }; }();

18 // If the result is not an empty string, then add a space to it to // separate the previous result from the contribution of this clump. if (result) { result = ' ' + result; } // Add the result of this clump to the result. result = word + result; } n = Math.floor(n / 1000); if (n === 0) { break; } return result; }; }();

19 // If the result is not an empty string, then add a space to it to // separate the previous result from the contribution of this clump. if (result) { result = ' ' + result; } // If the power of thousands has a name, include it. if (illion) { word += ' ' + illion; } // Add the result of this clump to the result. result = word + result; } n = Math.floor(n / 1000); if (n === 0) { break; } return result; }; }();

20 // Divide n by a thousand to remove the current clump. That sets the // loop up to handle the next clump. n = Math.floor(n / 1000); // If n went to zero, then there is no need to test the remaining powers of // thousand. We can finish early. if (n === 0) { break; } // That completes the loop. } // Return the result. (This is a useless comment.) return result; }; }();

21 // Divide n by a thousand to remove the current clump. That sets the // loop up to handle the next clump. n = Math.floor(n / 1000); // If n went to zero, then there is no need to test the remaining powers of // thousand. We can finish early. if (n === 0) { break; } // That completes the loop. } return result; }; // Execute the outer function. }();

22 Wrapping a loop in a function var reduce = function (array, func) { var i, length = array.length, result = array[0]; for (i = 1; i < length; i += 1) { result = func(result, array[i]) } return result; }; total = reduce([1,2,3,4,5], function (a, b) { return a + b; }); product = reduce([1,2,3,4,5], function (a, b) { return a * b; });

23 Recursive Function var reduce = function (array, func) { var i = 0, length = array.length, step; step = function (result) { i += 1; if (i < length) { return step(func(result, array[i])); } else { return result; } }; return step(array[0]); };

24 Average var average = function (array) { var total = reduce(array, function (a, b) { return a + b; }); return total / array.length; }; mean = average([1,2,3,4,5]);

25 Map var map = function (array, func) { var i, length = array.length, result = []; for (i = 0; i < length; i += 1) { result[i] = func(array[i]); } return result; }; doubled = map([1,2,3,4,5], function (a) { return a * 2; });

26 A little database var database = [{ aka: "burds.b", id: "kladenheim", gender: "f", lucky: 3 }, { aka: "corevette", id: "dewqazxc86474", gender: "m", lucky: 42 }, { aka: "hopdude", id: "sergemerov", gender: "m", lucky: 8 }, { aka: "kaeru", id: "leslie.hamachi", gender: "f", lucky: 4 }, { aka: "lmperry", id: "lmperry11", gender: "f", lucky: 7 }, { aka: "minchew", id: "minchew01", gender: "f", lucky: 14 }, { aka: "moongodess", id: "catherinex06", gender: "f", lucky: 20 }, { aka: "necroskiss", id: "pigganon", gender: "f", lucky: 12 }];

27 Select var ids = map(database, function (record) { return record.id; }); var select = function (database, name) { return map(database, function (record) { return record[name]; }); }; var ids = select(database, 'id');

28 Filter var filter = function (array, func) { var i, length = array.length, result = []; for (i = 0; i < length; i += 1) { if (func(array[i])) { result.push(array[i]); } return result; }; var girls = filter(database, function (record) { return record.gender === 'f'; });

29 Where var boys = filter(database, function (record) { return record.gender === 'm'; }); var where = function (array, name, value) { return filter(array, function (record) { return record[name] === value; }); }; boys = where(database, 'gender', 'm');

30 Average girls' lucky number average_girls_lucky_number = average(select(where( database, 'gender', 'f'), 'lucky'));

31 Assignments Read Chapter 2. Due next week: Adapt wordify to another language. The program should be fully commented. We will read all of the programs in class.


Download ppt "JavaScript: The First Parts Part Five Douglas Crockford Yahoo! Inc."

Similar presentations


Ads by Google