├── .editorconfig ├── .gitignore ├── .travis.yml ├── README.md ├── compiled ├── array-is-equal.js ├── array │ ├── flatten.js │ ├── remove-from-array.js │ └── reverse.js ├── interview-questions │ ├── given-sum.js │ ├── is-palindrome.js │ ├── reorder-by-indexes.js │ ├── reverse-string.js │ └── string-contains.js └── math │ ├── factorial.js │ ├── fibonacci.js │ ├── greatest-common-divisor.js │ ├── is-prime.js │ ├── permutation.js │ └── simple-combination.js ├── gulpfile.js ├── package.json ├── src ├── array │ ├── __tests__ │ │ ├── concat-test.js │ │ ├── flatten-test.js │ │ ├── intersection-test.js │ │ ├── remove-duplicates-test.js │ │ ├── remove-from-array-test.js │ │ ├── reverse-test.js │ │ └── union-test.js │ ├── concat.js │ ├── flatten.js │ ├── intersection.js │ ├── remove-duplicates.js │ ├── remove-from-array.js │ ├── reverse.js │ └── union.js ├── interview-questions │ ├── __tests__ │ │ ├── given-sum-test.js │ │ ├── is-palindrome-test.js │ │ ├── reorder-by-indexes.js │ │ ├── reverse-string-test.js │ │ └── string-contains-test.js │ ├── given-sum.js │ ├── is-palindrome.js │ ├── reorder-by-indexes.js │ ├── reverse-string.js │ └── string-contains.js └── math │ ├── __tests__ │ ├── factorial-test.js │ ├── fibonacci-test.js │ ├── greatest-common-divisor.js │ ├── is-prime-test.js │ ├── permutation-test.js │ └── simple-combination-test.js │ ├── factorial.js │ ├── fibonacci.js │ ├── greatest-common-divisor.js │ ├── is-prime.js │ ├── permutation.js │ └── simple-combination.js └── utils ├── __tests__ └── array-is-equal-test.js ├── array-is-equal.js └── es6-transformer.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = spaces 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | node_modules 3 | npm-debug.log -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Algorithms with ES6 2 | 3 | This is just a lab to test how classic algorithms written in Ecmascript 6 would look like. The code result is incredible, because it resolves a lot of problems that we used to have while writting Javascript. 4 | 5 | The list of Algorithms so far: 6 | 7 | ## Math Expressions 8 | 9 | Algorithm | Description 10 | --------- | ----------- 11 | [factorial](/src/math/factorial.js) | Factorial algorithm 12 | [fibonacci](/src/math/fibonacci.js) | Fibonacci algorithm 13 | [greatestCommonDivisor](/src/math/greatest-common-divisor.js) | Get the greatest common divisor of two numbers 14 | [isPrime](/src/math/is-prime.js) | Verify if is a prime number 15 | [permutation](/src/math/permutation.js) | Permutation algorithm 16 | [simpleCombination](/src/math/simple-combination.js) | Simple combination algorithm 17 | 18 | ## Array functions 19 | 20 | Algorithm | Description 21 | --------- | ----------- 22 | [flatten](/src/array/flatten.js) | Flatten an array 23 | [removeFromArr](/src/array/remove-from-array.js) | Remove an item from an array 24 | [reverse](/src/array/reverse.js) | Reverse an array 25 | [removeDuplicates](/src/array/remove-duplicates.js) | Remove duplicates item in array 26 | [concat](/src/array/concat.js) | Concatenate arrays 27 | [intersection](/src/array/intersection.js) | Intersection between arrays 28 | [union](/src/array/union.js) | Union between arrays 29 | 30 | ## Interview Questions 31 | 32 | Algorithm | Description 33 | --------- | ----------- 34 | [givenSum](/src/interview-questions/given-sum.js) | Get two numbers in an array that the sum is equal a other number that is passed as second parameter 35 | [isPalindrome](/src/interview-questions/is-palindrome.js) | Return if word is a [palindrome](http://en.wikipedia.org/wiki/Palindrome) 36 | [reorderByIndexes](/src/interview-questions/reorder-by-indexes.js) | Reorder an array based on other array with indexes 37 | [reverseStr](/src/interview-questions/reverse-string.js) | Reverses a given string 38 | 39 | You can see the compiled codes [at here](/compiled). 40 | 41 | ## How to run 42 | 43 | 1. Make sure to install all modules 44 | ```bash 45 | $ npm install 46 | ``` 47 | 1. After making any changes, add your tests to `__tests__` folder and run the test command 48 | ```bash 49 | $ npm test 50 | ``` 51 | 1. To build from ES6 to ES5: 52 | ```bash 53 | $ gulp build 54 | ``` 55 | 56 | Enjoy! :ghost: 57 | -------------------------------------------------------------------------------- /compiled/array-is-equal.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var _arrIsEqual = function(arrX, arrY) { 3 | if (arrX.length !== arrY.length) return false; 4 | if (!Array.isArray(arrX) || !Array.isArray(arrY)) return false; 5 | 6 | var _ref = [-1, true]; 7 | var _index = _ref[0]; 8 | var _isEqual = _ref[1]; 9 | 10 | while (++_index <= arrX.length) { 11 | if (arrX[_index] !== arrY[_index]) { 12 | _isEqual = false; 13 | } 14 | } 15 | 16 | return _isEqual; 17 | }; 18 | 19 | module.exports = _arrIsEqual; 20 | -------------------------------------------------------------------------------- /compiled/array/flatten.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var _flatten = function(arr, givenArr) { 3 | if (givenArr === undefined) 4 | givenArr = []; 5 | 6 | arr.forEach(function(item) { 7 | (Array.isArray(item)) && (_flatten(item, givenArr)); 8 | (!Array.isArray(item)) && (givenArr.push(item)); 9 | }); 10 | 11 | return givenArr; 12 | }; 13 | 14 | module.exports = _flatten; 15 | -------------------------------------------------------------------------------- /compiled/array/remove-from-array.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var _removeFromArr = function(arr, val) { 3 | arr.forEach(function(num, i) { if (num === val) arr.splice(i, 1) }); 4 | }; 5 | 6 | module.exports = _removeFromArr; 7 | -------------------------------------------------------------------------------- /compiled/array/reverse.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var _reverse = function(arr) { 3 | var _index = arr.length; 4 | return arr.map(function(_index) { 5 | return function() { 6 | return arr[--_index]; 7 | }; 8 | }(_index)); 9 | }; 10 | 11 | module.exports = _reverse; 12 | -------------------------------------------------------------------------------- /compiled/interview-questions/given-sum.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var _arrIsEqual = require('../../utils/array-is-equal'); 3 | 4 | var _givenSum = function(arr, sum) { 5 | var _nums = []; 6 | 7 | arr.forEach(function(_nums) { 8 | return function(x) { 9 | arr.forEach(function(y) { 10 | var _contains = _nums.filter(function(arr) { 11 | return _arrIsEqual(arr, [y,x]); 12 | }); 13 | ((x + y === sum) && (_contains.length === 0)) && (_nums.push([x, y])); 14 | }); 15 | }; 16 | }(_nums)); 17 | 18 | return _nums.sort(function(a, b) { 19 | return a[0] - b[0]; 20 | }); 21 | }; 22 | 23 | module.exports = _givenSum; 24 | -------------------------------------------------------------------------------- /compiled/interview-questions/is-palindrome.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var _isPalindrome = function(word) { 3 | var _ref = [word.length, true]; 4 | var _len = _ref[0]; 5 | var _result = _ref[1]; 6 | 7 | for (var _i = 0; _i < _len / 2; _i++) { 8 | if (word[_i] !== word[_len - 1 - _i]) _result = false; 9 | } 10 | 11 | return _result; 12 | }; 13 | 14 | module.exports = _isPalindrome; 15 | -------------------------------------------------------------------------------- /compiled/interview-questions/reorder-by-indexes.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var _reorderByIndexes = function(arr, indexes) { 3 | return indexes.map(function(i) { 4 | return arr[i]; 5 | }); 6 | }; 7 | 8 | module.exports = _reorderByIndexes; 9 | -------------------------------------------------------------------------------- /compiled/interview-questions/reverse-string.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var _arrayReverse = require('../array/reverse'); 3 | 4 | var _reverseStr = function(str) { 5 | return _arrayReverse(str.split('')).join(''); 6 | }; 7 | 8 | var _rReverseStr = function(str) { 9 | var _lastChar = str.length - 1; 10 | return !str ? '' : str[_lastChar] + _rReverseStr(str.substr(0, _lastChar)); 11 | }; 12 | 13 | module.exports = { 14 | rReverseStr: _rReverseStr, 15 | reverseStr: _reverseStr 16 | }; 17 | -------------------------------------------------------------------------------- /compiled/interview-questions/string-contains.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var _stringContains = function(str, substr) { 3 | var contains = false; 4 | var _ref = [substr.split(''), str.split('')]; 5 | var _splitSubstr = _ref[0]; 6 | var _splitStr = _ref[1]; 7 | 8 | _splitStr.forEach(function(_splitSubstr, _splitStr) { 9 | return function(letter, i) { 10 | (_splitSubstr.map(function(foundLetter, j) { 11 | if (_splitStr[i + (j - 1)] === foundLetter) return _splitStr[i + (j - 1)]; 12 | }).join('') === substr) && (contains = true); 13 | }; 14 | }(_splitSubstr, _splitStr)); 15 | 16 | return contains; 17 | }; 18 | 19 | var _rStrContains = function(a, b) { 20 | return !a ? false : (a === b ? true : _rStrContains(a.substr(1), b)); 21 | }; 22 | 23 | module.exports = { 24 | stringContains: _stringContains, 25 | rStrContains: _rStrContains 26 | }; 27 | -------------------------------------------------------------------------------- /compiled/math/factorial.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var _factorial = function(num) { 3 | var _result = 1; 4 | while (--num) _result *= num + 1; 5 | return _result; 6 | }; 7 | 8 | module.exports = _factorial; 9 | -------------------------------------------------------------------------------- /compiled/math/fibonacci.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var _fibonacci = function(num) { 3 | if (num <= 1) return num; 4 | var _ref = [0, 1]; 5 | var _previous = _ref[0]; 6 | var _current = _ref[1]; 7 | var _result = _ref[2]; 8 | 9 | for (var _i = 1; _i < num; _i++) { 10 | var _temp = _previous + _current; 11 | var _ref2 = [_current, _current = _result = _temp]; 12 | _previous = _ref2[0]; 13 | _current = _ref2[1]; 14 | } 15 | 16 | return _result; 17 | }; 18 | 19 | var _fib = function(n) { 20 | return n <= 1 ? n : (_fib(n - 1) + _fib(n - 2)); 21 | }; 22 | 23 | module.exports = { 24 | fibonacciRec: _fib, 25 | fibonacci: _fibonacci 26 | }; 27 | -------------------------------------------------------------------------------- /compiled/math/greatest-common-divisor.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var _gcd = function(a, b) { 3 | return !b ? a : _gcd(b, a % b); 4 | }; 5 | 6 | module.exports = _gcd; 7 | -------------------------------------------------------------------------------- /compiled/math/is-prime.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var _isPrime = function(num) { 3 | if (num % 2 === 0) return false; 4 | if (num === 2 || num === 3) return true; 5 | 6 | var _ref = [3, Math.sqrt(num)]; 7 | var _divisor = _ref[0]; 8 | var _limit = _ref[1]; 9 | 10 | while (_divisor <= _limit) { 11 | if (num % _divisor === 0) return false; 12 | _divisor += 2; 13 | } 14 | 15 | return true; 16 | }; 17 | 18 | module.exports = _isPrime; 19 | -------------------------------------------------------------------------------- /compiled/math/permutation.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var _fac = require('./factorial'); 3 | 4 | var _permutation = function(num, repetition) { 5 | return _fac(num) / _fac(num - repetition); 6 | }; 7 | 8 | module.exports = _permutation; 9 | -------------------------------------------------------------------------------- /compiled/math/simple-combination.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var _fac = require('./factorial'); 3 | 4 | var _simpleCombination = function(num, rep) { 5 | return _fac(num) / (_fac(rep) * _fac(num - rep)); 6 | }; 7 | 8 | module.exports = _simpleCombination; 9 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | var gulp = require('gulp'); 2 | var to5 = require('gulp-6to5'); 3 | 4 | gulp.task('build', function () { 5 | var paths = [ 6 | 'utils/*.js', 7 | 'src/**/*.js', 8 | '!**/__tests__/*.js', 9 | '!utils/es6-transformer.js' 10 | ]; 11 | 12 | gulp.src(paths) 13 | .pipe(to5()) 14 | .pipe(gulp.dest('compiled')); 15 | }); 16 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "algorithms-with-js", 3 | "version": "0.0.1", 4 | "description": "Just a bunch of algorithms using Javascript with ES6", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "jest" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/pedronauck/algorithms-with-js" 12 | }, 13 | "keywords": [ 14 | "algorithms", 15 | "javascript", 16 | "js" 17 | ], 18 | "author": "Pedro Nauck", 19 | "license": "MIT", 20 | "bugs": { 21 | "url": "https://github.com/pedronauck/algorithms-with-js/issues" 22 | }, 23 | "jest": { 24 | "scriptPreprocessor": "./utils/es6-transformer.js" 25 | }, 26 | "homepage": "https://github.com/pedronauck/algorithms-with-js", 27 | "devDependencies": { 28 | "6to5": "^1.10.8", 29 | "gulp": "^3.8.8", 30 | "gulp-6to5": "^1.0.1", 31 | "jest": "^0.1.37", 32 | "jest-cli": "^0.1.18" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/array/__tests__/concat-test.js: -------------------------------------------------------------------------------- 1 | jest.dontMock('../concat'); 2 | 3 | describe('concat()', () => { 4 | it('should concatenate arrays', () => { 5 | let concat = require('../concat'); 6 | 7 | expect(concat([1,2,3],[4,5,6])).toEqual([1,2,3,4,5,6]); 8 | }); 9 | }); 10 | -------------------------------------------------------------------------------- /src/array/__tests__/flatten-test.js: -------------------------------------------------------------------------------- 1 | jest.dontMock('../flatten'); 2 | 3 | describe('flatten()', () => { 4 | it('should flatten an array', () => { 5 | var flatten = require('../flatten'); 6 | var unflattenedArr = [1,[2,[[2, 3, 4],5],6]]; 7 | 8 | expect(flatten(unflattenedArr)).toEqual([1,2,2,3,4,5,6]); 9 | }); 10 | }); 11 | -------------------------------------------------------------------------------- /src/array/__tests__/intersection-test.js: -------------------------------------------------------------------------------- 1 | jest 2 | .dontMock('../intersection') 3 | .dontMock('../concat'); 4 | 5 | describe('intersection()', () => { 6 | it('should return the intersection between arrays', () => { 7 | let intersection = require('../intersection'); 8 | 9 | expect(intersection([1,2,3],[5,2,1,4],[2,1])).toEqual([1,2]); 10 | }); 11 | }); 12 | -------------------------------------------------------------------------------- /src/array/__tests__/remove-duplicates-test.js: -------------------------------------------------------------------------------- 1 | jest.dontMock('../remove-duplicates'); 2 | 3 | describe('removeDuplicates()', () => { 4 | it('should return an array without any duplicated value', () => { 5 | let { removeDuplicates, rRemoveDuplicates } = require('../remove-duplicates'); 6 | 7 | expect(removeDuplicates([1,2,3,1,4])).toEqual([1,2,3,4]); 8 | expect(rRemoveDuplicates([1,2,3,1,4])).toEqual([1,2,3,4]); 9 | }); 10 | }); 11 | -------------------------------------------------------------------------------- /src/array/__tests__/remove-from-array-test.js: -------------------------------------------------------------------------------- 1 | jest.dontMock('../remove-from-array'); 2 | 3 | describe('removeFromArr()', () => { 4 | it('should remove a value from an array', () => { 5 | var removeFromArr = require('../remove-from-array'); 6 | var arr = [1,2,3]; 7 | 8 | expect(arr).toEqual([1,2,3]); 9 | removeFromArr(arr, 2); 10 | expect(arr).toEqual([1,3]); 11 | }); 12 | }); 13 | -------------------------------------------------------------------------------- /src/array/__tests__/reverse-test.js: -------------------------------------------------------------------------------- 1 | jest.dontMock('../reverse'); 2 | 3 | describe('reverse()', () => { 4 | it('should reverse an array', () => { 5 | var reverse = require('../reverse'); 6 | var arr = [0,1,2,3,4]; 7 | var reversedArr = reverse(arr); 8 | 9 | expect(reversedArr).toEqual([4,3,2,1,0]); 10 | }); 11 | }); 12 | -------------------------------------------------------------------------------- /src/array/__tests__/union-test.js: -------------------------------------------------------------------------------- 1 | jest 2 | .dontMock('../union') 3 | .dontMock('../concat') 4 | .dontMock('../remove-duplicates'); 5 | 6 | describe('union()', () => { 7 | it('should return the union between arrays', () => { 8 | let union = require('../union'); 9 | 10 | expect(union([1,2,3],[5,2,1,4], [2,1])).toEqual([1,2,3,5,4]); 11 | }); 12 | }); 13 | -------------------------------------------------------------------------------- /src/array/concat.js: -------------------------------------------------------------------------------- 1 | let concat = (...arrs) => { 2 | let newArr = []; 3 | arrs.forEach(arr => arr.forEach(i => newArr.push(i))); 4 | return newArr; 5 | }; 6 | 7 | module.exports = concat; 8 | -------------------------------------------------------------------------------- /src/array/flatten.js: -------------------------------------------------------------------------------- 1 | let flatten = (arr, givenArr = []) => { 2 | arr.forEach((item) => { 3 | (Array.isArray(item)) && (flatten(item, givenArr)); 4 | (!Array.isArray(item)) && (givenArr.push(item)); 5 | }); 6 | 7 | return givenArr; 8 | }; 9 | 10 | module.exports = flatten; 11 | -------------------------------------------------------------------------------- /src/array/intersection.js: -------------------------------------------------------------------------------- 1 | let concat = require('./concat.js'); 2 | let intersection = (...arrs) => { 3 | let newArr = []; 4 | 5 | concat(...arrs).forEach((item, index) => { 6 | concat(...arrs).forEach((newItem, newIndex) => { 7 | let isOtherIndex = index !== newIndex; 8 | let isOtherItem = item === newItem; 9 | let notExist = newArr.indexOf(newItem) < 0; 10 | 11 | (isOtherIndex && isOtherItem && notExist) && (newArr.push(newItem)); 12 | }); 13 | }); 14 | 15 | return newArr; 16 | }; 17 | 18 | module.exports = intersection; 19 | -------------------------------------------------------------------------------- /src/array/remove-duplicates.js: -------------------------------------------------------------------------------- 1 | let removeDuplicates = (arr, newArr = []) => { 2 | arr.forEach(item => newArr.indexOf(item) < 0 && newArr.push(item)); 3 | return newArr; 4 | }; 5 | 6 | let rRemoveDuplicates = (arr, index = 0, newArr = []) => { 7 | newArr.indexOf(arr[index]) === -1 && newArr.push(arr[index]); 8 | return index < arr.length - 1 ? rRemoveDuplicates(arr, ++index, newArr) : newArr; 9 | }; 10 | 11 | module.exports = { removeDuplicates, rRemoveDuplicates }; 12 | -------------------------------------------------------------------------------- /src/array/remove-from-array.js: -------------------------------------------------------------------------------- 1 | let removeFromArr = (arr, val) => { 2 | arr.forEach((num, i) => { if (num === val) arr.splice(i, 1) }); 3 | }; 4 | 5 | module.exports = removeFromArr; 6 | -------------------------------------------------------------------------------- /src/array/reverse.js: -------------------------------------------------------------------------------- 1 | let reverse = (arr) => { 2 | let index = arr.length; 3 | return arr.map(() => arr[--index]); 4 | }; 5 | 6 | module.exports = reverse; 7 | -------------------------------------------------------------------------------- /src/array/union.js: -------------------------------------------------------------------------------- 1 | let concat = require('./concat'); 2 | let { removeDuplicates } = require('./remove-duplicates'); 3 | 4 | let union = (...arrs) => removeDuplicates(concat(...arrs)); 5 | 6 | module.exports = union; 7 | -------------------------------------------------------------------------------- /src/interview-questions/__tests__/given-sum-test.js: -------------------------------------------------------------------------------- 1 | jest 2 | .dontMock('../given-sum'); 3 | 4 | describe('givenSum()', () => { 5 | it('should return two numbers that the sum is equal other number as second argument', () => { 6 | var givenSum = require('../given-sum'); 7 | 8 | expect(givenSum([1, 2, 4, 7, 11, 15], 15)).toEqual([[4, 11]]); 9 | expect(givenSum([2, 3, 4, 5, 6], 7)).toEqual([[2, 5], [3, 4]]); 10 | }); 11 | 12 | describe('for very large array', () => { 13 | var givenSum = require('../given-sum'); 14 | 15 | var largeArray = []; 16 | for (var i = 0; i < 10000; i++) { 17 | largeArray.push(i); 18 | } 19 | 20 | it('returns correct result', () => { 21 | expect(givenSum(largeArray, 3)).toEqual([[0, 3], [1, 2]]); 22 | }); 23 | 24 | it('runs in O(N) time', () => { 25 | var startTime = Date.now(); 26 | givenSum(largeArray, 3); 27 | var endTime = Date.now(); 28 | 29 | var elapsedMilliseconds = endTime - startTime; 30 | 31 | expect(elapsedMilliseconds).toBeLessThan(5); 32 | }); 33 | }); 34 | }); 35 | -------------------------------------------------------------------------------- /src/interview-questions/__tests__/is-palindrome-test.js: -------------------------------------------------------------------------------- 1 | jest.dontMock('../is-palindrome'); 2 | 3 | describe('isPalindrome', () => { 4 | it('should return if word is a palindrome or not', () => { 5 | var isPalindrome = require('../is-palindrome'); 6 | 7 | expect(isPalindrome('madam')).toBe(true); 8 | expect(isPalindrome('anna')).toBe(true); 9 | expect(isPalindrome('hello')).toBe(false); 10 | }); 11 | }); 12 | -------------------------------------------------------------------------------- /src/interview-questions/__tests__/reorder-by-indexes.js: -------------------------------------------------------------------------------- 1 | jest.dontMock('../reorder-by-indexes'); 2 | 3 | describe('reorderByIndexes()', () => { 4 | it('should reorder an array by other array with indexes', () => { 5 | var reorderByIndexes = require('../reorder-by-indexes'); 6 | var arr = ['a','b','c','d','e','f']; 7 | var indexes = [2, 3, 4, 0, 5, 1]; 8 | var orderedArr = reorderByIndexes(arr, indexes); 9 | 10 | expect(orderedArr).toEqual(['c','d','e','a','f','b']); 11 | }); 12 | }); 13 | -------------------------------------------------------------------------------- /src/interview-questions/__tests__/reverse-string-test.js: -------------------------------------------------------------------------------- 1 | jest 2 | .dontMock('../../array/reverse') 3 | .dontMock('../reverse-string'); 4 | 5 | describe('reverseStr()', () => { 6 | it('should return a reverse string', () => { 7 | let { rReverseStr, reverseStr } = require('../reverse-string'); 8 | let str = 'abcde'; 9 | 10 | expect(rReverseStr(str)).toEqual('edcba'); 11 | expect(reverseStr(str)).toEqual('edcba'); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /src/interview-questions/__tests__/string-contains-test.js: -------------------------------------------------------------------------------- 1 | jest.dontMock('../string-contains'); 2 | 3 | describe('stringContains()', () => { 4 | it('should return if a string contains in other', () => { 5 | var { stringContains, rStrContains } = require('../string-contains'); 6 | 7 | expect(stringContains('John', 'hn')).toBe(true); 8 | expect(stringContains('Ana', 'na')).toBe(true); 9 | expect(stringContains('Joanana', 'ana')).toBe(true); 10 | expect(stringContains('Joe', 'op')).toBe(false); 11 | 12 | expect(rStrContains('John', 'hn')).toBe(true); 13 | expect(rStrContains('Ana', 'na')).toBe(true); 14 | expect(rStrContains('Joanana', 'ana')).toBe(true); 15 | expect(rStrContains('Joe', 'op')).toBe(false); 16 | }); 17 | }); 18 | -------------------------------------------------------------------------------- /src/interview-questions/given-sum.js: -------------------------------------------------------------------------------- 1 | let givenSum = (arr, sum) => { 2 | // For each pair of results we need to return, 3 | // keep the smaller value as a key on the object `results` 4 | // to avoid returning duplicates. 5 | let results = {}; 6 | 7 | // `allElements` allows to quickly check if a number is in the original array 8 | let allElements = {}; 9 | arr.forEach((x) => { allElements[x] = true; }); 10 | 11 | arr.forEach((x) => { 12 | let complement = sum - x; 13 | 14 | // avoid mirror duplicates 15 | if (x > complement) { 16 | return; 17 | } 18 | 19 | if (allElements[complement]) { 20 | results[x] = [x, complement]; 21 | } 22 | }); 23 | 24 | return Object.keys(results).map(k => results[k]); 25 | }; 26 | 27 | module.exports = givenSum; 28 | -------------------------------------------------------------------------------- /src/interview-questions/is-palindrome.js: -------------------------------------------------------------------------------- 1 | let isPalindrome = (word) => { 2 | let [len, result] = [word.length, true]; 3 | 4 | for (let i = 0; i < len / 2; i++) { 5 | if (word[i] !== word[len - 1 - i]) result = false; 6 | } 7 | 8 | return result; 9 | } 10 | 11 | module.exports = isPalindrome; 12 | -------------------------------------------------------------------------------- /src/interview-questions/reorder-by-indexes.js: -------------------------------------------------------------------------------- 1 | let reorderByIndexes = (arr, indexes) => indexes.map((i) => arr[i]); 2 | 3 | module.exports = reorderByIndexes; 4 | -------------------------------------------------------------------------------- /src/interview-questions/reverse-string.js: -------------------------------------------------------------------------------- 1 | let arrayReverse = require('../array/reverse'); 2 | 3 | let reverseStr = (str) => arrayReverse(str.split('')).join(''); 4 | 5 | let rReverseStr = (str) => { 6 | let lastChar = str.length - 1; 7 | return !str ? '' : str[lastChar] + rReverseStr(str.substr(0, lastChar)); 8 | }; 9 | 10 | module.exports = { 11 | rReverseStr, 12 | reverseStr 13 | }; 14 | -------------------------------------------------------------------------------- /src/interview-questions/string-contains.js: -------------------------------------------------------------------------------- 1 | let stringContains = (str, substr) => { 2 | var contains = false; 3 | let [splitSubstr, splitStr] = [substr.split(''), str.split('')]; 4 | 5 | splitStr.forEach((letter, i) => { 6 | (splitSubstr.map((foundLetter, j) => { 7 | if (splitStr[i + (j - 1)] === foundLetter) return splitStr[i + (j - 1)]; 8 | }).join('') === substr) && (contains = true); 9 | }); 10 | 11 | return contains; 12 | }; 13 | 14 | let rStrContains = (a, b) => { 15 | return !a ? false : (a === b ? true : rStrContains(a.substr(1), b)); 16 | }; 17 | 18 | module.exports = { 19 | stringContains, 20 | rStrContains 21 | }; 22 | -------------------------------------------------------------------------------- /src/math/__tests__/factorial-test.js: -------------------------------------------------------------------------------- 1 | jest.dontMock('../factorial'); 2 | 3 | describe('factorial()', () => { 4 | it('should return a factorial of a number', () => { 5 | let factorial = require('../factorial'); 6 | 7 | expect(factorial(5)).toBe(120); 8 | expect(factorial(4)).toBe(24); 9 | }); 10 | }); 11 | -------------------------------------------------------------------------------- /src/math/__tests__/fibonacci-test.js: -------------------------------------------------------------------------------- 1 | jest.dontMock('../fibonacci'); 2 | 3 | describe('fibonacci()', () => { 4 | it('should return the right value for fibonacci sequence', () => { 5 | let { fibonacci, fibonacciRec } = require('../fibonacci'); 6 | 7 | expect(fibonacci(10) === fibonacciRec(10)); 8 | expect(fibonacci(9) === fibonacciRec(9)); 9 | 10 | expect(fibonacci(10)).toBe(55); 11 | expect(fibonacci(9)).toBe(34); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /src/math/__tests__/greatest-common-divisor.js: -------------------------------------------------------------------------------- 1 | jest.dontMock('../greatest-common-divisor'); 2 | 3 | describe('greatestCommonDivisor()', () => { 4 | it('should return a greatest common divisor of two numbers', () => { 5 | var greatestCommonDivisor = require('../greatest-common-divisor'); 6 | 7 | expect(greatestCommonDivisor(14, 21)).toBe(7); 8 | expect(greatestCommonDivisor(20, 10)).toBe(10); 9 | }); 10 | }); 11 | -------------------------------------------------------------------------------- /src/math/__tests__/is-prime-test.js: -------------------------------------------------------------------------------- 1 | jest.dontMock('../is-prime'); 2 | 3 | describe('isPrime()', () => { 4 | it('should return if an number is a prime number or not', () => { 5 | var isPrime = require('../is-prime'); 6 | 7 | expect(isPrime(137)).toBe(true); 8 | expect(isPrime(237)).toBe(false); 9 | }); 10 | }); 11 | -------------------------------------------------------------------------------- /src/math/__tests__/permutation-test.js: -------------------------------------------------------------------------------- 1 | jest 2 | .dontMock('../permutation') 3 | .dontMock('../factorial'); 4 | 5 | describe('permutation()', () => { 6 | it('should calculate a permutation math expression', () => { 7 | var permutation = require('../permutation'); 8 | 9 | expect(permutation(6,2)).toBe(30); 10 | expect(permutation(3,2)).toBe(6); 11 | expect(permutation(7,3)).toBe(210); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /src/math/__tests__/simple-combination-test.js: -------------------------------------------------------------------------------- 1 | jest 2 | .dontMock('../simple-combination') 3 | .dontMock('../factorial'); 4 | 5 | describe('simpleCombination()', () => { 6 | it('should calculate a simple math combination expression', () => { 7 | var simpleCombination = require('../simple-combination'); 8 | 9 | expect(simpleCombination(6,2)).toBe(15); 10 | }); 11 | }); 12 | -------------------------------------------------------------------------------- /src/math/factorial.js: -------------------------------------------------------------------------------- 1 | let factorial = (num) => { 2 | let result = 1; 3 | while (--num) result *= num + 1; 4 | return result; 5 | }; 6 | 7 | module.exports = factorial; 8 | -------------------------------------------------------------------------------- /src/math/fibonacci.js: -------------------------------------------------------------------------------- 1 | let fibonacci = (num) => { 2 | if (num <= 1) return num; 3 | let [previous, current, result] = [0, 1]; 4 | 5 | for (let i = 1; i < num; i++) { 6 | let temp = previous + current; 7 | [previous, current] = [current, current = result = temp]; 8 | } 9 | 10 | return result; 11 | }; 12 | 13 | let fib = (n) => n <= 1 ? n : (fib(n - 1) + fib(n - 2)); 14 | 15 | module.exports = { 16 | fibonacciRec: fib, 17 | fibonacci: fibonacci 18 | }; 19 | -------------------------------------------------------------------------------- /src/math/greatest-common-divisor.js: -------------------------------------------------------------------------------- 1 | let gcd = (a, b) => !b ? a : gcd(b, a % b); 2 | 3 | module.exports = gcd; 4 | -------------------------------------------------------------------------------- /src/math/is-prime.js: -------------------------------------------------------------------------------- 1 | let isPrime = function(num) { 2 | if (num % 2 === 0) return false; 3 | if (num === 2 || num === 3) return true; 4 | 5 | let [divisor, limit] = [3, Math.sqrt(num)]; 6 | 7 | while (divisor <= limit) { 8 | if (num % divisor === 0) return false; 9 | divisor += 2; 10 | } 11 | 12 | return true; 13 | }; 14 | 15 | module.exports = isPrime; 16 | -------------------------------------------------------------------------------- /src/math/permutation.js: -------------------------------------------------------------------------------- 1 | let fac = require('./factorial'); 2 | 3 | let permutation = (num, repetition) => fac(num) / fac(num - repetition); 4 | 5 | module.exports = permutation; 6 | -------------------------------------------------------------------------------- /src/math/simple-combination.js: -------------------------------------------------------------------------------- 1 | let fac = require('./factorial'); 2 | 3 | let simpleCombination = (num, rep) => fac(num) / (fac(rep) * fac(num - rep)); 4 | 5 | module.exports = simpleCombination; 6 | -------------------------------------------------------------------------------- /utils/__tests__/array-is-equal-test.js: -------------------------------------------------------------------------------- 1 | jest.dontMock('../array-is-equal'); 2 | 3 | describe('arrIsEqual', () => { 4 | it('should return if two arrays is equal', () => { 5 | var arrIsEqual = require('../array-is-equal'); 6 | 7 | expect(arrIsEqual([1,2], [1,2])).toBe(true); 8 | expect(arrIsEqual([1,2], [2,1])).toBe(false); 9 | expect(arrIsEqual([1,2], ['1', 2])).toBe(false); 10 | }); 11 | }); 12 | -------------------------------------------------------------------------------- /utils/array-is-equal.js: -------------------------------------------------------------------------------- 1 | let arrIsEqual = (arrX, arrY) => { 2 | if (arrX.length !== arrY.length) return false; 3 | if (!Array.isArray(arrX) || !Array.isArray(arrY)) return false; 4 | 5 | let [index, isEqual] = [-1, true]; 6 | 7 | while (++index <= arrX.length) { 8 | if (arrX[index] !== arrY[index]) { 9 | isEqual = false; 10 | } 11 | } 12 | 13 | return isEqual; 14 | }; 15 | 16 | module.exports = arrIsEqual; 17 | -------------------------------------------------------------------------------- /utils/es6-transformer.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | process: function (src, path) { 5 | return require('6to5').transform(src).code; 6 | } 7 | }; 8 | --------------------------------------------------------------------------------