Skip to content

Commit 4d6668f

Browse files
committed
Integrate copyediting for Chapter 2
1 parent dd35f53 commit 4d6668f

File tree

1 file changed

+46
-51
lines changed

1 file changed

+46
-51
lines changed

02_program_structure.md

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ quote}}
1313

1414
{{figure {url: "img/chapter_picture_2.jpg", alt: "Picture of tentacles holding objects", chapter: framed}}}
1515

16-
In this chapter, we start to do things that can actually be called
16+
In this chapter, we will start to do things that can actually be called
1717
_programming_. We will expand our command of the JavaScript language
1818
beyond the nouns and sentence fragments we've seen so far, to the
1919
point where we can express meaningful prose.
@@ -25,7 +25,7 @@ point where we can express meaningful prose.
2525
In [Chapter ?](values), we made values and applied operators to them
2626
to get new values. Creating values like this is the main substance of
2727
any JavaScript program. But that substance has to be framed in a
28-
larger structure to be useful. So that's what we'll get to next.
28+
larger structure to be useful. So that's what we'll cover next.
2929

3030
{{index "literal expression"}}
3131

@@ -38,8 +38,7 @@ applied to two expressions or a ((unary operator)) applied to one.
3838
{{index [nesting, "of expressions"], "human language"}}
3939

4040
This shows part of the beauty of a language-based interface.
41-
Expressions can contain other expressions in a way very similar to the
42-
way subsentences in human languages are nested—a subsentence can
41+
Expressions can contain other expressions in a way similar to how subsentences in human languages are nested—a subsentence can
4342
contain its own subsentences, and so on. This allows us to build
4443
expressions that describe arbitrarily complex computations.
4544

@@ -120,7 +119,7 @@ console.log(ten * ten);
120119
When a binding points at a value, that does not mean it is tied to
121120
that value forever. The `=` operator can be used at any time on
122121
existing bindings to disconnect them from their current value and have
123-
them point to a new one:
122+
them point to a new one.
124123

125124
```
126125
let mood = "light";
@@ -141,7 +140,7 @@ to hold on to it or you reattach one of your existing tentacles to it.
141140

142141
Let's look at another example. To remember the number of dollars that
143142
Luigi still owes you, you create a binding. And then when he pays back
144-
$35, you give this binding a new value:
143+
$35, you give this binding a new value.
145144

146145
```
147146
let luigisDebt = 140;
@@ -180,7 +179,7 @@ console.log(greeting + name);
180179
{{index "var keyword"}}
181180

182181
The first, `var` (short for "variable"), is the way bindings were
183-
declared in pre-2015 JavaScript. We'll get back to the precise way it
182+
declared in pre-2015 JavaScript. I'll get back to the precise way it
184183
differs from `let` in the [next chapter](functions). For now,
185184
remember that it mostly does the same thing, but we'll rarely use it
186185
in this book because it has some confusing properties.
@@ -199,15 +198,15 @@ easily refer to it later.
199198
Binding names can be any word. Digits can be part of binding
200199
names—`catch22` is a valid name, for example—but the name must not
201200
start with a digit. A binding name may include dollar signs (`$`) or
202-
underscores (`_`), but no other punctuation or special characters.
201+
underscores (`_`) but no other punctuation or special characters.
203202

204203
{{index syntax, "implements (reserved word)", "interface (reserved word)", "package (reserved word)", "private (reserved word)", "protected (reserved word)", "public (reserved word)", "static (reserved word)", "void operator", "yield (reserved word)", "enum (reserved word)", "reserved word", [binding, naming]}}
205204

206205
Words with a special meaning, such as `let`, are _((keyword))s_, and
207206
they may not be used as binding names. There are also a number of
208207
words that are "reserved for use" in ((future)) versions of
209208
JavaScript, which also can't be used as binding names. The full list
210-
of keywords and reserved words is rather long:
209+
of keywords and reserved words is rather long.
211210

