Skip to content

Instantly share code, notes, and snippets.

@endverbraucher
Last active December 21, 2017 18:48
Show Gist options
  • Save endverbraucher/1573a5e06d24d3591805f4a15e897b1f to your computer and use it in GitHub Desktop.
Save endverbraucher/1573a5e06d24d3591805f4a15e897b1f to your computer and use it in GitHub Desktop.
JavaScript Programming Exercise
// Railslove JavaScript programming exercise A1
//
// To run the following exercise you need to have mocha installed.
// Install mocha with yarn global add mocha or npm install -g mocha
//
// Then watch the output of `mocha filter.js` and see 4 of 5 failing tests.
//
// The task is to filter a given collection (L#35) for entries that contain unique pairs.
// While filtering return the same structure and keep the ranking.
// Write code in the body of the filter method to make the tests pass.
// Refactor until you're happy with your solution, by asking the follwing questions:
// * Is the code understandable/maintainable?
// * Do I really need all these if/else/elsif statements?
//
// In case you're wondering where all the semicolons are, this exercise is written
// in JavaScript Standard Style: https://standardjs.com
const assert = require('assert')
let filter = (collection, a = null, b = null) => {
// Your code goes here
return collection
}
// don't touch me
let all = [
{ '1': ['A', 'B'] },
{ '2': ['A', 'C'] },
{ '3': ['D', 'E'] },
{ '4': ['F', 'G'] },
{ '5': ['E', 'A'] },
{ '6': ['C', 'H'] },
{ '7': ['D', 'B'] }
]
filter(all, 'A')
describe('#filter', () => {
it('does not filter with empty params', () => {
assert.deepEqual(filter(all), all)
})
it('filters one result', () => {
assert.deepEqual(filter(all, 'H'), [{'6': ['C', 'H']}])
})
it('filters many results', () => {
assert.deepEqual(filter(all, 'A'), [{'1': ['A', 'B']}, {'2': ['A', 'C']}, {'5': ['E', 'A']}])
})
it('filters with 2 parameters', () => {
assert.deepEqual(filter(all, 'A', 'C'), [{'2': ['A', 'C']}])
})
it('filters order independent', () => {
assert.deepEqual(filter(all, 'C', 'A'), [{'2': ['A', 'C']}])
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment