Creating a senior memory book for my English class is one of my current projects, and being the nerdy student that I am, I've decided to incorporate some code into it. However, I'm facing some challenges with regex in my code. Here's the code snippet I'm working on:
//For future reference, "alert(*parametes*)" will make an alert box with the parameters inside it.
var phrase = prompt("Enter a phrase to be checked for palindrome.").toLowerCase()
var phraseBackwards = ""
for (var x in phrase) {
phraseBackwards += phrase[(phrase.length - 1) - x]
}
if (phraseBackwards == phrase) {
alert("The phrase you entered was a palindrome.")
} else {
if (phraseBackwards.replace("\s+/g", '') == phrase) {
alert("The phrase you entered would have been a palindrome, had it not had spaces")
} else {
if (phraseBackwards.replace(/\s+/g, '') != phrase) {
alert("The phrase you ented was not a palindrome.")
}
}
}
The specific section of code that is causing trouble:
if (phraseBackwards.replace(/\s+/g, '') == phrase) {
alert("The phrase you entered would have been a palindrome, had it not had spaces")
}
I understand that some parts of my code may not be optimal, but I'm trying to wrap things up quickly as I've procrastinated until the last minute.