Avatar of brunnock

brunnock's solution

to Accumulate in the JavaScript Track

Published at Jul 13 2018 · 0 comments
Test suite


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 accumulate operation, which, given a collection and an operation to perform on each element of the collection, returns a new collection containing the result of applying that operation to each element of the input collection.

Given the collection of numbers:

  • 1, 2, 3, 4, 5

And the operation:

  • square a number (x => x * x)

Your code should be able to produce the collection of squares:

  • 1, 4, 9, 16, 25

Check out the test suite to see the expected function signature.


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


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


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 accumulate.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.


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.


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

describe('accumulate()', function () {
  it('accumulation empty', function () {
    var accumulator = function (e) { return e * e; };
    expect(accumulate([], accumulator)).toEqual([]);

  xit('accumulate squares', function () {
    var accumulator = function (number) {
      return number * number;

    var result = accumulate([1, 2, 3], accumulator);

    expect(result).toEqual([1, 4, 9]);

  xit('accumulate upcases', function () {
    var accumulator = function (word) {
      return word.toUpperCase();

    var result = accumulate('hello world'.split(/\s/), accumulator);

    expect(result).toEqual(['HELLO', 'WORLD']);

  xit('accumulate reversed strings', function () {
    var accumulator = function (word) {
      return word.split('').reverse().join('');

    var result = accumulate('the quick brown fox etc'.split(/\s/), accumulator);

    expect(result).toEqual(['eht', 'kciuq', 'nworb', 'xof', 'cte']);

  xit('accumulate recursively', function () {
    var result = accumulate('a b c'.split(/\s/), function (char) {
      return accumulate('1 2 3'.split(/\s/), function (digit) {
        return char + digit;

    expect(result).toEqual([['a1', 'a2', 'a3'], ['b1', 'b2', 'b3'], ['c1', 'c2', 'c3']]);
function accumulate(list,op) {
  for (var x=0; x<list.length; ++x) 
    list[x] = op(list[x]);
  return list;


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?