How to write an algorithm that adds the odd digits of one number to the end of a second number in TypeScript

1 Answer

0 votes
function addOddDigits(n: number, second_n: number) {
    let multiply: number = 1, odd: number = 0;
  
    while (n !== 0) {
        if (n % 2 !== 0) {
            odd += (n % 10) * multiply;
            multiply *= 10;
        }
  
        n = Math.floor(n / 10);
    }
  
    return second_n * multiply + odd;
}
 
const n: number = 12734, second_n = 600;
          
console.log(addOddDigits(n, second_n));
 
 
 
 
/*
run:
 
600173 
 
*/

 



answered Nov 6, 2024 by avibootz
...