- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 numbers present in a string separated by spaces using JavaScript
Problem
We are required to write a JavaScript function that takes in a string which has integers inside it separated by spaces.
The task of our function is to convert each integer in the string into an integer and return their sum.
Example
Following is the code −
const str = '1 5 12 76 2'; const sumStringNumbers = (str = '') => { const findSum = (arr = []) => { const sum = arr.reduce((acc, val) => acc + val); return sum; }; let sum = 0; const arr = str .split(' ') .map(Number); sum = findSum(arr); return sum; }; console.log(sumStringNumbers(str));
Output
96
- Related Articles
- Summing array of string numbers using JavaScript
- Summing numbers from a string - JavaScript
- Find number of spaces in a string using JavaScript
- Removing all spaces from a string using JavaScript
- Finding the greatest and smallest number in a space separated string of numbers using JavaScript
- Picking all the numbers present in a string in JavaScript
- Validating a string with numbers present in it in JavaScript
- JavaScript - summing numbers from strings nested in array
- Summing cubes of natural numbers within a range in JavaScript
- Difference between numbers and string numbers present in an array in JavaScript
- Finding number of spaces in a string JavaScript
- Remove extra spaces in string JavaScript?
- How to sum a comma separated string (string with numbers) in MySQL?
- How to decrease size of a string by using preceding numbers - JavaScript?
- Finding the minimum and maximum value from a string with numbers separated by hyphen in MySQL?

Advertisements