
- 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
Adding numbers represented by string without complete conversion in JavaScript
We are required to write a JavaScript function that takes in two strings, str1 and str2 that represents two numbers.
Without converting the whole strings to respective numbers, our function should calculate the sum of those two string numbers and return the result as a string.
For example −
If the two strings are −
const str1 = '234'; const str2 = '129';
Then the output should be 363.−
Example
Following is the code −
const str1 = '234'; const str2 = '129'; const addStringNumbers = (str1, str2) => { let ind1 = str1.length - 1, ind2 = str2.length - 1, res = "", carry = 0; while(ind1 >= 0 || ind2 >= 0 || carry) { const val1 = str1[ind1] || 0; const val2 = str2[ind2] || 0; let sum = +val1 + +val2 + carry; carry = sum > 9 ? 1 : 0; res = sum % 10 + res; ind1--; ind2--; }; return res; }; console.log(addStringNumbers(str1, str2));
Output
Following is the console output −
363
- Related Articles
- Adding binary without converting in JavaScript
- Binary to original string conversion in JavaScript
- Add two numbers represented by linked lists?
- By which letter irrational numbers are represented?
- Add number strings without using conversion library methods in JavaScript
- Multiply two numbers represented by Linked Lists in C++
- Add two numbers represented by two arrays in C Program
- Adding only odd or even numbers JavaScript
- Number of carries required while adding two numbers in JavaScript
- Complete Equation by Filling Missing Operator in JavaScript
- Summing numbers present in a string separated by spaces using JavaScript
- Adding one to number represented as array of digits in C++?
- Adding paragraph tag to substrings within a string in JavaScript
- What is the maximum value represented by Number object in JavaScript?
- What is the minimum value represented by Number object in JavaScript?

Advertisements