Skip to content

Commit 617dd75

Browse files
committedJun 15, 2016
Initial commit
0 parents  commit 617dd75

File tree

9 files changed

+373
-0
lines changed

9 files changed

+373
-0
lines changed
 

‎.editorconfig

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
indent_style = space
6+
insert_final_newline = true
7+
8+
[*{.java,.py}]
9+
indent_size = 4
10+
11+
[*.{js,json,rb}]
12+
charset = utf-8
13+
indent_size = 2

‎.gitignore

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Created by https://www.gitignore.io/api/node
2+
3+
### Node ###
4+
# Logs
5+
logs
6+
*.log
7+
npm-debug.log*
8+
9+
# Runtime data
10+
pids
11+
*.pid
12+
*.seed
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# node-waf configuration
27+
.lock-wscript
28+
29+
# Compiled binary addons (http://nodejs.org/api/addons.html)
30+
build/Release
31+
32+
# Dependency directories
33+
node_modules
34+
jspm_packages
35+
36+
# Optional npm cache directory
37+
.npm
38+
39+
# Optional REPL history
40+
.node_repl_history
41+
42+
# Learn-specific .results.json
43+
.results.json
44+
45+
*.solution.*

‎.learn

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
tags:
2+
- javascript
3+
languages:
4+
- javascript
5+
resources:
6+
- 1

