Skip to content

Commit b004875

Browse files
committed
Major refactor to module.exports over $magic vars
Also tidied some problem.txts lacking info. Closes #9 as suggested by @brianloveswords in #7.
1 parent 94e015c commit b004875

File tree

28 files changed

+286
-191
lines changed

28 files changed

+286
-191
lines changed

Diff for: input.js

+35-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,42 @@
1-
module.exports = function(input, hydrate) {
2-
hydrate = hydrate || function() {}
3-
return function() {
4-
return {
5-
args: [JSON.stringify(input), hydrate.toString()],
1+
var path = require('path')
2+
3+
var serialize = function(input) {
4+
if (typeof input !== 'function') return JSON.stringify(input)
5+
return JSON.stringify({
6+
isFunction: true,
7+
body: input.toString()
8+
})
9+
}
10+
11+
var deserialize = function(input) {
12+
var out = JSON.parse(input)
13+
if (!out.isFunction) return out
14+
eval('out = (' + out.body + ')')
15+
return out
16+
}
17+
18+
module.exports = function() {
19+
var args = Array.prototype.slice.call(arguments).map(serialize)
20+
21+
function input() {
22+
var opts = {
23+
args: args,
624
solutionExecWrap: __filename,
7-
execWrap: __filename
25+
execWrap: __filename,
826
}
27+
return opts
928
}
29+
input.wrap = function(fn) {
30+
args.push(serialize(fn))
31+
return input
32+
}
33+
return input
34+
1035
}
1136

1237
module.exports.init = function() {
13-
global.$input = JSON.parse(process.argv[4])
14-
eval('(' +process.argv[5] + ')()') // whatever.
38+
var mod = require(path.resolve(process.cwd(), process.argv[3]))
39+
var args = process.argv.slice(4).map(deserialize)
40+
var wrap = args.pop()
41+
wrap(args, mod)
1542
}

Diff for: problems/basic_filter/problem.txt

+12-12
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ Use Array#filter to write a function called `getShortMessages`.
33
`getShortMessages` takes an array of objects with '.message' properties
44
and prints any messages that are *less than < 50 characters long*.
55

6+
Arguments:
67

7-
$input will be an Array of 10 to 100 random objects that look something like this:
8+
* messages: an Array of 10 to 100 random objects that look something like this:
89

910
```js
1011

@@ -14,8 +15,15 @@ $input will be an Array of 10 to 100 random objects that look something like thi
1415

1516
```
1617

17-
The console output should be of an array containing the messages themselves
18-
(i.e. messages *not in their containing object*).
18+
Conditions:
19+
20+
* Do not use for loops or Array#forEach.
21+
22+
Hint: Try chaining some Array methods!
23+
24+
Expected Output:
25+
26+
The output should be an array containing the messages themselves, without their containing object.
1927

2028
e.g.
2129

@@ -28,10 +36,6 @@ e.g.
2836

2937
```
3038

31-
Do not use for loops or Array#forEach.
32-
33-
Hint: try chaining some array methods!
34-
3539
Resources:
3640

3741
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
@@ -43,12 +47,8 @@ Resources:
4347

4448
```js
4549

46-
function getShortMessages(arr) {
50+
module.exports = function getShortMessages(messages) {
4751
// SOLUTION GOES HERE
4852
}
4953

50-
// do not edit below this line
51-
52-
console.log(getShortMessages($input))
53-
5454
```

Diff for: problems/basic_filter/setup.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
var input = require('../../input')
22

3-
function randomInts(min, max) {
4-
return Math.floor(Math.random() * max + min - 1)
3+
function randomInt(min, max) {
4+
return Math.floor((Math.random() * max - min) + min)
55
}
66

7-
module.exports = input(new Array(randomInts(10, 100))
7+
module.exports = input(new Array(randomInt(10, 100))
88
.join(',').split(',')
99
.map(function() {
10-
return {
11-
message: require('lorem-ipsum')()
12-
}
13-
}))
10+
return { message: require('lorem-ipsum')() }
11+
})).wrap(function(input, getShortMessages) {
12+
var messages = input[0]
13+
console.log(getShortMessages(messages))
14+
})

Diff for: problems/basic_filter/solution.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
function getShortMessages(arr) {
2-
return arr.filter(function(item) {
1+
module.exports = function getShortMessages(messages) {
2+
return messages.filter(function(item) {
33
return item.message.length < 50
44
}).map(function(item) {
55
return item.message
66
})
77
}
8-
9-
// do not edit below this line
10-
11-
console.log(getShortMessages($input))

Diff for: problems/basic_map/problem.txt

+35-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,43 @@
1-
Convert the following code from a for-loop to Array#map.
1+
Convert the following code from a for-loop to Array#map:
22

3-
The variable $input will be injected into your program.
4-
$input will be an Array of 1 to 20 Integers between 0 and 9
5-
Make sure to console.log your result.
3+
```js
4+
5+
function doubleAll(numbers) {
6+
var result = []
7+
for (var i = 0; i < input.length; i++) {
8+
result.push(input[i] * 2)
9+
}
10+
return result
11+
}
12+
13+
module.exports = doubleAll
14+
15+
```
16+
17+
Arguments:
18+
19+
* numbers: An Array of 1 to 20 Integers between 0 and 9
20+
21+
Conditions:
22+
23+
* Do not use any for/while loops.
24+
* You do not need to define any additional functions
25+
unless a stub is provided in the boilerplate.
26+
27+
Resources:
28+
29+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
30+
31+
#################
32+
## Boilerplate ##
33+
#################
634

735
```js
836

9-
var result = []
10-
for (var i = 0; i < $input.length; i++) {
11-
result.push($input[i] * 2)
37+
function doubleAll(numbers) {
38+
// SOLUTION GOES HERE
1239
}
1340

14-
console.log(result)
41+
module.exports = doubleAll
1542

1643
```

Diff for: problems/basic_map/setup.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,27 @@
22

33
var input = require('../../input')
44

5-
function randomInts(min, max) {
6-
return Math.floor(Math.random() * max + min - 1)
5+
function randomInt(min, max) {
6+
return Math.floor((Math.random() * max - min) + min)
77
}
88

9-
module.exports = input(new Array(randomInts(0, 19))
9+
module.exports = input(new Array(randomInt(0, 19))
1010
.join(',')
1111
.split(',')
1212
.map(function() {
13-
return randomInts(0, 9)
14-
}), function() {
13+
return randomInt(0, 9)
14+
})).wrap(function(input, doubleAll) {
15+
var numbers = input[0]
1516
var map = Array.prototype.map
1617
var usedMap = false
1718
Array.prototype.map = function() {
1819
usedMap = true
1920
return map.apply(this, arguments)
2021
}
22+
23+
console.log('input:', numbers)
24+
console.log('output:', doubleAll(numbers))
25+
2126
setTimeout(function() {
2227
if (!usedMap) {
2328
console.log('You didn\'t use Array#map!!??')

Diff for: problems/basic_map/solution.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
function doubleAll(arr) {
2-
return arr.map(function double(num) {
1+
module.exports = function doubleAll(numbers) {
2+
return numbers.map(function double(num) {
33
return num * 2
44
})
55
}
6-
7-
console.log(doubleAll($input))

Diff for: problems/basic_reduce/problem.txt

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
Array#reduce
22

33
Task: Given an Array of strings, use Array#reduce to create an object that
4-
contains the number of times each string occured in the array.
4+
contains the number of times each string occured in the array. Return the
5+
object directly (no need to console.log).
56

6-
Example:
7+
#############
8+
## Example ##
9+
#############
710

811
```js
912

10-
var $input = ['Apple', 'Banana', 'Apple', 'Durian', 'Durian', 'Durian']
13+
var inputWords = ['Apple', 'Banana', 'Apple', 'Durian', 'Durian', 'Durian']
1114

12-
console.log(countStrings($input))
15+
console.log(countWords(inputWords))
1316

1417
// =>
1518
// {
@@ -20,9 +23,9 @@ console.log(countStrings($input))
2023

2124
```
2225

23-
Injected Variables:
26+
Arguments:
2427

25-
* $input will be a Array of random Strings.
28+
* inputWords: An array of random Strings.
2629

2730
Conditions:
2831

@@ -41,12 +44,13 @@ Resources:
4144

4245
```js
4346

44-
function countStrings(arr) {
45-
// SOLUTION GOES HERE
47+
function countWords(inputWords) {
48+
return inputWords.reduce(function(prev, curr) {
49+
prev[curr] = ++prev[curr] || 1
50+
return prev
51+
}, {})
4652
}
4753

48-
// do not edit below this line
49-
50-
console.log(countStrings($input))
54+
module.exports = countWords
5155

5256
```

Diff for: problems/basic_reduce/setup.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ module.exports = input(lorem({count: 1, units:'paragraphs'})
77
.replace(/([^\w ])/g, '')// remove non-words and spaces
88
.toLowerCase() // lowercase I guess
99
.split(' ') // create array of words
10-
)
10+
).wrap(function(input, countStrings) {
11+
console.log(countStrings(input[0]))
12+
})

Diff for: problems/basic_reduce/solution.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
function countStrings(arr) {
1+
function countWords(arr) {
22
return arr.reduce(function(prev, curr) {
33
prev[curr] = ++prev[curr] || 1
44
return prev
55
}, {})
66
}
77

8-
// do not edit below this line
9-
10-
console.log(countStrings($input))
8+
module.exports = countWords

Diff for: problems/function_spies/problem.txt

+23-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
Overide a method with new functionality while still maintaining all of the old
1+
Overide a specified method of an object with new functionality while still maintaining all of the old
22
behaviour.
33

44
Create a spy that keeps track of how many times a function is is called.
55

6-
e.g.
7-
6+
#############
7+
## Example ##
8+
#############
89

910
```js
1011
var spy = Spy(console, 'error')
@@ -16,15 +17,28 @@ console.error('calling console.error')
1617
console.log(spy.count) // 3
1718

1819
```
19-
Use this boilerplate:
2020

21-
```
22-
function Spy(object, methodName) {
21+
Arguments:
22+
23+
* target: an object containing the method `method`
24+
* method: a string the name of the method on `target`.
25+
26+
Conditions:
27+
28+
* Do not use any for/while loops.
29+
* You do not need to define any additional functions
30+
unless a stub is provided in the boilerplate.
31+
32+
#################
33+
## Boilerplate ##
34+
#################
35+
36+
```js
37+
38+
function Spy(target, method) {
2339
// SOLUTION GOES HERE
2440
}
2541

26-
// do not edit below this line
27-
28-
$test(Spy)
42+
module.exports = Spy
2943

3044
```

0 commit comments

Comments
 (0)