列表

详情


FED37. 数组求和

描述

计算并返回给定数组 arr 中所有元素的总和

输入描述

数组中的元素均为 Number 类型

示例1

输入:

[ 1, 2, 3, 4 ]

输出:

10

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 839ms, 内存消耗: 77768KB, 提交时间: 2021-02-21

{"css":"","js":"function sum(arr) {\n    var sum = 0;\n    for(var i = 0 ;i<arr.length;i++){\n        sum += arr[i]\n    }\n    return sum\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 840ms, 内存消耗: 77752KB, 提交时间: 2020-12-02

{"css":"","js":"function sum(arr) {\n    var sum = 0\n    arr.forEach( item => {\n        sum += item\n    })\n    return sum\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 840ms, 内存消耗: 77760KB, 提交时间: 2020-11-23

{"css":"","js":"function sum(arr) {\n    var sum1 = 0;\n    for(var i = 0; i < arr.length; i++) {\n       sum1 += arr[i];\n    }  \n    return sum1;\n}\n\nvar arr = [1,2,3,4];\nconsole.log(sum(arr));\n","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 840ms, 内存消耗: 77808KB, 提交时间: 2021-04-04

{"css":"","js":"function sum(arr) {\n    var total = 0;\n    for(var i = 0,len=arr.length;i < len;i++){\n        total += arr[i];\n    }\n    return total;\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 841ms, 内存消耗: 77760KB, 提交时间: 2021-03-21

{"css":"","js":"function sum(arr) {\n    return arr.reduce((prev, curr, idx, arr) => {\n        return prev + curr\n    })\n}","html":"","libs":[]}

上一题