- Introduction
- Chapter 1 Values, Types, and Operators
- Chapter 2 Program Structure
- Expressions and statements
- Variables
- Keywords and reserved words
- The environment
- Functions
- The console.log function
- Return values
- prompt and confirm
- Control flow
- Conditional execution
- while and do loops
- Indenting Code
- for loops
- Breaking Out of a Loop
- Updating variables succinctly
- Dispatching on a value with switch
- Capitalization
- Comments
- Summary
- Exercises
- Chapter 3 Functions
- Chapter 4 Data Structures: Objects and Arrays
- Chapter 5 Higher-Order Functions
- Chapter 6 The Secret Life of Objects
- Chapter 7 Project: Electronic Life
- Chapter 8 Bugs and Error Handling
- Chapter 9 Regular Expressions
- Creating a regular expression
- Testing for matches
- Matching a set of characters
- Repeating parts of a pattern
- Grouping subexpressions
- Matches and groups
- The date type
- Word and string boundaries
- Choice patterns
- The mechanics of matching
- Backtracking
- The replace method
- Greed
- Dynamically creating RegExp objects
- The search method
- The lastIndex property
- Parsing an INI file
- International characters
- Summary
- Exercises
- Chapter 10 Modules
- Chapter 11 Project: A Programming Language
- Chapter 12 JavaScript and the Browser
- Chapter 13 The Document Object Model
- Chapter 14 Handling Events
- Chapter 15 Project: A Platform Game
- Chapter 16 Drawing on Canvas
- Chapter 17 HTTP
- Chapter 18 Forms and Form Fields
- Chapter 19 Project: A Paint Program
- Chapter 20 Node.js
- Chapter 21 Project: Skill-Sharing Website
- Eloquent JavaScript
- Exercise Hints
- Program Structure
- Functions
- Data Structures: Objects and Arrays
- Higher-Order Functions
- The Secret Life of Objects
- Project: Electronic Life
- Bugs and Error Handling
- Regular Expressions
- Modules
- Project: A Programming Language
- The Document Object Model
- Handling Events
- Project: A Platform Game
- Drawing on Canvas
- HTTP
- Forms and Form Fields
- Project: A Paint Program
- Node.js
- Project: Skill-Sharing Website
Exercises
The sum of a range
The introduction of this book alluded to the following as a nice way to compute the sum of a range of numbers:
console.log(sum(range(1, 10)));
Write a range
function that takes two arguments, start
and end
, and returns an array containing all the numbers from start
up to (and including) end
.
Next, write a sum
function that takes an array of numbers and returns the sum of these numbers. Run the previous program and see whether it does indeed return 55.
As a bonus assignment, modify your range
function to take an optional third argument that indicates the “step” value used to build up the array. If no step is given, the array elements go up by increments of one, corresponding to the old behavior. The function call range(1, 10, 2)
should return [1, 3, 5, 7, 9]
. Make sure it also works with negative step values so that range(5, 2, -1)
produces [5, 4, 3, 2]
.
Reversing an array
Arrays have a method reverse
, which changes the array by inverting the order in which its elements appear. For this exercise, write two functions, reverseArray
and reverseArrayInPlace
. The first, reverseArray
, takes an array as argument and produces a new array that has the same elements in the inverse order. The second, reverseArrayInPlace
, does what the reverse
method does: it modifies the array given as argument in order to reverse its elements. Neither may use the standard reverse
method.
Thinking back to the notes about side effects and pure functions in the previous chapter , which variant do you expect to be useful in more situations? Which one is more efficient?
A list
Objects, as generic blobs of values, can be used to build all sorts of data structures. A common data structure is the list (not to be confused with the array). A list is a nested set of objects, with the first object holding a reference to the second, the second to the third, and so on.
var list = { value: 1, rest: { value: 2, rest: { value: 3, rest: null } } };
The resulting objects form a chain, like this:
A nice thing about lists is that they can share parts of their structure. For example, if I create two new values {value: 0, rest: list}
and {value: -1, rest: list}
(with list
referring to the variable defined earlier), they are both independent lists, but they share the structure that makes up their last three elements. In addition, the original list is also still a valid three-element list.
Write a function arrayToList
that builds up a data structure like the previous one when given [1, 2, 3]
as argument, and write a listToArray
function that produces an array from a list. Also write the helper functions prepend
, which takes an element and a list and creates a new list that adds the element to the front of the input list, and nth
, which takes a list and a number and returns the element at the given position in the list, or undefined
when there is no such element.
If you haven’t already, also write a recursive version of nth
.
Deep comparison
The ==
operator compares objects by identity. But sometimes, you would prefer to compare the values of their actual properties.
Write a function, deepEqual
, that takes two values and returns true only if they are the same value or are objects with the same properties whose values are also equal when compared with a recursive call to deepEqual
.
To find out whether to compare two things by identity (use the ===
operator for that) or by looking at their properties, you can use the typeof
operator. If it produces "object"
for both values, you should do a deep comparison. But you have to take one silly exception into account: by a historical accident, typeof null
also produces "object"
.
This is a book about getting computers to do what you want them to do. Computers are about as common as screwdrivers today, but they contain a lot more hidden complexity and thus are harder to operate and understand. To many, they remain alien, slightly threatening things.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论