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:
Timur Sevimli
2024-12-07 16:48:14 +03:00
committed by GitHub
parent c06ad954f0
commit 45bf3c086d
4 changed files with 46 additions and 56 deletions

View File

@@ -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

View File

@@ -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