Palindrome Checker in JavaScript

Palindrome Checker In JavaScript
Code Snippet:Palindrome Checker
Author: Aleksandar Sandro Cvetković
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 677
License: MIT
Edit Code online: View on CodePen
Read More

A palindrome is a word or sentence that reads the same forwards and backward, ignoring punctuation, case, and spacing. This JavaScript code allows you to create a Palindrome Checker tool to quickly check whether a given string is a palindrome or not.

It comes with an easy-to-use interface, simply type a word or sentence into the input field and click the “Check” button. It will then tell you whether the input is a palindrome or not.

This code works by removing non-alphanumeric characters, converting the string to lowercase, reversing it, and comparing it to the original string. It’s a handy tool for verifying palindromes easily.

How to Create Palindrome Checker In Javascript

1. To use the Palindrome Checker, you need to create an HTML structure. Add the following HTML code to your web page:

<div class="container">
      <div class="main">
        <h1>Palindrome Checker</h1>
        <p>
          A palindrome is a word or sentence that's spelled the same way both
          forward and backward, ignoring punctuation, case, and spacing.
          Palindrom Checker will return <span>true</span> if the given string is
          a palindrome. Otherwise it will return <span>false</span>.
        </p>
        <input type="text" id="check" placeholder="Type any palindrom..." />

        <button onclick="palindrome(document.getElementById('check').value)">
          Check
        </button>

        <p id="result">Click the "Check" button for the result</p>
      </div>
    </div>

This HTML code includes the necessary structure for your palindrome checker, including a title, an input field, and a “Check” button.

2. Style the Palindrome Checker interface using the following CSS styles:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: "Muli", sans-serif;
  color: #fff;
}

/* Background */
.container {
  align-items: center;
  /*Photo by Johannes Plenio on Unsplash - https://unsplash.com/photos/HNDg-SQjDMI*/
  background-image: url("https://images.unsplash.com/photo-1577578509425-b29a6d6e663f?ixlib=rb-1.2.1&auto=format&fit=crop&w=1778&q=80");
  background-size: cover;
  background-position: bottom center;
  color: #fff;
  display: flex;
  height: 100vh;
  justify-content: center;
  position: relative;
  text-align: center;
  z-index: 1;
}

.container::before {
  content: " ";
  background-color: rgba(0, 0, 0, 0.3);
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  z-index: -1;
}

/* Main */
.main {
  margin: 5px 5px;
}

.main h1 {
  font-size: 46px;
  margin: 20px 0px 0px;
}

.main p {
  font-size: 20px;
  letter-spacing: 1px;
  margin: 50px 100px;
}

.main span {
  font-weight: 700;
  color: blanchedalmond;
}

#result {
  font-weight: 700;
  color: blanchedalmond;
  letter-spacing: 1px;
}

.main input {
  width: 20%;
  height: 40px;
  border: none;
  border-radius: 3px;
  font-size: 12px;
  padding: 10px 10px;
}

.main button {
  width: 8%;
  height: 40px;
  border-radius: 3px;
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  background-image: linear-gradient(to bottom right, #ff4e50, #f9d423);
  border: 1px solid rgba(255, 255, 255, 0.3);
  padding: 10px 10px;
}

@media (max-width: 1200px) {
  .main input {
    width: 55%;
  }
  .main button {
    width: 25%;
  }
  .main p {
    margin: 30px 50px;
  }
  #result {
    margin: 30px 50px;
  }
}

3. Finally, add the following JavaScript code to your project.

function palindrome(str) {
  let originalString = str.replace(/[\W_]/g, "").toLowerCase();

  let strCheck = str
    .toLowerCase()
    .replace(/[\W_]/g, "")
    .split("")
    .reverse("")
    .join("");

  if (originalString === strCheck) {
    return (document.getElementById("result").innerHTML =
      "This is a palindrome.");
  }
  return (document.getElementById("result").innerHTML =
    "It's a trap, this is not a palindrome!");
}

That’s all! hopefully, you have successfully created Palindrome Checker in JavaScript. This tool makes it easy to determine if a string is a palindrome. Whether you want to test words, phrases, or entire sentences, this code will help you quickly identify palindromes with ease.

If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X