Fixed formatting problems, example and JSDoc
This commit is contained in:
@@ -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