Created
April 21, 2015 05:47
-
-
Save anotheredward/ebbe30c6c2ac8cba0703 to your computer and use it in GitHub Desktop.
Functional Programming Exercise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Copy this over the contents at tddbin.com | |
//Replace the [] in the assert statements with a functional equivalant | |
//Reference here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array | |
//Lambda syntax: (x,y) => x + y, works in firefox, try it out :) | |
function range(start, end, jump) { | |
jump = jump || 1 | |
if (start && !end) | |
return actualRange(0, start, jump) | |
return actualRange(start, end, jump) | |
} | |
function actualRange(start, end, jump) { | |
var result = [] | |
for (var i = start; i < end; i += jump) | |
result.push(i) | |
return result | |
} | |
describe('For loops vs Map/Filter/Reduce', function() { | |
it('creates the same list', function() { | |
var numbers = []; | |
for (var i = 0; i < 100; i++){ | |
numbers.push(i * 2) | |
} | |
assert.deepEqual(numbers, []); | |
}); | |
it('creates the same list', function() { | |
var people = ['james', 'jane', 'anne']; | |
var peopleStartingWithJ = []; | |
for (var i = 0; i < people.length; i++) { | |
if (people[i][0] === 'j') | |
peopleStartingWithJ.push(people[i]) | |
} | |
assert.deepEqual(peopleStartingWithJ, []) | |
}); | |
it('creates the same list', function() { | |
var people = ['james', 'jane', 'anne']; | |
var firstLetterOfName = []; | |
for (var i = 0; i < people.length; i++) { | |
firstLetterOfName.push(people[i][0]) | |
} | |
assert.deepEqual(firstLetterOfName, []) | |
}); | |
it('creates the same list', function() { | |
var people = [5, 10, 15] | |
var sum = 0 | |
for (var i = 0; i < people.length; i++) { | |
sum += people[i] | |
} | |
assert.equal(sum, 0) | |
}); | |
it('creates the same list', function() { | |
var people = [11, 10, 15] | |
var allLargerThan10 = true; | |
for (var i = 0; i < people.length; i++) { | |
if (people[i] <= 10) { | |
allLargerThan10 = false; | |
break; | |
} | |
} | |
assert.deepEqual(allLargerThan10, true) | |
}); | |
it('creates the same list', function() { | |
var people = [5, 10, 15] | |
var anyLargerThan10 = false; | |
for (var i = 0; i < people.length; i++) { | |
if (people[i] > 10) { | |
anyLargerThan10 = true; | |
break; | |
} | |
} | |
assert.deepEqual(anyLargerThan10, false) | |
}); | |
//Reduce Challenges | |
//Note: I'm not encouraging using reduce everywhere, this is just an exercise | |
it('solves this using reduce', function() { | |
var people = ['james', 'jane', 'anne']; | |
var firstLetterOfName = []; | |
for (var i = 0; i < people.length; i++) { | |
firstLetterOfName.push(people[i][0]) | |
} | |
assert.deepEqual(firstLetterOfName, []) | |
}); | |
it('solves this using reduce', function() { | |
var people = ['james', 'jane', 'anne']; | |
var peopleStartingWithJ = []; | |
for (var i = 0; i < people.length; i++) { | |
if (people[i][0] === 'j') | |
peopleStartingWithJ.push(people[i]) | |
} | |
assert.deepEqual(peopleStartingWithJ, []) | |
}); | |
//Nesting | |
it('returns the indicies of two product prices that add up to credit', function() { | |
var credit = 100; | |
var productPrices = [5, 75, 25] | |
var productIndicies = [0,0]; | |
for (var i = 0; i < productPrices.length; i++) | |
for (var j = i + 1; j < productPrices.length; j++) | |
if (productPrices[i] + productPrices[j] === credit) productIndicies = [i,j] | |
assert.deepEqual(productIndicies, []) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment