You’ve already correctly reversed a string, but now you don’t seem to understand it.
Please ask any questions you have.
You’ve already correctly reversed a string, but now you don’t seem to understand it.
Please ask any questions you have.
Yes, please explain better…
how do I make the reversed string match the text AmanaplanacanalPanama
Can you please explain how you were able to reverse “Good, morning!”?
Just use the same method. It’s literally written out above, by you?
I assigned it to a variable named str, then assigned a new variable named reversed = str using dot notation to include split,reversed and join method
Yes! Can you do the same thing with 'AmanaplanacanalPanama'
?
Just replace "Good, morning!"
with 'AmanaplanacanalPanama'
done and I got the AmanaplanacanalPanama on the console output
Ok, can you please share the code here?
const checkBtn = document.getElementById("check-btn");
const textInput = document.getElementById('text-input');
const resultElement = document.getElementById('result');
checkBtn.addEventListener("click", () =>{
const alpha = textInput.value.replace(/[^A-Za-z0-9]/g, "");
const str= "AmanaplanacanalPanama";
const reversedStr = str.split("").reverse().join("");
if(textInput.value === ''){
alert('Please input a value');
}
else if(reversedStr === [...reversedStr]){
resultElement.innerText = `${textInput.value} is a palindrome`
}
else if(alpha === [...alpha].reverse().join('')){
resultElement.innerText = `${textInput.value} is a palindrome`
}
else{
resultElement.innerText = `${textInput.value} is not a palindrome`
}
})
``
Ok, I do not see a line of code that outputs/logs the reversedStr
to the console?
How are you able to see the reversed string?
Again you have already done this above:
You would just use the regular expression to remove characters in the original string that are not alphanumeric or spaces. Others are helping you understand that you then need to reverse that string. What else do you think you would need to do to that string to be able to compare it to itself reversed and test to see if the two strings match (a palindrome)?
Remember to console.log() as you code, so you can make sure your code is returning what you intend.
converting it to lower case
Yes! After you do that, you should just be able to write code to see if string1 === string 2, right?
when I console the reversedStr I see it on my console
but I don’t know how to compare it in my else if… statement