Skip to content

Commit d276674

Browse files
committed
js objects and iterators problem set
0 parents  commit d276674

File tree

5 files changed

+186
-0
lines changed

5 files changed

+186
-0
lines changed

Diff for: README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# <img src="https://cloud.githubusercontent.com/assets/7833470/10899314/63829980-8188-11e5-8cdd-4ded5bcb6e36.png" height="60"> Javascript Objects - Problem Set
2+
3+
* Please **Fork & Clone** this repo.
4+
* For each problem, modify the relevant file in the `challenges`folder. We encourage you to *commit often* and push your changes to github.
5+
* You will [submit this problem set for review](https://github.com/SF-WDI-LABS/shared_modules/blob/master/how-to/submit-homework.md).
6+
7+
## Challenges
8+
1. [Letter Count](/challenges/letter-count.js)
9+
2. [Query String Parser](/challenges/query-string-parser.js)
10+
3. [Birthday Countdown](/challenges/birthday-countdown.js)
11+
4. [Ping Pong](/challenges/ping-pong.js)
12+
13+
#### Key Concepts
14+
* Working with **key-value pairs**
15+
* Drilling into complex objects using **dot notation** and **bracket notation**
16+
* Dynamically setting keys in **objects** (i.e. keys are not hard-coded).
17+
* Using Array **iteration** methods.
18+
19+
## Executing your code
20+
There are a number of options for running/executing your code. They all have their uses, so we encourage you to learn all of them:
21+
22+
- From the browser:
23+
- **Using the [Chrome Developer Console](https://developers.google.com/web/tools/chrome-devtools/debug/console/console-ui?hl=en#opening-the-console)**
24+
* Tip: Use `` and `` to cycle through your code history. Pressing `` selects the current suggestion. If there’s a single suggestion, `Tab` to select it.
25+
+ **Using [Javascript Snippets](https://developers.google.com/web/tools/chrome-devtools/debug/snippets/)** to execute and save your scripts.
26+
- From the command line:
27+
+ **Using the [Node REPL / Shell](http://www.nodelabs.org/repl.html)**
28+
* Tip: Type `.exit` to quit. Use `` and `` to cycle through your code history.
29+
+ **Using the [`node some-file.js` command](http://javascript.cs.lmu.edu/notes/commandlinejs/)**
30+
* Tip: Use `ctrl` + `c` (shortcut) to quit "hanging" programs. And make sure to `console.log("your output")`.

Diff for: challenges/birthday-countdown.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
3+
Create a function `daysUntilDate` that accepts a string (with the format `"mm/dd/yyyy"`) and
4+
returns the number of days (integer) between today and that date. Please use the built in
5+
Javascript `Date` type in your solution.
6+
7+
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
8+
9+
Next, create a function `birthdayReminder` that accepts an array of objects, containing a person's
10+
date of birth (dob), and returns an array of reminder messages (strings).
11+
12+
```javascript
13+
birthdayReminder([
14+
{
15+
name: "Jack",
16+
dob: "10/31/2013"
17+
},
18+
{
19+
name: "Jill",
20+
dob: "4/01/1975"
21+
}
22+
]);
23+
24+
//=> [
25+
// "Jack's birthday is in 75 days",
26+
// "Jill's birthday is in 305 days"
27+
// ]
28+
```
29+
30+
Bonuses
31+
- Will your function still work tomorrow when the date is different? Is it future proofed?
32+
- To make your output more relevant, can you sort by days remaining (ascending)?
33+
34+
*/
35+
36+
// YOUR CODE HERE

Diff for: challenges/letter-count.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
3+
Create a function `letterCount` that accepts a string, and finds the number of times each letter
4+
occurs in the string. For example, given the word "apple", letterCount("apple") should count all
5+
occurrences of the letters "a", "p", "l" and "e" and then return the following output:
6+
7+
```javascript
8+
{
9+
"a": 1,
10+
"p": 2,
11+
"l": 1,
12+
"e": 1
13+
}
14+
```
15+
16+
Bonuses
17+
- Make sure that lower case letters and upper case letters count for the same character.
18+
- Ignore spaces, special characters, and punctuation.
19+
- Instead of just counting letters, calculate their percent-based frequency.
20+
See: http://www.math.cornell.edu/~mec/2003-2004/cryptography/subs/frequencies.html
21+
22+
```javascript
23+
{
24+
"a": 0.2, // percent
25+
"p": 0.4,
26+
"l": 0.2,
27+
"e": 0.2
28+
}
29+
```
30+
31+
*/
32+
33+
// YOUR CODE HERE

Diff for: challenges/ping-pong.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
3+
In this challenge you will simulate the movement of a ping-pong, bouncing back and forth across a
4+
table.
5+
6+
Create a function `pingPong` that accepts an array, and returns the *same* array
7+
with the ping-pong moved one cell. Each time the ping-pong moves, you must increment the
8+
ping-pong's `steps` counter by 1. The direction of movement should be determined soley by the
9+
current state of the array and the current `steps` value of the ping-pong.
10+
11+
``` javascript
12+
var table = [{steps: 0}, null, null, null];
13+
14+
pingPong(table); //=> [null, {steps: 1}, null, null]
15+
pingPong(table); //=> [null, null, {steps: 2}, null]
16+
pingPong(table); //=> [null, null, null, {steps: 3}]
17+
pingPong(table); //=> [null, null, {steps: 4}, null]
18+
pingPong(table); //=> [null, {steps: 5}, null, null]
19+
pingPong(table); //=> [{steps: 6}, null, null, null]
20+
pingPong(table); //=> [null, {steps: 7}, null, null]
21+
22+
table; //=> [null, {steps: 7}, null, null]
23+
```
24+
25+
Keep in mind that I should be able start this process at *any* point:
26+
27+
```
28+
var table = [null, {steps: 13}, null, null];
29+
pingPong(table); //=> [null, null, {steps: 14}, null]
30+
```
31+
32+
Bonuses
33+
- Allow arrays of *any length*
34+
- We can think of the ping-pong as having an internal "speed" of 1. Allow this value to be
35+
explicity set at a higher number (i.e. move 2 cells at a time, or 3 cells at a time).
36+
- Support multiple ping-pongs.
37+
38+
*/
39+
40+
// YOUR CODE HERE

Diff for: challenges/query-string-parser.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
3+
Create a function `parseQueryString` that accepts a query string parameter as an argument, and
4+
converts it into an object, using the following rules:
5+
6+
* An equals sign (`=`) separates a *key* on the left from a *value* on the right.
7+
* An ampersand (`&`) separates key-value pairs from each other.
8+
* All keys and values should be parsed as Strings.
9+
* The query string will not contain spaces.
10+
11+
Here are some example inputs and outputs (mind the edge cases!):
12+
13+
```javascript
14+
parseQueryString("");
15+
//=> {}
16+
17+
parseQueryString("a=1");
18+
//=> {
19+
// "a": "1",
20+
// }
21+
22+
parseQueryString("first=alpha&last=omega");
23+
//=> {
24+
// "first": "alpha",
25+
// "last": "omega"
26+
// }
27+
28+
parseQueryString("a=apple&b=beet&b=blueberry&c=&d=10");
29+
//=> {
30+
// "a": "apple",
31+
// "b": "blueberry", // "blueberry" overwrites "beet"!
32+
// "c": "", // empty string (missing value)
33+
// "d": "10" // "10" is a String!
34+
// }
35+
```
36+
37+
Mega Bonus
38+
- Can you create the reverse function? Given an object, output a Query Parameter String:
39+
40+
``` javascript
41+
var o = {first: "alpha", last: "omega"};
42+
convertToQueryParameter(o); // "first=alpha&last=omega";
43+
```
44+
45+
*/
46+
47+
// YOUR CODE HERE

0 commit comments

Comments
 (0)