Skip to content

Commit af6b745

Browse files
committed
add additional instructions, format readme
1 parent 0ec2cd2 commit af6b745

File tree

1 file changed

+76
-31
lines changed

1 file changed

+76
-31
lines changed

README.md

Lines changed: 76 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
1-
JavaScript Arithmetic Lab
2-
---
1+
# JavaScript Arithmetic Lab
32

43
## Objectives
54

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
98

109
## Introduction
1110

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."
1317

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.
1522

1623
## Basic Math
1724

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!)
1929

2030
``` javascript
2131
1 + 80 // 81
@@ -24,11 +34,13 @@ The most fundamental math operations work as one might expect in JavaScript: `+`
2434
5.0 / 2.5 // 2
2535
```
2636

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`.
2839

2940
## Math + Assignment
3041

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:
3244

3345
``` javascript
3446
var number = 5
@@ -50,35 +62,49 @@ We can also put the incrementor and decrementor operations before the number:
5062
++number // 5
5163
```
5264

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
67+
look
68+
[here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Increment)
5469

55-
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).
5675

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:
5878

5979
```javascript
6080
number += 3 // 8
6181
```
6282

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:
6485

6586
``` javascript
6687
number -= 2 // 3
6788
```
6889

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:
7092

7193
``` javascript
7294
number *= 10 // 50
7395
```
7496

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:
7699

77100
``` javascript
78101
number /= 5 // 1
79102
```
80103

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:
82108

83109
``` javascript
84110
var number = 10
@@ -113,56 +139,75 @@ console.log(number) // 5
113139

114140
**Because these methods are more explicit, we prefer `+=` to `++` and `-=` to `--` (usually).**
115141

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.
117146

118147
## Parsing Numbers
119148

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.
121152

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).
123155

124156
### `parseInt()`
125157

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
127161

128162
``` javascript
129163
parseInt('2', 10) // 2
130164
```
131165

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:
133168

134169
``` javascript
135170
parseInt('2.2222', 10)
136171
```
137172

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?
139175

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
141178

142179
``` javascript
143180
parseInt('nonsense!', 10)
144181
```
145182

146183
What did it return? `NaN`? What is that?
147184

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.
149188

150189
### `parseFloat()`
151190

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()`.
153194

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:
155197

156198
``` javascript
157199
parseFloat('80.123999') // 80.123999
158200
```
159201

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
205+
into a float and return it.
162206

163207
## Resources
164-
- * [MDN - parseInt()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt)
165208

166-
- * [MDN - parseFloat()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat)
209+
- [MDN - parseInt()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt)
210+
211+
- [MDN - parseFloat()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat)
167212

168213
<p class='util--hide'>View <a href='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

Comments
 (0)