You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 04_data.md
+15-15Lines changed: 15 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -54,7 +54,7 @@ We could get creative with strings—after all, strings can have any length, so
54
54
55
55
{{index [array, creation], "[] (array)"}}
56
56
57
-
Fortunately, JavaScript provides a data type specifically for storing sequences of values. It is called an _array_ and is written as a list of values between ((square brackets)), separated by commas:
57
+
Fortunately, JavaScript provides a data type specifically for storing sequences of values. It is called an _array_ and is written as a list of values between ((square brackets)), separated by commas.
58
58
59
59
```
60
60
let listOfNumbers = [2, 3, 5, 7, 11];
@@ -133,7 +133,7 @@ Properties that contain functions are generally called _methods_ of the value th
133
133
134
134
{{id array_methods}}
135
135
136
-
This example demonstrates two methods you can use to manipulate arrays:
136
+
This example demonstrates two methods you can use to manipulate arrays.
137
137
138
138
```
139
139
let sequence = [1, 2, 3];
@@ -192,7 +192,7 @@ let descriptions = {
192
192
193
193
{{index [braces, object]}}
194
194
195
-
This means that braces have _two_ meanings in JavaScript. At the start of a ((statement)), they begin a ((block)) of statements. In any other position, they describe an object. Fortunately, it is rarely useful to start a statement with an object in braces, so the ambiguity between these two is not much of a problem. The one case where this does come up is when you want to return an object from a short-hand arrow function—you can't write `n => {prop: n}` since the braces will be interpreted as a function body. Instead, you have to put a set of parentheses around the object to make it clear that it is an expression.
195
+
This means that braces have _two_ meanings in JavaScript. At the start of a ((statement)), they begin a ((block)) of statements. In any other position, they describe an object. Fortunately, it is rarely useful to start a statement with an object in braces, so the ambiguity between these two is not much of a problem. The one case where this does come up is when you want to return an object from a shorthand arrow function—you can't write `n => {prop: n}` since the braces will be interpreted as a function body. Instead, you have to put a set of parentheses around the object to make it clear that it is an expression.
@@ -518,7 +518,7 @@ for (let event of journalEvents(JOURNAL)) {
518
518
// → weekend: 0.1371988681
519
519
// → bread: -0.0757554019
520
520
// → pudding: -0.0648203724
521
-
// and so on...
521
+
// And so on...
522
522
```
523
523
524
524
Most correlations seem to lie close to zero. Eating carrots, bread, or pudding apparently does not trigger squirrel-lycanthropy. The transformations _do_ seem to occur somewhat more often on weekends. Let's filter the results to show only correlations greater than 0.1 or less than -0.1:
@@ -541,7 +541,7 @@ for (let event of journalEvents(JOURNAL)) {
541
541
542
542
Aha! There are two factors with a ((correlation)) clearly stronger than the others. Eating ((peanuts)) has a strong positive effect on the chance of turning into a squirrel, whereas brushing teeth has a significant negative effect.
543
543
544
-
Interesting. Let's try something:
544
+
Interesting. Let's try something.
545
545
546
546
```
547
547
for (let entry of JOURNAL) {
@@ -560,13 +560,13 @@ Knowing this, Jacques stops eating peanuts altogether and finds that his transfo
560
560
561
561
{{index "weresquirrel example"}}
562
562
563
-
But it only takes a few months for him to notice that something is missing from this entirely human way of living. Without his feral adventures, Jacques hardly feels alive at all. He decides he'd rather be a full-time wild animal. After building a beautiful little tree house in the forest and equipping it with a peanut butter dispenser and a ten-year supply of peanut butter, he changes form one last time, and lives the short and energetic life of a squirrel.
563
+
But it takes only a few months for him to notice that something is missing from this entirely human way of living. Without his feral adventures, Jacques hardly feels alive at all. He decides he'd rather be a full-time wild animal. After building a beautiful little tree house in the forest and equipping it with a peanut butter dispenser and a ten-year supply of peanut butter, he changes form one last time, and lives the short and energetic life of a squirrel.
564
564
565
565
## Further arrayology
566
566
567
567
{{index [array, methods], [method, array]}}
568
568
569
-
Before finishing the chapter, I want to introduce you to a few more object-related concepts. I'll start by introducing some generally useful array methods.
569
+
Before finishing the chapter, I want to introduce you to a few more object-related concepts. I'll start with some generally useful array methods.
Back to the `Math` object. If you need to do ((trigonometry)), `Math` can help. It contains `cos` (cosine), `sin` (sine), and `tan` (tangent), as well as their inverse functions, `acos`, `asin`, and `atan`, respectively. The number π (pi)—or at least the closest approximation that fits in a JavaScript number—is available as `Math.PI`. There is an old programming tradition of writing the names of ((constant)) values in all caps:
800
+
Back to the `Math` object. If you need to do ((trigonometry)), `Math` can help. It contains `cos` (cosine), `sin` (sine), and `tan` (tangent), as well as their inverse functions, `acos`, `asin`, and `atan`, respectively. The number π (pi)—or at least the closest approximation that fits in a JavaScript number—is available as `Math.PI`. There is an old programming tradition of writing the names of ((constant)) values in all caps.
801
801
802
802
```{test: no}
803
803
function randomPointOnCircle(radius) {
@@ -813,7 +813,7 @@ If you're not familiar with sines and cosines, don't worry. I'll explain them wh
813
813
814
814
{{index "Math.random function", "random number"}}
815
815
816
-
The previous example used `Math.random`. This is a function that returns a new pseudorandom number between zero (inclusive) and one (exclusive) every time you call it:
816
+
The previous example used `Math.random`. This is a function that returns a new pseudorandom number between 0 (inclusive) and 1 (exclusive) every time you call it:
817
817
818
818
```{test: no}
819
819
console.log(Math.random());
@@ -877,7 +877,7 @@ This also works for bindings created with `let`, `var`, or `const`. If you know
877
877
878
878
{{index [object, property], [braces, object]}}
879
879
880
-
A similar trick works for objects, using braces instead of square brackets:
880
+
A similar trick works for objects, using braces instead of square brackets.
The expression `a?.b` means the same as `a.b` when `a` isn't null or undefined. When it is, it evaluates to undefined. This can be convenient when, as in the example, you aren't sure that a given property exists or when a variable might hold an undefined value.
908
+
The expression `a?.b` means the same as `a.b` when `a` isn't null or undefined. When it is, it evaluates to `undefined`. This can be convenient when, as in the example, you aren't sure that a given property exists or when a variable might hold an undefined value.
909
909
910
910
A similar notation can be used with square bracket access, and even with function calls, by putting `?.` in front of the parentheses or brackets:
Arrays have a `reverse` method that changes the array by inverting the order in which its elements appear. For this exercise, write two functions, `reverseArray` and `reverseArrayInPlace`. The first, `reverseArray`, should take an array as argument and produce a _new_ array that has the same elements in the inverse order. The second, `reverseArrayInPlace`, should do what the `reverse` method does: _modify_ the array given as argument by reversing its elements. Neither may use the standard `reverse` method.
1032
+
Arrays have a `reverse` method that changes the array by inverting the order in which its elements appear. For this exercise, write two functions, `reverseArray` and `reverseArrayInPlace`. The first, `reverseArray`, should take an array as its argument and produce a _new_ array that has the same elements in the inverse order. The second, `reverseArrayInPlace`, should do what the `reverse` method does: _modify_ the array given as its argument by reversing its elements. Neither may use the standard `reverse` method.
The `==` operator compares objects by identity, but sometimes you'd prefer to compare the values of their actual properties.
1151
1151
1152
-
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, where the values of the properties are equal when compared with a recursive call to `deepEqual`.
1152
+
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, where the values of the properties are equal when compared with a recursive call to `deepEqual`.
0 commit comments