Test Suite

For Interview

by kontrach

HTML

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine-html.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/boot.min.js"></script>
<script type="text/babel">
	'use strict'; 
   /* MOCKS Section */
	 
   const app = new Main();  
   const mk = { 
    list: [1, [2], [3, 4, [5]]],
    
    summ: (a, b) => a + b,
		
    // Very expensive operation
    rememberMe: () => {
      return 1864;
    }
    
  };  
  
  describe('Flatten (1)...', () => {   
  	let out;
  	beforeAll(() => out = app.flatten(mk.list));
    it('returns an array', () => expect(out.map).toBeDefined());
  	it('equals expected result', () => expect(out).toEqual([1, 2, 3, 4, 5]));
	});
  
  describe('Curry (2)...', () => {   
  	let out;
    let curried = app.curry(mk.summ);
  	beforeAll(() => out = curried(1)(2));
    it('returns a function', () => expect(curried.constructor === Function).toEqual(true));
    it('gives correct result', () => expect(out).toEqual(3));
	});
  
  describe('Memoize (3)...', () => {   
  	let out;
    let memoized = app.memoize(mk.rememberMe);
    beforeAll(() => {
    	out = memoized();
      memoized();
      memoized();
    });
    it('returns a function', () => expect(memoized.constructor === Function).toEqual(true));
    it('returns correct result', () => expect(out).toEqual(1864));
    
	});
</script>

CSS

/*
*                                 TASKS
*============================================================================= *
* TASK 1: Flatten
	  Given an array which might include nested arrays inside write a method which will flatten those to one array.
    For example:
      for given list: [1, [2], [3, 4, [5]]]
      The result should be equal to [1,2,3,4,5]
* ============================================================================= *      
* TASK 2: Currying
     Currying is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions
     For example:
       Given function summ: (a, b) => a + b,
       Your method should work as follows:
         let curried = app.curry(mk.summ);
         curried(1)(2); // 3
* ============================================================================= *
* TASK 3: Memoization
    Memoization is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again:
    For example: 
      let memoized = app.memoize(mk.rememberMe); // passing expensive operation
      // Takes some time:
      memoized();
      // Takes almost no time at all:
      memoized();
      memoized();
* ============================================================================= */

Babel + JSX

'use strict';
/* Implementation Section */

window.Main = class {
  flatten(list) {
    /* // ES6
    let flatten = input => input.reduce(
      (acc, val) => acc.concat(Array.isArray(val) ? flatten(val) : val), []
    );*/
    
    // OR ES5
    function flatten(list) {
      return list.reduce(function (acc, val) {
        return acc.concat(val.constructor === Array ? flatten(val) : val);
      }, []);
    }
    return flatten(list);
  }
  
  curry(func) {
    let curry = f => a => b => f(a, b);
    return curry(func);
  }
  
  memoize(func) {
    return (x) => {
      func.memo = func.memo || {};
      return (x in func.memo)? func.memo[x] : func.memo[x] = func(x); 
  	}
  }
  
  constructor() {}
}