
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Picking all the numbers present in a string in JavaScript
We are required to write a JavaScript function that takes in a string that contains some one-digit numbers in between and the function should return the sum of all the numbers present in the string.
Example
The code for this will be −
const str = 'uyyudfgdfgf5jgdfj3hbj4hbj3jbb4bbjj3jb5bjjb5bj3'; const sumNum = str => { const strArr = str.split(""); let res = 0; for(let i = 0; i < strArr.length; i++){ if(+strArr[i]){ res += +strArr[i]; }; }; return res; }; console.log(sumNum(str));
Output
The output in the console −
35
- Related Articles
- Validating a string with numbers present in it in JavaScript
- Difference between numbers and string numbers present in an array in JavaScript
- Summing numbers present in a string separated by spaces using JavaScript
- Picking all elements whose value is equal to index in JavaScript
- Reversing words present in a string in JavaScript
- Picking the odd one out in JavaScript
- Product of numbers present in a nested array in JavaScript
- Picking index randomly from array in JavaScript
- Picking the largest elements from multidimensional array in JavaScript
- Moving all zeroes present in the array to the end in JavaScript
- Find all the numbers in a string using regular expression in Python
- Sum of all positives present in an array in JavaScript
- Picking out uniques from an array in JavaScript
- Pick out numbers from a string in JavaScript
- Program to find all substrings whose anagrams are present in a string in Python

Advertisements