Added more implementations (JS) (#67)

* Added recursive binary search (JS)

* Added recursive selection sorting

* Added another loop implementation sum func by reduce (JS)

* Recursion reduced by one iteration

* Recursive binary search in ES6 added to appropriate folder

* JS files ordered by standards (ES4/ES6)

* Added hashtable implementation in JS

* Fixed typo with LENGTH prop

* Added Euclidian algorithm for two numbers and set of them

* Added universal selection sort

* Commented output

* Added ES6 version of Euclidean algorithm

* Converted from ES6 to ES5

* #69 Added search for LCS

* #69 added levenstein algorithm

* Removed excessive property

* Removed excessive file

* Removed excessive function calls

* Removed excessive file

* Removed excessive file

* Renamed

* Fixed indentation
This commit is contained in:
Artem Solovev
2018-08-24 21:21:54 +03:00
committed by Aditya Bhargava
parent a5fe9178dd
commit 0d5d0164ce
12 changed files with 502 additions and 29 deletions

View File

@@ -1,9 +1,18 @@
const sum = (arr) => {
let total = 0;
for (let x = 0; x < arr.length; x += 1) {
total += arr[x];
}
return total;
/**
* Sums values in array by loop "for"
* @param {Array} arr Array of numbers
* @return {number} Sum of the numbers
*/
const sumLoop = ( arr ) => {
let result = 0;
for ( let i = 0; i < newArr.length; i++ ) {
result += newArr[i];
}
return result;
};
console.log(sum([1, 2, 3, 4])); // 10
let arr = [1, 2, 3, 4];
console.log( sumLoop( arr ) ); // 10

View File

@@ -0,0 +1,16 @@
/**
* Sums values in array by function "reduce"
* @param {Array} arr Array of numbers
* @return {number} Sum of the numbers
*/
const sumReduce = ( arr ) => {
let result = newArr.reduce( ( curr, prev ) => {
return curr + prev;
} );
return result;
};
let arr = [1, 2, 3, 4];
console.log( sumReduce( arr ) ); // 10

View File

@@ -0,0 +1,36 @@
/**
* Recursive function of Euclidean algorithm for two numbers
*
* @param {number} a first number
* @param {number} b second number (base case)
*
* @return {number} GCD (greatest common divisor)
*/
let gcdOfTwo = ( a, b ) => {
if ( !b ) {
return a;
}
return gcdOfTwo( b, a % b );
};
/**
* Recursive function of Euclidean algorithm for set of the numbers
*
* @param {Array} set Set of the numbers
*
* @return {number} GCD (greatest common divisor)
*/
let gcdOfSet = ( set ) => {
let result = set[0];
let newArr = Array.prototype.slice.call( set, 1 );
newArr.map( ( el ) => {
result = gcdOfTwo( result, el );
} );
return result;
};
const set = [1680, 640, 3360, 160, 240, 168000];
console.log( gcdOfSet( set ) ); // 80

View File

@@ -0,0 +1,19 @@
/**
* Recursive function of Euclidean algorithm
*
* @param {number} a first number
* @param {number} b second number (base case)
*
* @return {number} GCD (greatest common divisor)
*/
let getGCD = ( a, b ) => {
if ( !b ) {
return a;
}
return getGCD( b, a % b );
};
const a = 1680;
const b = 640;
console.log( getGCD( a, b ) ); // 80

View File

@@ -1,11 +1,18 @@
'use strict';
/**
* Sums values in array by loop "for"
* @param {Array} arr Array of numbers
* @return {number} Sum of the numbers
*/
function sumLoop( arr ) {
var result = 0;
function sum(arr) {
let total = 0;
for (let x = 0; x < arr.length; x++) {
total += arr[x];
}
return total;
for ( var i = 0; i < newArr.length; i++ ) {
result += newArr[i];
}
return result;
}
console.log(sum([1, 2, 3, 4])) // 10
var arr = [1, 2, 3, 4];
console.log( sumLoop( arr ) ); // 10

View File

@@ -0,0 +1,16 @@
/**
* Sums values in array by function "reduce"
* @param {Array} arr Array of numbers
* @return {number} Sum of the numbers
*/
function sumReduce( arr ) {
var result = newArr.reduce( ( curr, prev ) => {
return curr + prev;
} );
return result;
}
var arr = [1, 2, 3, 4];
console.log( sumReduce( arr ) ); // 10

View File

@@ -1,10 +1,13 @@
'use strict';
const arr = [1, 2, 3, 4];
function sum(list) {
if (list.length === 0) {
return 0;
}
return list[0] + sum(list.slice(1));
}
/**
* Sums values in array recursively
* @param {Array} arr Array of numbers
* @return {number} Sum of the numbers
*/
const sumRecursive = ( arr ) => {
if ( arr.length == 1 ) return arr[0];
return arr[0] + sumRecursive( arr.slice( 1 ) );
};
console.log(sum([1, 2, 3, 4])) // 10
console.log( sumRecursive( arr ) ); // 10