
- 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
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 Articles
- Finding sum of all numbers within a range in JavaScript
- Finding sum of remaining numbers to reach target average using JavaScript
- Finding closest pair sum of numbers to a given number in JavaScript
- Finding sum of multiples in JavaScript
- Finding two numbers that produce equal to the sum of rest in JavaScript
- Finding tidy numbers - JavaScript
- Finding special type of numbers - JavaScript
- Finding two numbers given their sum and Highest Common Factor using JavaScript
- Finding pandigital numbers using JavaScript
- Finding perfect numbers in JavaScript
- Finding the sum of all numbers in the nth row of an increasing triangle using JavaScript
- 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 sum of first n natural numbers in PL/SQL

Advertisements