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: README.md
+76-31Lines changed: 76 additions & 31 deletions
Original file line number
Diff line number
Diff line change
@@ -1,21 +1,31 @@
1
-
JavaScript Arithmetic Lab
2
-
---
1
+
# JavaScript Arithmetic Lab
3
2
4
3
## Objectives
5
4
6
-
1. Practice doing math with JavaScript
7
-
2. Practice writing functions that do things with numbers
8
-
3. Practice parsing strings as numbers
5
+
- Practice doing math with JavaScript
6
+
- Practice writing functions that do things with numbers
7
+
- Practice parsing strings as numbers
9
8
10
9
## Introduction
11
10
12
-
In this lab, we're going to practice writing functions and manipulating numbers in JavaScript. First, though, we need to go over some basic math. In this lab, we're going to learn about various arithmetic operators. What's an operator, you say? It's a symbol that _operates_ on one or more (usually two) objects — `+` is a good example. The `+` operator says "add what's to the left of `+` and what's to the right of `+` together." Easy-peasy!
11
+
In this lab, we're going to practice writing functions and manipulating numbers
12
+
in JavaScript. First, though, we need to go over some basic math. In this lab,
13
+
we're going to learn about various arithmetic operators. What's an operator, you
14
+
say? It's a symbol that _operates_ on one or more (usually two) objects — `+` is
15
+
a good example. The `+` operator says "add what's to the left of `+` and what's
16
+
to the right of `+` together."
13
17
14
-
As you read through this lesson, you're going to be adding your solutions to `index.js`. You'll write a total of eight functions; use the results of running `learn test` in your IDE to guide you towards the right function names and functionality.
18
+
As you read through this lesson, you're going to be adding your solutions to
19
+
`index.js`. You'll write a total of eight functions; use the results of running
20
+
`learn test` in your IDE to guide you towards the right function names and
21
+
functionality.
15
22
16
23
## Basic Math
17
24
18
-
The most fundamental math operations work as one might expect in JavaScript: `+` adds two numbers; `-` subtracts one number from another; `*` multiplies two numbers; and `/` divides one number by another. For example (as usual, follow along in console!)
25
+
The most fundamental math operations work as one might expect in JavaScript: `+`
26
+
adds two numbers; `-` subtracts one number from another; `*` multiplies two
27
+
numbers; and `/` divides one number by another. For example (as usual, follow
28
+
along in console!)
19
29
20
30
```javascript
21
31
1+80// 81
@@ -24,11 +34,13 @@ The most fundamental math operations work as one might expect in JavaScript: `+`
24
34
5.0/2.5// 2
25
35
```
26
36
27
-
At this point, we can fix the first _four_ broken tests: we can define functions `add()`, `subtract()`, `multiply()`, `divide()` in `index.js`.
37
+
At this point, we can fix the first _four_ failing tests: we can define
38
+
functions `add()`, `subtract()`, `multiply()`, `divide()` in `index.js`.
28
39
29
40
## Math + Assignment
30
41
31
-
Additionally, we can increment (`++`) and decrement (`--`) a number if it's assigned to a variable:
42
+
Additionally, we can increment (`++`) and decrement (`--`) a number if it's
43
+
assigned to a variable:
32
44
33
45
```javascript
34
46
var number =5
@@ -50,35 +62,49 @@ We can also put the incrementor and decrementor operations before the number:
50
62
++number // 5
51
63
```
52
64
53
-
But generally, you will see them placed _after_ the number (and we recommend that that's where you put them). If you're interested in the difference, take a look [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Increment)
65
+
But generally, you will see them placed _after_ the number (and we recommend
66
+
that that's where you put them). If you're interested in the difference, take a
And, while we're on the subject, you'll usually only want to use these incrementors and decrementors when the shorthand makes what you're writing easier to read (more on when _exactly_ later). Instead, it's best to use the basic arithmetic operators combined with `=`. For the examples below, assume that `number` is equal to `5` (and resets for every example).
70
+
And, while we're on the subject, you'll usually only want to use these
71
+
incrementors and decrementors when the shorthand makes what you're writing
72
+
easier to read (more on when _exactly_ later). Instead, it's best to use the
73
+
basic arithmetic operators combined with `=`. For the examples below, assume
74
+
that `number` is equal to `5` (and resets for every example).
56
75
57
-
-`+=` modifies the value to the operator's left by adding to it the value to the operator's right:
76
+
-`+=` modifies the value to the operator's left by adding to it the value to
77
+
the operator's right:
58
78
59
79
```javascript
60
80
number +=3// 8
61
81
```
62
82
63
-
-`-=` modifies the value to the operator's left by subtracting from it the value to the operator's right:
83
+
-`-=` modifies the value to the operator's left by subtracting from it the
84
+
value to the operator's right:
64
85
65
86
```javascript
66
87
number -=2// 3
67
88
```
68
89
69
-
-`*=` modifies the value to the operator's left by multiplying it by the value to the operator's right:
90
+
-`*=` modifies the value to the operator's left by multiplying it by the value
91
+
to the operator's right:
70
92
71
93
```javascript
72
94
number *=10// 50
73
95
```
74
96
75
-
-`/=` modifies the value to the operator's left by dividing it by the value to the operator's right:
97
+
-`/=` modifies the value to the operator's left by dividing it by the value to
98
+
the operator's right:
76
99
77
100
```javascript
78
101
number /=5// 1
79
102
```
80
103
81
-
The thing to remember about these methods is that they modify the variable in place. So if we have two functions that depend on the same external variable, the order in which they're called matters. Follow along in console:
104
+
The thing to remember about these methods is that they modify the variable in
105
+
place. So, if we have two functions that depend on the same external variable,
106
+
the order in which they are called matters. Follow along in console copying each
107
+
function and statement below one at a time:
82
108
83
109
```javascript
84
110
var number =10
@@ -113,56 +139,75 @@ console.log(number) // 5
113
139
114
140
**Because these methods are more explicit, we prefer `+=` to `++` and `-=` to `--` (usually).**
115
141
116
-
Okay, now we're ready to write solutions for the next two functions: `inc(n)` and `dec(n)`.
142
+
Okay, now we're ready to write solutions for the next two functions:
143
+
`increment(n)` and `decrement(n)`. These methods should take in a number, and
144
+
either increments the provided value by one or decrements it by one
145
+
respectively, returning the result.
117
146
118
147
## Parsing Numbers
119
148
120
-
Sometimes, we'll receive a number — well, we know it's a number, as we've seen many numbers in the past. JavaScript, however, won't know that it's a number because it shows up wrapped in quotes — JavaScript, then, thinks it's a string.
149
+
Sometimes, we'll receive a number — well, we know it's a number, as we've seen
150
+
many numbers in the past. JavaScript, however, won't know that it's a number
151
+
because it shows up wrapped in quotes — JavaScript, then, thinks it's a string.
121
152
122
-
Luckily, JavaScript gives us tools to turn these strings into proper numbers (that is, numbers that JavaScript understands).
153
+
Luckily, JavaScript gives us tools to turn these strings into proper numbers
154
+
(that is, numbers that JavaScript understands).
123
155
124
156
### `parseInt()`
125
157
126
-
The first such tool is the function `parseInt()`, which accepts two arguments: the value to parse and the base of the value being parsed. _Usually_ you will want to work with base 10, so a typical call to `parseInt()` looks like
158
+
The first such tool is the function `parseInt()`, which accepts two arguments:
159
+
the value to parse and the base of the value being parsed. _Usually_ you will
160
+
want to work with base 10, so a typical call to `parseInt()` looks like
127
161
128
162
```javascript
129
163
parseInt('2', 10) // 2
130
164
```
131
165
132
-
What happens if we pass a representation of a non-integer to `parseInt()`? Let's try it:
166
+
What happens if we pass a representation of a non-integer to `parseInt()`? Let's
167
+
try it:
133
168
134
169
```javascript
135
170
parseInt('2.2222', 10)
136
171
```
137
172
138
-
If we enter the above in console, we'll see that `parseInt()` forces the parsed number to be an integer — which makes sense when we think about it, right?
173
+
If we enter the above in console, we will see that `parseInt()` forces the parsed
174
+
number to be an integer — which makes sense when we think about it, right?
139
175
140
-
What happens, though, if we pass utter nonsense to `parseInt()`? Go ahead and try it in the console — something like
176
+
What happens, though, if we pass utter nonsense to `parseInt()`? Go ahead and
177
+
try it in the console — something like
141
178
142
179
```javascript
143
180
parseInt('nonsense!', 10)
144
181
```
145
182
146
183
What did it return? `NaN`? What is that?
147
184
148
-
`NaN` stands for "not a number" — pretty handy, right? This is the number (in the JavaScript sense) that JavaScript returns when it can't determine a valid value for a numeric operation.
185
+
`NaN` stands for "Not a Number" — pretty handy, right? This is the number (in
186
+
the JavaScript sense) that JavaScript returns when it can't determine a valid
187
+
value for a numeric operation.
149
188
150
189
### `parseFloat()`
151
190
152
-
Above, we saw that `parseInt()` lops off everything after the decimal point and only returns integers. If we want to preserve decimals, we'll need to use `parseFloat()`.
191
+
Above, we saw that `parseInt()` lops off everything after the decimal point and
192
+
only returns integers. If we want to preserve decimals, we'll need to use
193
+
`parseFloat()`.
153
194
154
-
Unlike `parseInt()`, `parseFloat()` accepts only a single argument, the thing to be parsed. We can use it like so:
195
+
Unlike `parseInt()`, `parseFloat()` accepts only a single argument, the thing to
196
+
be parsed. We can use it like so:
155
197
156
198
```javascript
157
199
parseFloat('80.123999') // 80.123999
158
200
```
159
201
160
-
You're now ready to solve the final two tests in this lab, `makeInt(n)` and `preserveDecimal(n)`.
161
-
202
+
You're now ready to solve the final two tests in this lab, `makeInt(string)` and
203
+
`preserveDecimal(string)`. `makeInt(string)` should take in a string, parse it into an
204
+
base 10 integer and return it. `preserveDecimal(string)` should take in a string, parse it
<pclass='util--hide'>View <ahref='https://learn.co/lessons/javascript-arithmetic-lab'>JavaScript Arithmetic Lab</a> on Learn.co and start learning to code for free.</p>
0 commit comments