This code seems correct but is not passing any tests.
When I run the test string “(0_0 /-\ (: 0-0)” I receive the error “Numeric separator cannot be used after leading 0.” Why is this? I would be grateful to see solutions written with the same techniques.
function palindrome(string) {
let extras = /[\W_]/g;
let newString = string.replace(extras,"").toLowerCase();
console.log(newString);
let forwards = "";
let backwards = "";
for (let i = 0; i < newString.length; i++) {
forwards += newString[i];
backwards = newString[i] + backwards;
}
console.log(forwards);
console.log(backwards);
if (forwards == backwards) {
return true;
}
else {return false;}
}
palindrome(0_0 (: /-\ :) 0-0);
I created a rex exp for non-alphanumeric numbers as a variable. That variable was used in replace to delete those symbols.
The string was put in one case.
New string is made visible.
A for loop displays the word forwards and backwards.
A conditional statement compares forwards to backwards spelling.
A boolean returns true for palindromes, false for not.
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.47
.
Challenge: Palindrome Checker
Link to the challenge: