Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // to calculate the sum of array elements
- const sum_declarative = (arr) => {
- let result = 0;
- for (let i = 0; i < arr.length; i++) {
- result += arr[i];
- }
- return result;
- };
- console.log(sum_declarative([1,2,3]));
- // calculate the sum of array elements
- const sum_imperative = (arr) => arr.reduce((total, item) => total += item, 0);
- console.log(sum_imperative([1,2,3]));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement