Skip to content

Commit e8a0253

Browse files
authored
Fixed formatting problems, example and JSDoc (egonSchiele#133)
2 parents 34df0a6 + 61b2925 commit e8a0253

14 files changed

+140
-106
lines changed

04_quicksort/ES6/01_loop_sum.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
2-
* Sums values in array by loop "for"
3-
* @param {Array} arr Array of numbers
4-
* @return {number} Sum of the numbers
2+
* Sums values in the array by loop "for"
3+
* @param {Array} array Array of numbers
4+
* @returns {number} Sum of the numbers
55
*/
6-
const sumLoop = arr => {
6+
const sumLoop = array => {
77
let result = 0;
8-
for (let i = 0; i < arr.length; i++) {
9-
result += arr[i];
8+
for (let i = 0; i < array.length; i++) {
9+
result += array[i];
1010
}
1111
return result;
1212
};
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
2-
* Sums values in array by function "reduce"
3-
* @param {Array} arr Array of numbers
4-
* @return {number} Sum of the numbers
2+
* Sums values in the array by function "reduce"
3+
* @param {Array} array Array of numbers
4+
* @returns {number} Sum of the numbers
55
*/
6-
const sumReduce = arr => arr.reduce((curr, prev) => curr + prev);
6+
const sumReduce = array => array.reduce((curr, prev) => curr + prev);
77

88
console.log(sumReduce([1, 2, 3, 4])); // 10

04_quicksort/ES6/02_recursive_sum.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const sum = (list) => {
2-
if (list.length === 0) {
3-
return 0;
4-
}
5-
return list[0] + sum(list.slice(1));
6-
};
1+
/**
2+
* Sums values in the array by recursive
3+
* @param {Array} array Array of numbers
4+
* @returns {number} Sum of the numbers
5+
*/
6+
const sum = array => (array.length === 0 ? 0 : array[0] + sum(array.slice(1)));
77

88
console.log(sum([1, 2, 3, 4])); // 10
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const count = (list) => {
2-
if (list.length === 0) {
3-
return 0;
4-
}
5-
return 1 + count(list.slice(1));
6-
};
1+
/**
2+
* Count the number of elements in the array
3+
* @param {Array} array Array of numbers
4+
* @returns {number} The number of elements in the array
5+
*/
6+
const count = array => (array.length === 0 ? 0 : 1 + count(array.slice(1)));
77

88
console.log(count([0, 1, 2, 3, 4, 5])); // 6

04_quicksort/ES6/04_recursive-max.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1-
const max = (list) => {
2-
if (list.length === 2) {
3-
return list[0] > list[1] ? list[0] : list[1];
4-
}
5-
const subMax = max(list.slice(1));
6-
return list[0] > subMax ? list[0] : subMax;
1+
/**
2+
* Calculate the largest number
3+
* This solution only works for arrays longer than one
4+
* @param {Array} array Array of numbers
5+
* @returns {number} The argest number
6+
*/
7+
const max = array => {
8+
if (array.length === 2) return array[0] > array[1] ? array[0] : array[1];
9+
const subMax = max(array.slice(1));
10+
return array[0] > subMax ? array[0] : subMax;
711
};
812

13+
/**
14+
* Calculate the largest number
15+
* This solution works for arrays of any length
16+
* @param {Array} array Array of numbers
17+
* @param {number} max Maximum value
18+
* @returns {number} The argest number
19+
*/
20+
const alternativeSolutionMax = (array, max = 0) =>
21+
array.length === 0
22+
? max
23+
: alternativeSolutionMax(array.slice(1), array[0] > max ? array[0] : max);
24+
925
console.log(max([1, 5, 10, 25, 16, 1])); // 25
26+
console.log(alternativeSolutionMax([1, 5, 10, 25, 16, 1])); // 25

04_quicksort/ES6/05_quicksort.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
const quickSort = (array) => {
2-
if (array.length < 2) {
3-
return array;
4-
}
1+
/**
2+
* Quick array sorting
3+
* @param {Array} array Source array
4+
* @returns {Array} Sorted array
5+
*/
6+
const quickSort = array => {
7+
if (array.length < 2) return array;
58
const pivot = array[0];
69
const keysAreLessPivot = array.slice(1).filter(key => key <= pivot);
710
const keysAreMorePivot = array.slice(1).filter(key => key > pivot);
8-
return [...quickSort(keysAreLessPivot), pivot, ...quickSort(keysAreMorePivot)];
11+
return [
12+
...quickSort(keysAreLessPivot),
13+
pivot,
14+
...quickSort(keysAreMorePivot)
15+
];
916
};
1017

1118
console.log(quickSort([10, 5, 2, 3])); // [2, 3, 5, 10]
Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,27 @@
11
/**
22
* Recursive function of Euclidean algorithm for two numbers
3-
*
43
* @param {number} a first number
54
* @param {number} b second number (base case)
6-
*
7-
* @return {number} GCD (greatest common divisor)
5+
* @returns {number} GCD (greatest common divisor)
86
*/
9-
let gcdOfTwo = ( a, b ) => {
10-
if ( !b ) {
11-
return a;
12-
}
13-
return gcdOfTwo( b, a % b );
14-
};
7+
const gcdOfTwo = (a, b) => (!b ? a : gcdOfTwo(b, a % b));
158

169
/**
1710
* Recursive function of Euclidean algorithm for set of the numbers
18-
*
1911
* @param {Array} set Set of the numbers
20-
*
21-
* @return {number} GCD (greatest common divisor)
12+
* @returns {number} GCD (greatest common divisor)
2213
*/
23-
let gcdOfSet = ( set ) => {
24-
let result = set[0];
25-
let newArr = Array.prototype.slice.call( set, 1 );
14+
const gcdOfSet = set => {
15+
let result = set[0];
16+
let newArr = set.slice(1);
2617

27-
newArr.map( ( el ) => {
28-
result = gcdOfTwo( result, el );
29-
} );
18+
newArr.map(el => {
19+
result = gcdOfTwo(result, el);
20+
});
3021

31-
return result;
22+
return result;
3223
};
3324

3425
const set = [1680, 640, 3360, 160, 240, 168000];
3526

36-
console.log( gcdOfSet( set ) ); // 80
27+
console.log(gcdOfSet(set)); // 80
Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
11
/**
22
* Recursive function of Euclidean algorithm
3-
*
43
* @param {number} a first number
54
* @param {number} b second number (base case)
6-
*
7-
* @return {number} GCD (greatest common divisor)
5+
* @returns {number} GCD (greatest common divisor)
86
*/
9-
let getGCD = ( a, b ) => {
10-
if ( !b ) {
11-
return a;
12-
}
13-
return getGCD( b, a % b );
14-
};
7+
const getGCD = (a, b) => (!b ? a : getGCD(b, a % b));
158

16-
const a = 1680;
17-
const b = 640;
18-
19-
console.log( getGCD( a, b ) ); // 80
9+
console.log(getGCD(1680, 640)); // 80
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
"use strict";
2+
13
/**
2-
* Sums values in array by loop "for"
3-
* @param {Array} arr Array of numbers
4-
* @return {total} Sum of the numbers
4+
* Sums values in the array by loop "for"
5+
* @param {Array} array Array of numbers
6+
* @returns {total} Sum of the numbers
57
*/
6-
7-
function sum(arr) {
8-
let total = 0;
9-
for (let i = 0; i < arr.length; i++) {
10-
total += arr[i];
11-
}
12-
return total;
8+
function sum(array) {
9+
let total = 0;
10+
for (let i = 0; i < array.length; i++) {
11+
total += array[i];
12+
}
13+
return total;
1314
}
1415

1516
console.log(sum([1, 2, 3, 4])); // 10

04_quicksort/javascript/01_loop_sum_reduce_version.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
"use strict";
2+
13
/**
2-
* Sums values in array by function "reduce"
3-
* @param {Array} arr Array of numbers
4-
* @return {number} Sum of the numbers
4+
* Sums values in the array by function "reduce"
5+
* @param {Array} array Array of numbers
6+
* @returns {number} Sum of the numbers
57
*/
6-
function sumReduce(arr) {
7-
return arr.reduce(function(curr, prev) {
8+
function sumReduce(array) {
9+
return array.reduce(function(curr, prev) {
810
return curr + prev;
911
});
1012
}

0 commit comments

Comments
 (0)