• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

148
Views
Palindrome dice infinito cuando se compara en la declaración final if

ok, lo he estado haciendo durante un tiempo, estoy haciendo un programa palíndromo en html por primera vez, pero cuando ingreso 111 dice que no es un palíndromo, aunque pasé por el bucle while con un cálculo y debería ser el mismo . Revisé las salidas revelando em y "input" = 111. "temp" = 0 como debería. "invertido" aunque es igual a infinito lol y idk y cualquier ayuda es apreciada.

 function Palindrome() { let input = document.querySelector("#userInput").value; var temp = input; let reversed = 0; while (temp > 0) { const lastDigit = temp % 10; reversed = (reversed * 10) + lastDigit; temp = (temp / 10); } if (input == reversed) { document.querySelector("#answer").innerHTML = `This is a Palindrome `; }else { document.querySelector("#answer").innerHTML = `This is not a Palindrome ${input} ${temp} ${reversed} `; } }
almost 3 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Estás haciendo una división de coma flotante, por lo que la temp nunca será más de 0 hasta que hagas muchas iteraciones y obtengas un subdesbordamiento de coma flotante. Después de cada iteración, se convierte en 11.1 , luego en 1.11 , luego en 0.111 , .0111 , y así sucesivamente. Esto es multiplicar al reversed por 10 cada vez, y finalmente se desborda hasta el infinito.

Redondea el número al dividir para que se descarte la fracción:

 temp = Math.round(temp / 10); 

 function Palindrome(input) { var temp = input; let reversed = 0; while (temp > 0) { const lastDigit = temp % 10; reversed = (reversed * 10) + lastDigit; temp = Math.round(temp / 10); } if (input == reversed) { console.log(`This is a Palindrome ${input}`); } else { console.log(`This is not a Palindrome ${input} ${temp} ${reversed} `); } } Palindrome(111); Palindrome(123)

almost 3 years ago · Juan Pablo Isaza Report

0

Como explicó Barmar en su respuesta sobre la división porcentual, ¡intenta deshacerte de ella!

 function Palindrome() { let input = document.querySelector("#userInput").value; //convert input to number var temp = +input;//or parseInt(input) let reversed = 0; while (temp > 0) { const lastDigit = temp % 10; reversed = reversed * 10 + lastDigit; temp = Math.floor(temp / 10);//<-- to prevent Infinity } if (input == reversed) { document.querySelector("#answer").innerHTML = `This is a Palindrome `; } else { document.querySelector( "#answer" ).innerHTML = `This is not a Palindrome ${input} ${temp} ${reversed} `; } }
 <input type="input" value="" id="userInput"> <input type="button" value="Is Palindrom" onclick="Palindrome()"> <div id='answer'></div>

almost 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error