Fix code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Fix code

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Palindrome Checker</title> </head> <body> <h1>Palindrome Checker</h1> <script> function isPalindrome(str) { // Remove non-alphanumeric characters and convert to lowercase const cleanStr = str.replace(/[^a-zA-Z0-9]/g, '').toLowerCase(); // Check if the clean string is equal to its reverse return cleanStr === cleanStr.split('').reverse().join(''); // Error: Missing semicolon } // Test cases try { console.log(isPalindrome("A man, a plan, a canal, Panama")); // Output: true console.log(isPalindrome("racecar")); // Output: true console.log(isPalindrome("hello")); // Output: false console.log(isPalindrome(123)); // This should throw an error } catch (error) { console.error(error.message); } </script> </body> </html>

5th May 2024, 3:08 AM
Vidhya Tharan
Vidhya Tharan - avatar
3 Answers
+ 1
The code you provided is ment to throw an error. You've created a test case that will throw an error because the isPalindrome function is expecting a string as an argument, but you're passing an integer (123) Changing it to "123" would make it a string and the test will pass.
6th May 2024, 8:39 PM
Chris Coder
Chris Coder - avatar
+ 4
what's your question? outputs: True True False <file_name>.html:27 str.replace is not a function
5th May 2024, 5:16 AM
Mihaly Nyilas
Mihaly Nyilas - avatar
0
Mihaly Nyilas error is generating.
5th May 2024, 6:13 AM
Vidhya Tharan
Vidhya Tharan - avatar