The Code for TacoCat
function getString(){
let originalString = document.getElementById("userString").value;
if(originalString != "" && originalString.length > 1){
let userStr = originalString.replace(/\W/g, '').toLowerCase();
let msgObj = checkPalindrome(userStr)
displayMessage(msgObj)
} else {
alert("Please enter a phrase or word")
}
}
function checkPalindrome(str){
let revStr = "";
let msgObj = {}
for( let i = str.length -1; i >= 0 ; i --){
revStr += str[i];
}
(revStr == str)?(msgObj ={
heading : `You entered a Palindrome!`,
msg :`Your palindrome is ${str}.`
}):(msgObj ={
heading : `You did not enter a palindrome`,
msg : `${str} is not the same as ${revStr}`
})
return msgObj;
}
function displayMessage(obj){
document.getElementById("alert-header").innerHTML = obj.heading;
document.getElementById("msg").innerHTML = obj.msg;
document.getElementById("alert").classList.remove("invisible");
}
This project retrieves the user's string and checks to see if the string is a palindrome or not, and displays its results to the user.
getString
getString gets the user's input and validates the input value, making sure that it is a string. If it is not, then it will ask the user to enter a phrase or word. If it is, then it will change the string to lowercase and user regex to take out spaces and special characters. Then it will call checkPalindrome and use that result to call displayMessage.
checkPalindrome
checkPalindrome uses a for loop to create a new reversed string of the user's string. The function will then return a message object according to the results fo the palindrome check to be used in displayMessage.
displayMessage
displayMessage uses the results of checkPalindrome, takes out the class that makes the alert visible, and adds the message of the results to the alert dialoge box for the user to see.