JavaScript: The First Parts Part Five - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

JavaScript: The First Parts Part Five

Description:

JavaScript: The First Parts Part Five Douglas Crockford Yahoo! Inc. – PowerPoint PPT presentation

Number of Views:88
Avg rating:3.0/5.0
Slides: 32
Provided by: Dougla292
Category:

less

Transcript and Presenter's Notes

Title: JavaScript: The First Parts Part Five


1
JavaScript The First PartsPart 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'

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'

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'

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

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 lt illions.length i 1)

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'

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

9
  • // Force n to be an integer.
  • n Math.floor(n)
  • if (!n)
  • return 'zero'
  • for (i 0 i lt illions.length i 1)
  • clump n 1000
  • if (clump)
  • hundred Math.floor(n / 100)
  • unit clump - (hundred 100)
  • illion illionsi
  • word unitsunit
  • if (typeof word ! 'string')
  • ten Math.floor(unit / 10)
  • unit unit 10
  • word unitsunit
  • if (word)

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 lt illions.length i 1)
  • clump n 1000
  • if (clump)
  • hundred Math.floor(n / 100)
  • unit clump - (hundred 100)
  • illion illionsi
  • word unitsunit
  • if (typeof word ! 'string')
  • ten Math.floor(unit / 10)

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 lt illions.length i 1)
  • clump n 1000
  • if (clump)
  • hundred Math.floor(n / 100)
  • unit clump - (hundred 100)
  • illion illionsi
  • word unitsunit

12
  • // Loop for each of the illions
  • for (i 0 i lt 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 illionsi
  • word unitsunit
  • if (typeof word ! 'string')
  • ten Math.floor(unit / 10)
  • unit unit 10

13
  • // Loop for each of the illions
  • for (i 0 i lt 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 illionsi
  • word unitsunit

14
  • // Loop for each of the illions
  • for (i 0 i lt 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.

15
  • // Get the remaining digits in the clump.
  • unit clump - (hundred 100)
  • // Get the name of the power of thousand.
  • illion illionsi
  • // 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 unitsunit
  • if (typeof word ! 'string')
  • ten Math.floor(unit / 10)
  • unit unit 10
  • word unitsunit
  • if (word)
  • word ' ' word

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 unitsunit
  • // 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 unitsunit
  • // If the unit is not zero, add its name to the
    word.
  • if (word)
  • word ' ' word

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 unitshundred '
    hundred' word
  • if (result)
  • result ' ' result
  • result word result
  • n Math.floor(n / 1000)
  • if (n 0)
  • break

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)

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
    array0
  • for (i 1 i lt length i 1)
  • result func(result, arrayi)
  • 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 lt length)
  • return step(func(result,
    arrayi))
  • else
  • return result
  • return step(array0)

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 lt length i 1)
  • resulti func(arrayi)
  • 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 recordname
  • )
  • var ids select(database, 'id')

28
Filter
  • var filter function (array, func)
  • var i, length array.length, result
  • for (i 0 i lt length i 1)
  • if (func(arrayi))
  • result.push(arrayi)
  • 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 recordname 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.
Write a Comment
User Comments (0)
About PowerShow.com