Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

268
Visualizações
codewars Longest Palindrome kata: function returns -Infinity error

Task: https://www.codewars.com/kata/54bb6f887e5a80180900046b/train/javascript

My solution:

function longestPalindrome(str){

var Palindromes = [], reverser = i => i.split("").reverse().join(""), l = str.length;
  
  if (str === Infinity || str === -Infinity)  {return 0} 

else{
for (let i = l ; i >= 0; i--){
  
 for(let j = 0; j < l; j++){
   temp = str.slice(j, i)
 if (reverser(temp) === temp){
   Palindromes.push(temp)
   }
  } 
 } 
  
 return Math.max(...Palindromes.map(e => e.length));
}
}

My code works perfectly in my editor console, but when I submit it on codewars it says "-Infinity should return 0"? -Infinity returns 0 in other consoles however? Is there something wrong with my code? Please don't link to another question. I think my code works (?) except for a minor tweak somewhere.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Check this out. Kind of brute-force solution

str = 'I like racecars that go fast'
const longestPalindrome = (s) => {
  const strLength = s.length
  if (strLength <= 1) return strLength;


  const strReverse = (s) => [...s].reverse().join('');

  colls = [...Array(strLength)].map((c, i) => i);
  rows = [...Array(strLength)].map((c, i) => i);

  const indexes = rows.reduce((acc, r) => (
    [...acc, ...colls.map((c) => (r > c) ? [c, r] : false)]
  ), []).filter((o) => o);

  const results = indexes.map(([begin, end]) => {
    const chunk = s.slice(begin, end + 1);
    return (strReverse(chunk) === chunk) ? end - begin + 1 : 0;
  });

  return Math.max(...results) || 1;
};
console.log(longestPalindrome(str))

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda