Fixed formatting problems, example and JSDoc
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
/**
|
||||
* Sums values in array by loop "for"
|
||||
* @param {Array} arr Array of numbers
|
||||
* @return {number} Sum of the numbers
|
||||
* Sums values in the array by loop "for"
|
||||
* @param {Array} array Array of numbers
|
||||
* @returns {number} Sum of the numbers
|
||||
*/
|
||||
const sumLoop = arr => {
|
||||
const sumLoop = array => {
|
||||
let result = 0;
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
result += arr[i];
|
||||
for (let i = 0; i < array.length; i++) {
|
||||
result += array[i];
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
/**
|
||||
* Sums values in array by function "reduce"
|
||||
* @param {Array} arr Array of numbers
|
||||
* @return {number} Sum of the numbers
|
||||
* Sums values in the array by function "reduce"
|
||||
* @param {Array} array Array of 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
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
const sum = (list) => {
|
||||
if (list.length === 0) {
|
||||
return 0;
|
||||
}
|
||||
return list[0] + sum(list.slice(1));
|
||||
};
|
||||
/**
|
||||
* Sums values in the array by recursive
|
||||
* @param {Array} array Array of numbers
|
||||
* @returns {number} Sum of the numbers
|
||||
*/
|
||||
const sum = array => (array.length === 0 ? 0 : array[0] + sum(array.slice(1)));
|
||||
|
||||
console.log(sum([1, 2, 3, 4])); // 10
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
const count = (list) => {
|
||||
if (list.length === 0) {
|
||||
return 0;
|
||||
}
|
||||
return 1 + count(list.slice(1));
|
||||
};
|
||||
/**
|
||||
* Count the number of elements in the array
|
||||
* @param {Array} array Array of numbers
|
||||
* @returns {number} The number of elements in the array
|
||||
*/
|
||||
const count = array => (array.length === 0 ? 0 : 1 + count(array.slice(1)));
|
||||
|
||||
console.log(count([0, 1, 2, 3, 4, 5])); // 6
|
||||
|
||||
@@ -1,9 +1,26 @@
|
||||
const max = (list) => {
|
||||
if (list.length === 2) {
|
||||
return list[0] > list[1] ? list[0] : list[1];
|
||||
}
|
||||
const subMax = max(list.slice(1));
|
||||
return list[0] > subMax ? list[0] : subMax;
|
||||
/**
|
||||
* Calculate the largest number
|
||||
* This solution only works for arrays longer than one
|
||||
* @param {Array} array Array of numbers
|
||||
* @returns {number} The argest number
|
||||
*/
|
||||
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(alternativeSolutionMax([1, 5, 10, 25, 16, 1])); // 25
|
||||
|
||||
@@ -1,11 +1,18 @@
|
||||
const quickSort = (array) => {
|
||||
if (array.length < 2) {
|
||||
return array;
|
||||
}
|
||||
/**
|
||||
* Quick array sorting
|
||||
* @param {Array} array Source array
|
||||
* @returns {Array} Sorted array
|
||||
*/
|
||||
const quickSort = array => {
|
||||
if (array.length < 2) return array;
|
||||
const pivot = array[0];
|
||||
const keysAreLessPivot = 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]
|
||||
|
||||
@@ -1,36 +1,27 @@
|
||||
/**
|
||||
* 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)
|
||||
* @returns {number} GCD (greatest common divisor)
|
||||
*/
|
||||
let gcdOfTwo = ( a, b ) => {
|
||||
if ( !b ) {
|
||||
return a;
|
||||
}
|
||||
return gcdOfTwo( b, a % b );
|
||||
};
|
||||
const gcdOfTwo = (a, b) => (!b ? a : 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)
|
||||
* @returns {number} GCD (greatest common divisor)
|
||||
*/
|
||||
let gcdOfSet = ( set ) => {
|
||||
let result = set[0];
|
||||
let newArr = Array.prototype.slice.call( set, 1 );
|
||||
const gcdOfSet = set => {
|
||||
let result = set[0];
|
||||
let newArr = set.slice(1);
|
||||
|
||||
newArr.map( ( el ) => {
|
||||
result = gcdOfTwo( result, el );
|
||||
} );
|
||||
newArr.map(el => {
|
||||
result = gcdOfTwo(result, el);
|
||||
});
|
||||
|
||||
return result;
|
||||
return result;
|
||||
};
|
||||
|
||||
const set = [1680, 640, 3360, 160, 240, 168000];
|
||||
|
||||
console.log( gcdOfSet( set ) ); // 80
|
||||
console.log(gcdOfSet(set)); // 80
|
||||
|
||||
@@ -1,19 +1,9 @@
|
||||
/**
|
||||
* Recursive function of Euclidean algorithm
|
||||
*
|
||||
* @param {number} a first number
|
||||
* @param {number} b second number (base case)
|
||||
*
|
||||
* @return {number} GCD (greatest common divisor)
|
||||
* @returns {number} GCD (greatest common divisor)
|
||||
*/
|
||||
let getGCD = ( a, b ) => {
|
||||
if ( !b ) {
|
||||
return a;
|
||||
}
|
||||
return getGCD( b, a % b );
|
||||
};
|
||||
const getGCD = (a, b) => (!b ? a : getGCD(b, a % b));
|
||||
|
||||
const a = 1680;
|
||||
const b = 640;
|
||||
|
||||
console.log( getGCD( a, b ) ); // 80
|
||||
console.log(getGCD(1680, 640)); // 80
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
/**
|
||||
* Sums values in array by loop "for"
|
||||
* @param {Array} arr Array of numbers
|
||||
* @return {total} Sum of the numbers
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
function sum(arr) {
|
||||
let total = 0;
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
total += arr[i];
|
||||
}
|
||||
return total;
|
||||
/**
|
||||
* Sums values in the array by loop "for"
|
||||
* @param {Array} array Array of numbers
|
||||
* @returns {total} Sum of the numbers
|
||||
*/
|
||||
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
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Sums values in array by function "reduce"
|
||||
* @param {Array} arr Array of numbers
|
||||
* @return {number} Sum of the numbers
|
||||
* Sums values in the array by function "reduce"
|
||||
* @param {Array} array Array of numbers
|
||||
* @returns {number} Sum of the numbers
|
||||
*/
|
||||
function sumReduce(arr) {
|
||||
return arr.reduce(function(curr, prev) {
|
||||
function sumReduce(array) {
|
||||
return array.reduce(function(curr, prev) {
|
||||
return curr + prev;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Sums values in array recursively
|
||||
* @param {Array} arr Array of numbers
|
||||
* @return {number} Sum of the numbers
|
||||
* Sums values in the array by recursive
|
||||
* @param {Array} array Array of numbers
|
||||
* @returns {number} Sum of the numbers
|
||||
*/
|
||||
function sumRecursive(arr) {
|
||||
if (arr.length == 1) {
|
||||
return arr[0];
|
||||
}
|
||||
return arr[0] + sumRecursive(arr.slice(1));
|
||||
function sumRecursive(array) {
|
||||
if (array.length == 1) return array[0];
|
||||
return array[0] + sumRecursive(array.slice(1));
|
||||
}
|
||||
|
||||
console.log(sumRecursive([1, 2, 3, 4])); // 10
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
'use strict';
|
||||
"use strict";
|
||||
|
||||
function count(list) {
|
||||
if (list.length === 0) {
|
||||
return 0;
|
||||
}
|
||||
return 1 + count(list.slice(1));
|
||||
/**
|
||||
* Count the number of elements in the array
|
||||
* @param {Array} array Array of numbers
|
||||
* @returns {number} The number of elements in the array
|
||||
*/
|
||||
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
|
||||
|
||||
@@ -1,11 +1,29 @@
|
||||
'use strict';
|
||||
"use strict";
|
||||
|
||||
function max(list) {
|
||||
if (list.length === 2) {
|
||||
return list[0] > list[1] ? list[0] : list[1];
|
||||
}
|
||||
let sub_max = max(list.slice(1));
|
||||
return list[0] > sub_max ? list[0] : sub_max;
|
||||
/**
|
||||
* Calculate the largest number
|
||||
* This solution only works for arrays longer than one
|
||||
* @param {Array} array Array of numbers
|
||||
* @returns {number} The argest number
|
||||
*/
|
||||
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(alternativeSolutionMax([1, 5, 10, 25, 16, 1])); // 25
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Quick array sorting
|
||||
* @param {Array} array Source array
|
||||
* @returns {Array} Sorted array
|
||||
*/
|
||||
function quicksort(array) {
|
||||
if (array.length < 2) {
|
||||
// base case, arrays with 0 or 1 element are already "sorted"
|
||||
return array;
|
||||
}
|
||||
// base case, arrays with 0 or 1 element are already "sorted"
|
||||
if (array.length < 2) return array;
|
||||
// recursive case
|
||||
let pivot = array[0];
|
||||
// sub-array of all the elements less than the pivot
|
||||
|
||||
Reference in New Issue
Block a user