212211
```{lang: "text/plain"}
213212
break case catch class const continue debugger default
@@ -217,8 +216,8 @@ new package private protected public return static super
217216
switch this throw true try typeof var void while with yield
218217
```
219218

220-
Don't worry about memorizing these. When creating a binding produces
221-
an unexpected ((syntax error)), see if you're trying to define a
219+
Don't worry about memorizing this list. When creating a binding produces
220+
an unexpected ((syntax error)), see whether you're trying to define a
222221
reserved word.
223222

224223
## The environment
@@ -278,9 +277,9 @@ systems (including all modern web ((browser))s and Node.js) provide a
278277
`console.log` function that writes out its arguments to _some_ text
279278
output device. In browsers, the output lands in the ((JavaScript
280279
console)). This part of the browser interface is hidden by default,
281-
but most browsers open it when you press F12 or, on Mac, Command-Option-I.
282-
If that does not work, search through the menus for an item named "developer
283-
tools" or similar.
280+
but most browsers open it when you press F12 or, on a Mac, command-option-I.
281+
If that does not work, search through the menus for an item named Developer
282+
Tools or similar.
284283

285284
{{if interactive
286285

@@ -301,7 +300,7 @@ if}}
301300
Though binding names cannot contain ((period character))s,
302301
`console.log` does have one. This is because `console.log` isn't a
303302
simple binding. It is actually an expression that retrieves the `log`
304-
((property)) from the value held by the `console` binding. We will
303+
((property)) from the value held by the `console` binding. We'll
305304
find out exactly what this means in [Chapter ?](data#properties).
306305

307306
{{id return_values}}
@@ -408,12 +407,11 @@ doesn't represent a valid number. Thus, the condition translates to
408407

409408
{{index grouping, "{} (block)"}}
410409

411-
The statement below the `if` is wrapped in ((curly braces)) (`{` and
412-
`}`) in this example. Those can be used to group any number of
410+
The statement after the `if` is wrapped in ((braces)) (`{` and
411+
`}`) in this example. The braces can be used to group any number of
413412
statements into a single statement, called a _((block))_. You could
414413
also have omitted them in this case, since they hold only a single
415-
statement, but to avoid having to think about whether they are needed
416-
or not, most JavaScript programmers use them in every wrapped
414+
statement, but to avoid having to think about whether they are needed, most JavaScript programmers use them in every wrapped
417415
statement like this. We'll mostly follow that convention in this book,
418416
except for the occasional one-liner.
419417

@@ -426,8 +424,7 @@ if (1 + 1 == 2) console.log("It's true");
426424

427425
You often won't just have code that executes when a condition holds
428426
true, but also code that handles the other case. This alternate path
429-
is represented by the second arrow in the diagram. The `else` keyword
430-
can be used, together with `if`, to create two separate, alternative
427+
is represented by the second arrow in the diagram. You can use the `else` keyword, together with `if`, to create two separate, alternative
431428
execution paths.
432429

433430
```{test: wrap}
@@ -442,8 +439,8 @@ if (!Number.isNaN(theNumber)) {
442439

443440
{{index ["if keyword", chaining]}}
444441

445-
If we have more than two paths to choose from, multiple `if`/`else`
446-
pairs can be "chained" together. Here's an example:
442+
If you have more than two paths to choose from, you can "chain" multiple `if`/`else`
443+
pairs together. Here's an example:
447444

448445
```
449446
let num = Number(prompt("Pick a number"));
@@ -490,7 +487,7 @@ That works, but the idea of writing a program is to make something
490487
_less_ work, not more. If we needed all even numbers less than 1,000,
491488
this approach would be unworkable. What we need is a way to run a
492489
piece of code multiple times. This form of control flow is called a
493-
_((loop))_:
490+
_((loop))_.
494491

495492
{{figure {url: "img/controlflow-loop.svg", alt: "Loop control flow",width: "4cm"}}}
496493

@@ -549,7 +546,7 @@ console.log(result);
549546
```
550547

551548
The counter could also have started at `1` and checked for `<= 10`,
552-
but, for reasons that will become apparent in [Chapter
549+
but for reasons that will become apparent in [Chapter
553550
?](data#array_indexing), it is a good idea to get used to
554551
counting from 0.
555552

@@ -559,7 +556,7 @@ A `do` loop is a control structure similar to a `while` loop. It
559556
differs only on one point: a `do` loop always executes its body at
560557
least once, and it starts testing whether it should stop only after
561558
that first execution. To reflect this, the test appears after the body
562-
of the loop:
559+
of the loop.
563560

564561
```
565562
let yourName;
@@ -582,7 +579,7 @@ continues going round until you provide a non-empty name.
582579
{{index "code structure", whitespace, "programming style"}}
583580

584581
In the examples, I've been adding spaces in front of statements that
585-
are part of some larger statement. These are not required—the computer
582+
are part of some larger statement. These spaces are not required—the computer
586583
will accept the program just fine without them. In fact, even the
587584
((line)) breaks in programs are optional. You could write a program as
588585
a single long line if you felt like it.
@@ -613,7 +610,7 @@ amount.
613610

614611
{{index syntax, "while loop", "counter variable"}}
615612

616-
Many loops follow the pattern seen in the `while` examples. First, a
613+
Many loops follow the pattern shown in the `while` examples. First a
617614
"counter" binding is created to track the progress of the loop. Then
618615
comes a `while` loop, usually with a test expression that checks whether the
619616
counter has reached its end value. At the end of the loop body, the
@@ -623,7 +620,7 @@ counter is updated to track progress.
623620

624621
Because this pattern is so common, JavaScript and similar languages
625622
provide a slightly shorter and more comprehensive form, the `for`
626-
loop:
623+
loop.
627624

628625
```
629626
for (let number = 0; number <= 12; number = number + 2) {
@@ -650,7 +647,7 @@ cases, this is shorter and clearer than a `while` construct.
650647

651648
{{index exponentiation}}
652649

653-
This is the code that computes 2^10^, using `for` instead of `while`:
650+
This is the code that computes 2^10^ using `for` instead of `while`:
654651

655652
```{test: wrap}
656653
let result = 1;
@@ -704,8 +701,7 @@ will never finish running, which is usually a bad thing.
704701
If you create an infinite loop in one of the examples on these pages,
705702
you'll usually be asked whether you want to stop the script after a
706703
few seconds. If that fails, you will have to close the tab that you're
707-
working in, or on some browsers close your whole browser, in order to
708-
recover.
704+
working in, or on some browsers close your whole browser, to recover.
709705

710706
if}}
711707

