Skip to content

Commit fb4e268

Browse files
committed
Merge branch 'quicksort_ES6_examples' of github.com:antonlipilin/grokking_algorithms into antonlipilin-quicksort_ES6_examples
2 parents 5d9ae51 + 2080afc commit fb4e268

8 files changed

+63
-0
lines changed

04_quicksort/ES6/04_loop_count.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 countLoop = (array) => {
7+
let result = 0;
8+
9+
if (array.length === 0) {
10+
return result;
11+
}
12+
13+
for (let i = 0; i < array.length; i += 1) {
14+
result += 1;
15+
}
16+
17+
return result;
18+
}
19+
20+
console.log(countLoop([3,5,8,11])) //4
21+
console.log(countLoop([])) // 0
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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 countReduce = (array) => array.reduce((count, next) => count += 1, 0);
7+
8+
console.log(countReduce([5,8,11,13])) // 4
9+
console.log(countReduce([]))// 0

04_quicksort/ES6/07_loop_max.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Calculate the largest number
3+
* This solution works for arrays of any length
4+
* @param {Array} array Array of numbers
5+
* @returns {number} The argest number
6+
*/
7+
const maxLoop = (array) => {
8+
let result = 0;
9+
10+
if (array.length === 0) {
11+
return result;
12+
}
13+
14+
for (let i = 0; i < array.length; i += 1) {
15+
if (array[i] > result) {
16+
result = array[i]
17+
}
18+
}
19+
return result;
20+
};
21+
22+
console.log(maxLoop([2,5,7,22,11])); // 22
23+
console.log(maxLoop([])) // 0
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Calculate the largest number
3+
* This solution works for arrays of any length
4+
* @param {Array} array Array of numbers
5+
* @returns {number} The argest number
6+
*/
7+
const maxReduce = (array) => array.reduce((curr, next) => next > curr ? next : curr, 0);
8+
9+
console.log(maxReduce([1,3,11,7])) // 11
10+
console.log(maxReduce([])) // 0
File renamed without changes.

0 commit comments

Comments
 (0)