‎CONTRIBUTING.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Contributing to Learn.co Curriculum
2+
3+
We're really excited that you're about to contribute to the [open curriculum](https://learn.co/content-license) on [Learn.co](https://learn.co). If this is your first time contributing, please continue reading to learn how to make the most meaningful and useful impact possible.
4+
5+
## Raising an Issue to Encourage a Contribution
6+
7+
If you notice a problem with the curriculum that you believe needs improvement
8+
but you're unable to make the change yourself, you should raise a Github issue
9+
containing a clear description of the problem. Include relevant snippets of
10+
the content and/or screenshots if applicable. Curriculum owners regularly review
11+
issue lists and your issue will be prioritized and addressed as appropriate.
12+
13+
## Submitting a Pull Request to Suggest an Improvement
14+
15+
If you see an opportunity for improvement and can make the change yourself go
16+
ahead and use a typical git workflow to make it happen:
17+
18+
* Fork this curriculum repository
19+
* Make the change on your fork, with descriptive commits in the standard format
20+
* Open a Pull Request against this repo
21+
22+
A curriculum owner will review your change and approve or comment on it in due
23+
course.
24+
25+
# Why Contribute?
26+
27+
Curriculum on Learn is publicly and freely available under Learn's
28+
[Educational Content License](https://learn.co/content-license). By
29+
embracing an open-source contribution model, our goal is for the curriculum
30+
on Learn to become, in time, the best educational content the world has
31+
ever seen.
32+
33+
We need help from the community of Learners to maintain and improve the
34+
educational content. Everything from fixing typos, to correcting
35+
out-dated information, to improving exposition, to adding better examples,
36+
to fixing tests—all contributions to making the curriculum more effective are
37+
welcome.

‎LICENSE.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Learn.co Educational Content License
2+
3+
Copyright (c) 2016 Flatiron School, Inc
4+
5+
The Flatiron School, Inc. owns this Educational Content. However, the Flatiron School supports the development and availability of educational materials in the public domain. Therefore, the Flatiron School grants Users of the Flatiron Educational Content set forth in this repository certain rights to reuse, build upon and share such Educational Content subject to the terms of the Educational Content License set forth [here](http://learn.co/content-license) (http://learn.co/content-license). You must read carefully the terms and conditions contained in the Educational Content License as such terms govern access to and use of the Educational Content.
6+
7+
Flatiron School is willing to allow you access to and use of the Educational Content only on the condition that you accept all of the terms and conditions contained in the Educational Content License set forth [here](http://learn.co/content-license) (http://learn.co/content-license). By accessing and/or using the Educational Content, you are agreeing to all of the terms and conditions contained in the Educational Content License. If you do not agree to any or all of the terms of the Educational Content License, you are prohibited from accessing, reviewing or using in any way the Educational Content.

‎README.md

+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
JavaScript Arithmetic Lab
2+
---
3+
4+
## Objectives
5+
6+
1. Practice doing math with JavaScript
7+
2. Practice writing functions that do things with numbers
8+
3. Practice parsing strings as numbers
9+
10+
## Introduction
11+
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!
13+
14+
## Basic Math
15+
16+
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!)
17+
18+
``` javascript
19+
1 + 80 // 81
20+
60 - 40 // 20
21+
2 * 3.4 // 7.1999999999999 (there's that floating-point arithmetic again...)
22+
5.0 / 2.5 // 2
23+
```
24+
25+
At this point, we can fix the first few broken tests: we can define functions `add()`, `subtract()`, `multiply()`, `divide()` in `index.js`.
26+
27+
## Math + Assignment
28+
29+
Additionally, we can increment (`++`) and decrement (`--`) a number if it's assigned to a variable:
30+
31+
``` javascript
32+
var number = 5
33+
34+
number++ // 5... hmmmm
35+
36+
number // 6 -- the number was incremented after it was evaluated
37+
38+
number-- // 6
39+
40+
number // 5
41+
```
42+
43+
We can also put the incrementor and decrementor operations before the number:
44+
45+
``` javascript
46+
--number // 4
47+
48+
++number // 5
49+
```
50+
51+
But generally, you will see them placed _after_ the number (and we recommend that that's where you put them).
52+
53+
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).
54+
55+
- `+=` modifies the value to the operator's left by adding to it the value to the operator's right:
56+
57+
```javascript
58+
number += 3 // 8
59+
```
60+
61+
- `-=` modifies the value to the operator's left by subtracting from it the value to the operator's right:
62+
63+
``` javascript
64+
number -= 2 // 3
65+
```
66+
67+
- `*=` modifies the value to the operator's left by multiplying it by the value to the operator's right:
68+
69+
``` javascript
70+
number *= 10 // 50
71+
```
72+
73+
- `/=` modifies the value to the operator's left by dividing it by the value to the operator's right:
74+
75+
``` javascript
76+
number /= 5 // 1
77+
```
78+
79+
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, they order in which they're called matters. Follow along in console:
80+
81+
``` javascript
82+
var number = 10
83+
84+
function add5() {
85+
number += 5
86+
}
87+
88+
function divideBy3() {
89+
number /= 3
90+
}
91+
92+
divideBy3()
93+
94+
console.log(number) // 3.333333333335
95+
96+
add5()
97+
98+
console.log(number) // 8.333333333335
99+
100+
// reset number
101+
number = 10
102+
103+
add5()
104+
105+
console.log(number) // 15
106+
107+
divideBy3()
108+
109+
console.log(number) // 5
110+
```
111+
112+
**Because these methods are more explicit, prefer `+=` to `++` and `-=` to `--` (usually).**
113+
114+
Okay, now we're ready to write solutions for `inc(n)` and `dec(n)`.
115+
116+
## Parsing Numbers
117+
118+
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.
119+
120+
Luckily, JavaScript gives us tools to turn these strings into proper numbers (that is, numbers that JavaScript understands).
121+
122+
### `parseInt()`
123+
124+
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
125+
126+
``` javascript
127+
parseInt('2', 10) // 2
128+
```
129+
130+
What happens if we pass a representation of a non-integer to `parseInt()`? Let's try it:
131+
132+
``` javascript
133+
parseInt('2.2222', 10)
134+
```
135+
136+
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?
137+
138+
What happens, though, if we pass utter nonsense to `parseInt()`? Go ahead and try it in the console — something like
139+
140+
``` javascript
141+
parseInt('nonsense!', 10)
142+
```
143+
144+
What did it return? `NaN`? What is that?
145+
146+
`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.
147+
148+
### `parseFloat()`
149+
150+
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()`.
151+
152+
Unlike `parseInt()`, `parseFloat()` accepts only a single argument, the thing to be parsed. We can use it like so:
153+
154+
``` javascript
155+
parseFloat('80.123999') // 80.123999
156+
```
157+
158+
You're now ready to solve the final tests in this lab, `makeInt(n)` and `preserveDecimal(n)`.
159+
160+
## Resources
161+
162+
- `parseInt()`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
163+
164+
- `parseFloat()`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat

‎index.js

Whitespace-only changes.

‎package.json

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "java-script-arithmetic-lab",
3+
"version": "0.1.0",
4+
"description": "Practice defining functions and operating on numbers in JavaScript",
5+
"engines": {
6+
"node": "6.x",
7+
"npm": "3.x"
8+
},
9+
"main": "index.js",
10+
"scripts": {
11+
"test": "mocha -R mocha-multi --reporter-options nyan=-,json=.results.json"
12+
},
13+
"repository": {
14+
"type": "git",
15+
"url": "git+ssh://git@github.com/learn-co-curriculum/java-script-arithmetic-lab.git"
16+
},
17+
"keywords": [
18+
"javascript",
19+
"flatiron",
20+
"learn"
21+
],
22+
"author": "pletcher",
23+
"license": "SEE LICENSE IN LICENSE.md",
24+
"bugs": {
25+
"url": "https://github.com/learn-co-curriculum/java-script-arithmetic-lab/issues"
26+
},
27+
"homepage": "https://github.com/learn-co-curriculum/java-script-arithmetic-lab#readme",
28+
"devDependencies": {
29+
"expect": "^1.20.1",
30+
"jsdom": "^9.2.1",
31+
"mocha": "^2.5.3",
32+
"mocha-jsdom": "^1.1.0",
33+
"mocha-multi": "^0.9.0"
34+
}
35+
}

‎test/index-test.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const expect = require('expect')
2+
const fs = require('fs')
3+
const jsdom = require('mocha-jsdom')
4+
const path = require('path')
5+
6+
7+
describe('index', () => {
8+
jsdom({
9+
src: fs.readFileSync(path.resolve(__dirname, '..', 'index.solution.js'), 'utf-8')
10+
})
11+
12+
var a, b
13+
14+
beforeEach(() => {
15+
a = Math.floor(Math.random() * 1000)
16+
b = Math.floor(Math.random() * 1000)
17+
})
18+
19+
it('add(a, b) adds two numbers and returns the result', () => {
20+
expect(add(a, b)).toEqual(a + b)
21+
})
22+
23+
it('subtract(a, b) subtracts b from a and returns the result', () => {
24+
expect(subtract(a, b)).toEqual(a - b)
25+
})
26+
27+
it('multiply(a, b) multiplies two numbers and returns the result', () => {
28+
expect(multiply(a, b)).toEqual(a * b)
29+
})
30+
31+
it('divide(a, b) divides a by b and returns the result', () => {
32+
expect(divide(a, b)).toEqual(a / b)
33+
})
34+
35+
it('inc(n) increments n and returns the result', () => {
36+
expect(inc(a)).toEqual(a + 1)
37+
})
38+
39+
it('dec(n) decrements n and returns the result', () => {
40+
expect(dec(a)).toEqual(a - 1)
41+
})
42+
43+
describe('makeInt(n)', () => {
44+
it('parses n as an integer and returns the parsed integer', () => {
45+
expect(makeInt(a.toString())).toEqual(a)
46+
})
47+
48+
it('assumes base 10', () => {
49+
expect(makeInt('0x2328')).toEqual(0)
50+
})
51+
52+
it('returns NaN as appropriate', () => {
53+
expect(isNaN(makeInt('sldkjflksjf'))).toEqual(true)
54+
})
55+
})
56+
57+
describe('preserveDecimal(n)', () => {
58+
it('preserves n\'s decimals (it parses n as a floating point number) and returns the parsed number', () => {
59+
expect(preserveDecimal('2.222')).toEqual(2.222)
60+
})
61+
62+
it('returns NaN as appropriate', () => {
63+
expect(isNaN(preserveDecimal('sldkjflksjf'))).toEqual(true)
64+
})
65+
})
66+
})

0 commit comments

Comments
 (0)
Please sign in to comment.