Fixed formatting problems, example and JSDoc (#133)

This commit is contained in:
Aditya Bhargava
2020-09-14 10:46:57 -05:00
committed by GitHub
14 changed files with 141 additions and 107 deletions

View File

@@ -1,12 +1,12 @@
/** /**
* Sums values in array by loop "for" * Sums values in the array by loop "for"
* @param {Array} arr Array of numbers * @param {Array} array Array of numbers
* @return {number} Sum of the numbers * @returns {number} Sum of the numbers
*/ */
const sumLoop = arr => { const sumLoop = array => {
let result = 0; let result = 0;
for (let i = 0; i < arr.length; i++) { for (let i = 0; i < array.length; i++) {
result += arr[i]; result += array[i];
} }
return result; return result;
}; };

View File

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

View File

@@ -1,8 +1,8 @@
const sum = (list) => { /**
if (list.length === 0) { * Sums values in the array by recursive
return 0; * @param {Array} array Array of numbers
} * @returns {number} Sum of the numbers
return list[0] + sum(list.slice(1)); */
}; const sum = array => (array.length === 0 ? 0 : array[0] + sum(array.slice(1)));
console.log(sum([1, 2, 3, 4])); // 10 console.log(sum([1, 2, 3, 4])); // 10

View File

@@ -1,8 +1,8 @@
const count = (list) => { /**
if (list.length === 0) { * Count the number of elements in the array
return 0; * @param {Array} array Array of numbers
} * @returns {number} The number of elements in the array
return 1 + count(list.slice(1)); */
}; const count = array => (array.length === 0 ? 0 : 1 + count(array.slice(1)));
console.log(count([0, 1, 2, 3, 4, 5])); // 6 console.log(count([0, 1, 2, 3, 4, 5])); // 6

View File

@@ -1,9 +1,26 @@
const max = (list) => { /**
if (list.length === 2) { * Calculate the largest number
return list[0] > list[1] ? list[0] : list[1]; * This solution only works for arrays longer than one
} * @param {Array} array Array of numbers
const subMax = max(list.slice(1)); * @returns {number} The argest number
return list[0] > subMax ? list[0] : subMax; */
const max = array => {
if (array.length === 2) return array[0] > array[1] ? array[0] : array[1];
const subMax = max(array.slice(1));
return array[0] > subMax ? array[0] : subMax;
}; };
/**
* Calculate the largest number
* This solution works for arrays of any length
* @param {Array} array Array of numbers
* @param {number} max Maximum value
* @returns {number} The argest number
*/
const alternativeSolutionMax = (array, max = 0) =>
array.length === 0
? max
: alternativeSolutionMax(array.slice(1), array[0] > max ? array[0] : max);
console.log(max([1, 5, 10, 25, 16, 1])); // 25 console.log(max([1, 5, 10, 25, 16, 1])); // 25
console.log(alternativeSolutionMax([1, 5, 10, 25, 16, 1])); // 25

View File

@@ -1,11 +1,18 @@
const quickSort = (array) => { /**
if (array.length < 2) { * Quick array sorting
return array; * @param {Array} array Source array
} * @returns {Array} Sorted array
*/
const quickSort = array => {
if (array.length < 2) return array;
const pivot = array[0]; const pivot = array[0];
const keysAreLessPivot = array.slice(1).filter(key => key <= pivot); const keysAreLessPivot = array.slice(1).filter(key => key <= pivot);
const keysAreMorePivot = array.slice(1).filter(key => key > pivot); const keysAreMorePivot = array.slice(1).filter(key => key > pivot);
return [...quickSort(keysAreLessPivot), pivot, ...quickSort(keysAreMorePivot)]; return [
...quickSort(keysAreLessPivot),
pivot,
...quickSort(keysAreMorePivot)
];
}; };
console.log(quickSort([10, 5, 2, 3])); // [2, 3, 5, 10] console.log(quickSort([10, 5, 2, 3])); // [2, 3, 5, 10]

View File

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

View File

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

View File

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

View File

@@ -1,10 +1,12 @@
"use strict";
/** /**
* Sums values in array by function "reduce" * Sums values in the array by function "reduce"
* @param {Array} arr Array of numbers * @param {Array} array Array of numbers
* @return {number} Sum of the numbers * @returns {number} Sum of the numbers
*/ */
function sumReduce(arr) { function sumReduce(array) {
return arr.reduce(function(curr, prev) { return array.reduce(function(curr, prev) {
return curr + prev; return curr + prev;
}); });
} }

View File

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

View File

@@ -1,10 +1,13 @@
'use strict'; "use strict";
function count(list) { /**
if (list.length === 0) { * Count the number of elements in the array
return 0; * @param {Array} array Array of numbers
} * @returns {number} The number of elements in the array
return 1 + count(list.slice(1)); */
function count(array) {
if (array.length === 0) return 0;
return 1 + count(array.slice(1));
} }
console.log(count([0, 1, 2, 3, 4, 5])); // 6 console.log(count([0, 1, 2, 3, 4, 5])); // 6

View File

@@ -1,11 +1,29 @@
'use strict'; "use strict";
function max(list) { /**
if (list.length === 2) { * Calculate the largest number
return list[0] > list[1] ? list[0] : list[1]; * This solution only works for arrays longer than one
} * @param {Array} array Array of numbers
let sub_max = max(list.slice(1)); * @returns {number} The argest number
return list[0] > sub_max ? list[0] : sub_max; */
function max(array) {
if (array.length === 2) return array[0] > array[1] ? array[0] : array[1];
let sub_max = max(array.slice(1));
return array[0] > sub_max ? array[0] : sub_max;
}
/**
* Calculate the largest number
* This solution works for arrays of any length
* @param {Array} array Array of numbers
* @param {number} max Maximum value
* @returns {number} The argest number
*/
function alternativeSolutionMax(array, max = 0) {
return array.length === 0
? max
: alternativeSolutionMax(array.slice(1), array[0] > max ? array[0] : max);
} }
console.log(max([1, 5, 10, 25, 16, 1])); // 25 console.log(max([1, 5, 10, 25, 16, 1])); // 25
console.log(alternativeSolutionMax([1, 5, 10, 25, 16, 1])); // 25

View File

@@ -1,8 +1,13 @@
"use strict";
/**
* Quick array sorting
* @param {Array} array Source array
* @returns {Array} Sorted array
*/
function quicksort(array) { function quicksort(array) {
if (array.length < 2) { // base case, arrays with 0 or 1 element are already "sorted"
// base case, arrays with 0 or 1 element are already "sorted" if (array.length < 2) return array;
return array;
}
// recursive case // recursive case
let pivot = array[0]; let pivot = array[0];
// sub-array of all the elements less than the pivot // sub-array of all the elements less than the pivot