
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding lunar sum of Numbers - JavaScript
Lunar Sum
The concept of lunar sum says that the sum of two numbers is calculated, instead of adding the corresponding digits, but taking the bigger of the corresponding digits.
For example −
Let’s say,
a = 879 and b = 768
(for the scope of this problem, consider only the number with equal digits)
Then the lunar sum of a and b will be −
879
We are required to write a JavaScript function that takes in two numbers and returns their lunar sum.
Example
Following is the code −
const num1 = 6565; const num2 = 7385; const lunarSum = (num1, num2) => { const numStr1 = String(num1); const numStr2 = String(num2); let res = 0, temp; for(let i = 0; i < numStr1.length; i++){ temp = Math.max(+numStr1[i], +numStr2[i]); res = (res*10) + temp; }; return res; }; console.log(lunarSum(num1, num2));
Output
Following is the output in the console −
7585
- Related Questions & Answers
- Finding sum of all numbers within a range in JavaScript
- Finding sum of remaining numbers to reach target average using JavaScript
- Finding sum of multiples in JavaScript
- Finding special type of numbers - JavaScript
- Finding closest pair sum of numbers to a given number in JavaScript
- Finding tidy numbers - JavaScript
- Finding two numbers that produce equal to the sum of rest in JavaScript
- Finding pandigital numbers using JavaScript
- Finding perfect numbers in JavaScript
- Finding sum of first n natural numbers in PL/SQL
- Finding the sum of unique array values - JavaScript
- Finding sum of all unique elements in JavaScript
- Finding the nth digit of natural numbers JavaScript
- Finding two numbers given their sum and Highest Common Factor using JavaScript
- Sum of consecutive numbers in JavaScript
Advertisements