diff --git a/04_quicksort/ES6/04_loop_count.js b/04_quicksort/ES6/04_loop_count.js new file mode 100644 index 0000000..676f8cc --- /dev/null +++ b/04_quicksort/ES6/04_loop_count.js @@ -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 \ No newline at end of file diff --git a/04_quicksort/ES6/05_loop_count_reduce_version.js b/04_quicksort/ES6/05_loop_count_reduce_version.js new file mode 100644 index 0000000..e96cec3 --- /dev/null +++ b/04_quicksort/ES6/05_loop_count_reduce_version.js @@ -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 \ No newline at end of file diff --git a/04_quicksort/ES6/04_recursive-max.js b/04_quicksort/ES6/06_recursive-max.js similarity index 100% rename from 04_quicksort/ES6/04_recursive-max.js rename to 04_quicksort/ES6/06_recursive-max.js diff --git a/04_quicksort/ES6/07_loop_max.js b/04_quicksort/ES6/07_loop_max.js new file mode 100644 index 0000000..06f49da --- /dev/null +++ b/04_quicksort/ES6/07_loop_max.js @@ -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 \ No newline at end of file diff --git a/04_quicksort/ES6/08_loop_max_reduce_version.js b/04_quicksort/ES6/08_loop_max_reduce_version.js new file mode 100644 index 0000000..ea9fb22 --- /dev/null +++ b/04_quicksort/ES6/08_loop_max_reduce_version.js @@ -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 \ No newline at end of file diff --git a/04_quicksort/ES6/05_quicksort.js b/04_quicksort/ES6/09_quicksort.js similarity index 100% rename from 04_quicksort/ES6/05_quicksort.js rename to 04_quicksort/ES6/09_quicksort.js diff --git a/04_quicksort/ES6/07_euclidean_algorithm_two_numbers.js b/04_quicksort/ES6/10_euclidean_algorithm_two_numbers.js similarity index 100% rename from 04_quicksort/ES6/07_euclidean_algorithm_two_numbers.js rename to 04_quicksort/ES6/10_euclidean_algorithm_two_numbers.js diff --git a/04_quicksort/ES6/06_euclidean_algorithm_set_numbers.js b/04_quicksort/ES6/11_euclidean_algorithm_set_numbers.js similarity index 100% rename from 04_quicksort/ES6/06_euclidean_algorithm_set_numbers.js rename to 04_quicksort/ES6/11_euclidean_algorithm_set_numbers.js