DEV Community

Discussion on: Project Euler #4 - Largest Palindrome Product

Collapse
 
flrnd profile image
Florian Rand • Edited

My typescript solution:

function reverseInteger(n: number): number {
  return Number(String(n).split('').reverse().join(''));
}

function isPalindrome(num: number): boolean {
  if (num === reverseInteger(num)) {
    return true;
  }
  return false;
}

// I had this in separate files sorry if its confusing X)
import { max } from 'mathjs';

let number = 0;
let a = 999;

const palindromes: number[] = [];

while (a > 1) {
  for (let i = 2; i <= 999; i += 1) {
    number = a * i;
    if (isPalindrome(number)) {
      palindromes.push(number);
    }
  }
  a -= 1;
}
console.log(`Max palindrome ${max(...palindromes)}`);
Collapse
 
maxart2501 profile image
Massimo Artizzu
if (condition) {
  return true;
}
return false;

Please don't do this 😕 It's verbose for no reason. You can accomplish the same with just return condition;.

Collapse
 
flrnd profile image
Florian Rand

cool! yes so much simple

function isPalindrome(num: number): boolean {
  return num === reverseInteger(num);
}

thanks!