How to find the lunar sum of numbers (take the bigger of the corresponding digits) in Node.js

1 Answer

0 votes
const lunarSum = (num1, num2) => {
    const numStr1 = String(num1);
    const numStr2 = String(num2);
    let result = 0;
    
    for (let i = 0; i < numStr1.length; i++) {
        let temp = Math.max(numStr1[i], numStr2[i]);
        result = (result * 10) + temp;
    };
    
    return result;
};

const num1 = 9762;
const num2 = 3485;

console.log(lunarSum(num1, num2));

  
  
  
/*
run:
  
9785
  
*/

 



answered Mar 23, 2024 by avibootz
...