使用 JavaScript 对数字字符串数组求和
问题
我们将编写一个 JavaScript 函数,该函数接收一个包含整数和数字字符串的数组。
我们的函数应将所有整数和数字字符串相加,以得出新数字,并返回该数字。
示例
以下是代码 −
const arr = [67, 45, '34', '23', 4, 6, '6'];
const mixedSum = (arr = []) => {
let sum = 0;
for(let i = 0; i < arr.length; i++){
const el = arr[i];
sum += +el;
};
return sum;
};
console.log(mixedSum(arr));输出
185
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP