Increase readability, update code style and optimise for modern JavaScript (#273)
* Remove loops on recursive example, increase readability and optimise * Update style and optimise * Update 01_loop_sum_reduce_version.js Update style * Update style * Rename identificators * Rename identificators * Convert shift to splice
This commit is contained in:
@@ -5,10 +5,6 @@
|
||||
* @param {Array} array Array of numbers
|
||||
* @returns {number} Sum of the numbers
|
||||
*/
|
||||
function sumReduce(array) {
|
||||
return array.reduce(function(prev, cur) {
|
||||
return prev + cur;
|
||||
});
|
||||
}
|
||||
const sumReduce = (array) => array.reduce((prev, cur) => prev + cur, 0);
|
||||
|
||||
console.log(sumReduce([1, 2, 3, 4])); // 10
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
"use strict";
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* 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 == 0) return 0;
|
||||
const sumRecursive = (arr = []) => {
|
||||
if (!arr.length) return 0;
|
||||
return arr[0] + sumRecursive(arr.slice(1));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
console.log(sumRecursive([1, 2, 3, 4])); // 10
|
||||
|
||||
Reference in New Issue
Block a user