JavaScript palindrome checker
JavaScript palindrome checker
We are going to create a small script that will allow us to check if a word is a palindrome. To do this, nothing could be simpler, we will need HTML and JavaScript (A javascript palindrome Checker).
But first of all, let's have a clear notion about Palindromes
Definition:
Palindromes are texts or words that can be read in both directions. The palindrome is a literary game based on symmetry: a word or a group of words must be identical, by the letters, by the syllables, or by the sound produced, when it is read from left to right and from right to left. In other words, the word read backward must be the same as the one read upright. No rule forces the author of a palindrome (a palindromist) to respect spaces or accents. Most often, the order of the letters is the same from left to right and from right to left.
Examples
The phrase "Aesop stays here and rests", read from left to right or right to left, is identical. "Summer", "kayak" or "here" are palindromic words. "1001" is a palindromic number.
Types of palindromes
Character-unit palindromes:
Character-unit palindromes are single words that read the same backward and forward, such as “racecar,” “kayak,” “repaper,” or “tenet.” They are the palindromic examples most widely known to English-language speakers. “Aibohphobia” is a word that refers to “a fear of palindromes,” and it’s also a character-unit palindrome.
Line palindromes:
Also known as line-unit palindromes, these poems usually have an initial set of lines that reverse order halfway through the piece without changing the word order within the line.
Musical palindromes:
Certain musical compositions are also palindromic. For example, Joseph Haydn’s Symphony No. 47 features a minute and a trio in its third movement. The second halves of the minuet and trio are identical to the first halves but backward, and the minuet itself repeats at the end of its second half.
Name palindromes:
There are many notable palindromic first names, such as Hannah, Eve, Bob, and Otto. Some individuals have palindromic first and last names, like flamenco dancer Sara Baras. There are also palindromic band names, like Abba, a Swedish pop group.
Numeric palindromes:
Numbers can be palindromes if the digits read the same backward and forward, such as 10101. Dates, too, can be palindromes; February 2, 2020, is officially known as “Universal Palindrome Day” because the digits form a palindrome regardless of date format (day/month/year or month/day/year)
Palindromepoetry:
Palindrome poems use line palindromes within a poetic form. They begin with an initial poem and then feature a line at the halfway mark that reverses the rest of the lines in order. Its unique structure gives it the alternate name “mirrored poetry.” Learn more about palindrome poetry.
Palindrome sentences:
Palindrome sentences take the form of complete sentences with a subject, verb, and predicate. While punctuation and capitalization are not necessary to the form, some examples include punctuation, like “Go hang a salami, I’m a lasagna hog.”
Semordnilap:
Words that, when spelled backward, form a new word are called semordnilap, which is also a reverse spelling of “palindromes.” Examples include “stressed,” which is “desserts” spelled backward. Some semordnilaps are deliberate creations, like “mho,” which is “ohm”—a unit of electrical resistance—spelled backward.
Word-unit palindromes:
Phrases, sentences, or groups of words that appear in identical order when viewed forward or backward are word-unit palindromes. A word-unit palindrome in phrase form is “Murder for a jar of red rum.” A sentence-form palindrome is, “Did I say you ‘never say never?’ You say I did.”
We will make a small script that will allow us to check if a word is a palindrome. To do this nothing simpler, we will need HTML and Javascript.
Step 1:
Place an index.html page with the script that redirects to the script treatment page.js:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Palindrome</title>
</head>
<body>
<script type="text/javascript" src="js/script.js"></script>
</body>
</html>
Step2 :
Write a function that will take as input the value entered by the user.
function Palindrome(input)
{
var enter = prompt("Please enter your word").toLowerCase();
input_1 = [...input].reverse().join('');
}
Palindrome();
prompt() is the function that will allow the user to enter the value of their choice. Concatenated to the toLowerCase() function, we convert all user input to lowercase.
input_1 is the variable that retrieves what has been entered by the user and converts the character string into an array. Then, we reverse using the reverse() function the position of each entry in the array, then we assemble the result with the join() function to form a new character string.
Step 3:
All its steps respected, we will carry out checks to determine the words which are palindromes thanks to if(), else if() and else() conditions
var enter = prompt("Please enter your word").toLowerCase();
input_1 = [...input].reverse().join('');
if (input === "") {
console.log('Please enter a word first');
}
else if (input === input_1) {
console.log(input + ' is a palindrome');
}
else {
console.log(input + ' is not a palindrome');
}
The first condition checks if there is an entry.
The second compares if the two inputs (user input and comparison input) match, if so display a message that shows that the two words are palindromes.
The third condition allows us to say that the two words are not palindromes.
Following all its steps, you will have a working palindrome checker. you can customize it as you wish.