Skip to content

Instantly share code, notes, and snippets.

@rbtcollins
Created April 22, 2015 10:23
Show Gist options
  • Save rbtcollins/89262f11b63029920e7e to your computer and use it in GitHub Desktop.
Save rbtcollins/89262f11b63029920e7e to your computer and use it in GitHub Desktop.
answers to problems
//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, range(0,100).map(x => x*2));
});
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,
people.filter(name => name[0]==='j'))
});
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, people.map(name => name[0]))
});
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, people.reduce((acc, x)=>acc+x,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, people.reduce((acc,x) => acc ? (x <= 10) : acc, 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, people.reduce((acc, x) => acc ? acc : (x > 10), 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,
people.reduce((acc, x)=>acc.concat([x[0]]), []))
});
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,
people.reduce((acc, x) => x[0] === 'j' ? acc.concat([x]) : acc , []))
});
//Nesting
it('returns the indices 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]
// array [first pos] of arrays [second pos] of arrays [pos1, pos2, sum]
var sums = productPrices.map(
(val, index) => productPrices.slice(index+1).map((x, index2)=>[index, index + index2 + 1, x+val]));
// flatten
var candidates = sums.reduce((acc, x) => acc.concat(x), []);
// dropWhile better but not available here...
var matches = candidates.filter(candidate => candidate[2] === credit);
assert.deepEqual(productIndicies, matches[0].slice(0,2))
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment