Merge branch 'quicksort_ES6_examples' of github.com:antonlipilin/grokking_algorithms into antonlipilin-quicksort_ES6_examples
This commit is contained in:
21
04_quicksort/ES6/04_loop_count.js
Normal file
21
04_quicksort/ES6/04_loop_count.js
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
/**
|
||||||
|
* Count the number of elements in the array
|
||||||
|
* @param {Array} array Array of numbers
|
||||||
|
* @returns {number} The number of elements in the array
|
||||||
|
*/
|
||||||
|
const countLoop = (array) => {
|
||||||
|
let result = 0;
|
||||||
|
|
||||||
|
if (array.length === 0) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < array.length; i += 1) {
|
||||||
|
result += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(countLoop([3,5,8,11])) //4
|
||||||
|
console.log(countLoop([])) // 0
|
||||||
9
04_quicksort/ES6/05_loop_count_reduce_version.js
Normal file
9
04_quicksort/ES6/05_loop_count_reduce_version.js
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
/**
|
||||||
|
* Count the number of elements in the array
|
||||||
|
* @param {Array} array Array of numbers
|
||||||
|
* @returns {number} The number of elements in the array
|
||||||
|
*/
|
||||||
|
const countReduce = (array) => array.reduce((count, next) => count += 1, 0);
|
||||||
|
|
||||||
|
console.log(countReduce([5,8,11,13])) // 4
|
||||||
|
console.log(countReduce([]))// 0
|
||||||
23
04_quicksort/ES6/07_loop_max.js
Normal file
23
04_quicksort/ES6/07_loop_max.js
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
/**
|
||||||
|
* Calculate the largest number
|
||||||
|
* This solution works for arrays of any length
|
||||||
|
* @param {Array} array Array of numbers
|
||||||
|
* @returns {number} The argest number
|
||||||
|
*/
|
||||||
|
const maxLoop = (array) => {
|
||||||
|
let result = 0;
|
||||||
|
|
||||||
|
if (array.length === 0) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < array.length; i += 1) {
|
||||||
|
if (array[i] > result) {
|
||||||
|
result = array[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log(maxLoop([2,5,7,22,11])); // 22
|
||||||
|
console.log(maxLoop([])) // 0
|
||||||
10
04_quicksort/ES6/08_loop_max_reduce_version.js
Normal file
10
04_quicksort/ES6/08_loop_max_reduce_version.js
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
/**
|
||||||
|
* Calculate the largest number
|
||||||
|
* This solution works for arrays of any length
|
||||||
|
* @param {Array} array Array of numbers
|
||||||
|
* @returns {number} The argest number
|
||||||
|
*/
|
||||||
|
const maxReduce = (array) => array.reduce((curr, next) => next > curr ? next : curr, 0);
|
||||||
|
|
||||||
|
console.log(maxReduce([1,3,11,7])) // 11
|
||||||
|
console.log(maxReduce([])) // 0
|
||||||
Reference in New Issue
Block a user