Avatar of brunnock

brunnock's solution

to Strain in the JavaScript Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Implement the keep and discard operation on collections. Given a collection and a predicate on the collection's elements, keep returns a new collection containing those elements where the predicate is true, while discard returns a new collection containing those elements where the predicate is false.

For example, given the collection of numbers:

  • 1, 2, 3, 4, 5

And the predicate:

  • is the number even?

Then your keep operation should produce:

  • 2, 4

While your discard operation should produce:

  • 1, 3, 5

Note that the union of keep and discard is all the elements.

The functions may be called keep and discard, or they may need different names in order to not clash with existing functions or concepts in your language.

Restrictions

Keep your hands off that filter/reject/whatchamacallit functionality provided by your standard library! Solve this one yourself using other basic tools instead.

Setup

Go through the setup instructions for JavaScript to install the necessary dependencies:

http://exercism.io/languages/javascript/installation

Running the test suite

The provided test suite uses Jasmine. You can install it by opening a terminal window and running the following command:

npm install -g jasmine

Run the test suite from the exercise directory with:

jasmine strain.spec.js

In many test suites all but the first test have been marked "pending". Once you get a test passing, activate the next one by changing xit to it.

Source

Conversation with James Edward Gray II https://twitter.com/jeg2

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

strain.spec.js

var strain = require('./strain');

describe('strain', function () {
  it('keeps on empty array returns empty array', function () {
    expect(strain.keep([], function (e) { return e < 10; })).toEqual([]);
  });

  xit('keeps everything ', function () {
    expect(strain.keep([1, 2, 3], function (e) { return e < 10; })).toEqual([1, 2, 3]);
  });

  xit('keeps first and last', function () {
    expect(strain.keep([1, 2, 3], function (e) { return (e % 2) === 1; })).toEqual([1, 3]);
  });

  xit('keeps neither first nor last', function () {
    expect(strain.keep([1, 2, 3, 4, 5], function (e) { return (e % 2) === 0; })).toEqual([2, 4]);
  });

  xit('keeps strings', function () {
    var words = 'apple zebra banana zombies cherimoya zelot'.split(' ');
    var result = strain.keep(words, function (word) { return word.indexOf('z') === 0; });
    expect(result).toEqual('zebra zombies zelot'.split(' '));
  });

  xit('keeps arrays', function () {
    var rows = [
      [1, 2, 3],
      [5, 5, 5],
      [5, 1, 2],
      [2, 1, 2],
      [1, 5, 2],
      [2, 2, 1],
      [1, 2, 5]
    ];
    var result = strain.keep(rows, function (row) { return row.indexOf(5) > -1; });
    expect(result).toEqual([[5, 5, 5], [5, 1, 2], [1, 5, 2], [1, 2, 5]]);
  });

  xit('empty discard', function () {
    expect(strain.discard([], function (e) { return e < 10;})).toEqual([]);
  });

  xit('discards nothing', function () {
    expect(strain.discard([1, 2, 3], function (e) { return e > 10; })).toEqual([1, 2, 3]);
  });

  xit('discards first and last', function () {
    expect(strain.discard([1, 2, 3], function (e) { return e % 2 === 1; })).toEqual([2]);
  });

  xit('discards neither first nor last', function () {
    var result = strain.discard([1, 2, 3, 4, 5], function (e) { return e % 2 === 0; });
    expect(result).toEqual([1, 3, 5]);
  });

  xit('discards strings', function () {
    var words = 'apple zebra banana zombies cherimoya zelot'.split(' ');
    var result = strain.discard(words, function (word) { return word.indexOf('z') === 0; });
    expect(result).toEqual('apple banana cherimoya'.split(' '));
  });

  xit('discards arrays', function () {
    var rows = [
      [1, 2, 3],
      [5, 5, 5],
      [5, 1, 2],
      [2, 1, 2],
      [1, 5, 2],
      [2, 2, 1],
      [1, 2, 5]
    ];
    var result = strain.discard(rows, function (row) { return row.indexOf(5) > -1; });
    expect(result).toEqual([[1, 2, 3], [2, 1, 2], [2, 2, 1]]);
  });
});
module.exports = {

    keep : function (list, predicate) {
	var list2=[];
	for (var x=0; x<list.length; ++x) {
	    if (predicate(list[x])) list2.push(list[x]);
	}
	return list2;
    },

    discard : function (list, predicate) {
	return this.keep(list, function(e) {return !predicate(e)});
    }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?