@@ -727,7 +723,7 @@ to hold a value based on that binding's previous value.
727723
counter = counter + 1;
728724
```
729725

730-
JavaScript provides a shortcut for this:
726+
JavaScript provides a shortcut for this.
731727

732728
```{test: no}
733729
counter += 1;
@@ -898,21 +894,21 @@ of useful standard bindings into your environment.
898894

899895
Functions are special values that encapsulate a piece of program. You
900896
can invoke them by writing `functionName(argument1, argument2)`. Such
901-
a function call is an expression, and may produce a value.
897+
a function call is an expression and may produce a value.
902898

903899
## Exercises
904900

905901
{{index exercises}}
906902

907-
If you are unsure how to try your solutions to exercises, refer to the
908-
[introduction](intro).
903+
If you are unsure how to test your solutions to the exercises, refer to the
904+
[Introduction](intro).
909905

910-
Each exercise starts with a problem description. Read that and try to
906+
Each exercise starts with a problem description. Read this description and try to
911907
solve the exercise. If you run into problems, consider reading the
912908
hints [after the exercise]{if interactive}[at the [end of the
913909
book](hints)]{if book}. Full solutions to the exercises are
914910
not included in this book, but you can find them online at
915-
[_eloquentjavascript.net/code_](https://eloquentjavascript.net/code#2).
911+
[_https://eloquentjavascript.net/code_](https://eloquentjavascript.net/code#2).
916912
If you want to learn something from the exercises, I recommend looking
917913
at the solutions only after you've solved the exercise, or at least
918914
after you've attacked it long and hard enough to have a slight
@@ -938,7 +934,7 @@ following triangle:
938934
{{index [string, length]}}
939935

940936
It may be useful to know that you can find the length of a string by
941-
writing `.length` after it:
937+
writing `.length` after it.
942938

943939
```
944940
let abc = "abc";
@@ -981,7 +977,7 @@ Write a program that uses `console.log` to print all the numbers from
981977
`"Fizz"` instead of the number, and for numbers divisible by 5 (and
982978
not 3), print `"Buzz"` instead.
983979

984-
When you have that working, modify your program to print `"FizzBuzz"`,
980+
When you have that working, modify your program to print `"FizzBuzz"`
985981
for numbers that are divisible by both 3 and 5 (and still print
986982
`"Fizz"` or `"Buzz"` for numbers divisible by only one of those).
987983

@@ -1010,22 +1006,22 @@ number, so you'll have to create an `if`/`else if`/`else` chain.
10101006
{{index "|| operator", ["if keyword", chaining]}}
10111007

10121008
The second version of the program has a straightforward solution and a
1013-
clever one. The simple way is to add another conditional "branch" to
1014-
precisely test the given condition. For the clever method, build up a
1009+
clever one. The simple solution is to add another conditional "branch" to
1010+
precisely test the given condition. For the clever solution, build up a
10151011
string containing the word or words to output and print either this
10161012
word or the number if there is no word, potentially by making good use
10171013
of the `||` operator.
10181014

10191015
hint}}
10201016

1021-
### Chess board
1017+
### Chessboard
10221018

1023-
{{index "chess board (exercise)", loop, [nesting, "of loops"], "newline character"}}
1019+
{{index "chessboard (exercise)", loop, [nesting, "of loops"], "newline character"}}
10241020

10251021
Write a program that creates a string that represents an 8×8 grid,
10261022
using newline characters to separate lines. At each position of the
10271023
grid there is either a space or a "#" character. The characters should
1028-
form a chess board.
1024+
form a chessboard.
10291025

10301026
Passing this string to `console.log` should show something like this:
10311027

@@ -1054,17 +1050,17 @@ if}}
10541050

10551051
{{index "chess board (exercise)"}}
10561052

1057-
The string can be built by starting with an empty one (`""`) and
1053+
You can build the string by starting with an empty one (`""`) and
10581054
repeatedly adding characters. A newline character is written `"\n"`.
10591055

1060-
{{index [nesting, "of loops"]}}
1056+
{{index [nesting, "of loops"], "curly braces"}}
10611057

10621058
To work with two ((dimensions)), you will need a ((loop)) inside of a
1063-
loop. Put ((curly braces)) around the bodies of both loops to make it
1059+
loop. Put braces around the bodies of both loops to make it
10641060
easy to see where they start and end. Try to properly indent these
10651061
bodies. The order of the loops must follow the order in which we build
10661062
up the string (line by line, left to right, top to bottom). So the
1067-
outer loop handles the lines and the inner loop handles the characters
1063+
outer loop handles the lines, and the inner loop handles the characters
10681064
on a line.
10691065

10701066
{{index "counter variable", "remainder operator", "% operator"}}
@@ -1074,7 +1070,6 @@ put a space or a hash sign at a given position, you could test whether
10741070
the sum of the two counters is even (`% 2`).
10751071

10761072
Terminating a line by adding a newline character must happen after the
1077-
line has been built up, so do this after the inner loop but inside of
1078-
the outer loop.
1073+
line has been built up, so do this after the inner loop but inside the outer loop.
10791074

10801075
hint}}

0 commit comments

Comments
 (0)