Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

266
Views
codewars Longest Palindrome kata: la función devuelve -Infinity error

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

Mi solución:

 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)); } }

Mi código funciona perfectamente en la consola de mi editor, pero cuando lo envío en codewars dice "-Infinity debería devolver 0"? -¿Infinity devuelve 0 en otras consolas sin embargo? ¿Hay algo mal con mi código? Por favor, no enlace a otra pregunta. Creo que mi código funciona (?) Excepto por un ajuste menor en alguna parte.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Mira esto. Tipo de solución de fuerza bruta

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!