- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Summing array of string numbers using JavaScript
Problem
We are required to write a JavaScript function that takes in an array that contains integers and string numbers.
Our function should sum all the integers and string numbers together to derive a new number and return that number.
Example
Following is the code −
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));
Output
185
Advertisements