The Code for Taco Cat

                        
                            
                            function getValue() {
                            //hide alert before function is executed
                            document.getElementById("alert").classList.add('invisible');
                            //Get user Input from the DOM:
                            let userString = document.getElementById("userString").value;
                            let returnObject = ckPalindrome(userString);
                            displayData(returnObject);
                            }
                            //LOGIC: reverse string and comparing for palindrome
                            
                            function ckPalindrome(userString) {
                            //make all characters lower case to remove casing conflicts in comparisons
                            userString = userString.toLowerCase();
                            //remove special characters and spaces
                            let regex = /[^a-z0-9]/gi;
                            userString = userString.replace(regex, "");
                            
                            let revString = [];
                            returnObject = {};
                            //Reversing the user string
                            for (let index = userString.length - 1; index >= 0; index--) {
                            revString += userString[index];
                            
                            }
                            if (revString == userString) {
                            returnObject.msg = "Congratulations! 
                            You have a palindrome."
                            
                            }
                            else {
                            returnObject.msg = "Sorry, you do not have a palindrome."
                            }
                            
                            returnObject.reversed = revString;
                            return returnObject;
                            }
                            
                            //Display data back to the DOM
                            
                            function displayData(returnObject) {
                            
                            document.getElementById("alertHeader")
                            .innerHTML = returnObject.msg;
                            document.getElementById("msg").innerHTML 
                            = ` Your string reversed is: "${returnObject.reversed}."`
                            //Make alert visible
                            document.getElementById("alert").classList.remove('invisible');
                            
                            }
                        
                
            
                        
                    

This JavaScript challenge reverses a string and checks for a palindrome.

TACO CAT

Backwards and forwards--the mystery of the palindrome. This Javascript coding exercise takes a forward-looking string, reverses it and compares the user-inputed string to the reverse string to see if a palindrome exists.

Brief code overview:

  • Initiation:
    • the event listener (located in the bottom <script> tag on the app page) is triggered by a click on the submit button;
    • A classlist.add is placed on the alert to hide it on the page until the DisplayData function is called.
    • the value from the user input field (userString) is passed into the Get Values function;

  • Validation:

    No validation is needed since differentiation between integers and strings is unnecessary; the program will treat numbers as strings.


  • Logic:

    When the ckPalindrome function is called by declaring returnObject, the following processes occur:

    • All the characters in the userString are set to lowercase to remove casing errors in camparison
    • A regular operator, or regex, is used to remove all special characters and spaces;
    • the reverseString Array is declared in order to hold the reversed userString contents;
    • the returnObject object is declared and will hold two properties: the message and the reverseString;
    • the decrementing for loop begins at the last index position of the userString and decrements the string until index is less than or equal to zero.
    • results of each iteration is passed to the reverseString
    • In the if/else if statement the reverseString is tested against the userString (refer to the code on this page)
    • the results are passed to the returnObject

  • Display:
  • Inside the DisplayData function:
    • A classlist.remove is placed on the alert to make it visable on the page
    • the returnObject.msg (determined in the if statement described above) is printed to the page announcing the palindrome results;
    • the returnObject.reversed results are also printed on the page