Validate palindromes with javascript 💻 2022

verificar palindromos con javascript | discover palindromes with javascript code
k

23 September 2020

Comparte | Share 

Validate Palindromes with Javascript code

In this post, we will know how to validate palindromes with javascript code. Firstly we should begin by explaining, what the term palindrome specifically means. Well, no more, no less than we can read text or words from left to right as well as from right to left without changing their meaning.

This type of exercise is usually part of the practical tests for javascript students.

With a little bit of JavaScript code, we will be able to know if a word is a palindrome or not. If this text or word is a palindrome, then we will get a message confirming otherwise the message will confirm that it is not.

With the following code we ask the user to enter a word or term. Once it has been entered a pop-up message will confirm it is or not. To make it easier to verify what the user has entered, the javascript code does not take into account the spaces between words that may be there.

JS


<script> 
var cadena = prompt('Introduzca un texto o palabra por favor');
cadena = cadena.toLowerCase().replace(/\s/g,"");
var reves = cadena.split("").reverse().join("");

function palindromo (cadena) {
    if(cadena == reves){
      alert('El texto o palabra introducido es un palindromo');
    }
    else{
      alert('El texto o palabra introducido NO es un palindromo');
    }
 }
palindromo (cadena);
</script>

 

Verify palindromes with Javascript code step by step

Firstly we create a variable called cadena. In this variable, we will see the option prompt. When the prompt option is used the browser will give us the possibility to insert text into a small pop-up. Therefore the information that the user enters in this pop-up will be stored in the variable cadena.

Since we do not know if the user will enter uppercase or lowercase letters, we apply the toLowerCase ( ) method . This method converts the entered text to lowercase letters, in case uppercase letters are entered.

With the replace ( ) method, we remove the empty spaces that the text inserted by the user may have.

As a result of those two methods, toLowerCase() and replace(), we are converting what the user has written in text without spaces and in lower case.

This format will help us in the comparison to validate palindromes with javascript code and we avoid the potential “case sensitive” and space errors of letters or names.

We also create another variable called reves. This variable converts the information stored in the variable cadena from right to left.

After this, we are ready to compare the values of cadena and reves.

The last step of the verification of a word being a palindrome with JavaScript code is carrying out the comparison by the function palindrome.

If the value of both of them does not match, then an unsuccessful message will come up. However, if the value matches with each other, we will get a successful message and we can happily say that we have verified palindromes with javascript code.

In the image below we can see that the word “RECONOCER” is a perfect example of a palindrome. With the code explained above we will be able to verify palindromes with Javascript.

verificar palindromos con javascript | discover palindromes with javascript code

Creatuwebpymes, is a web design company based in Lanzarote – Canary Islands. We ❤️ programming in JavaScript, hence the reason for this post.

This post is an exercise that JavaScript students usually come across in their initial projects. If you wish to learn more about this language, here are some ebooks to learn JavaScript code

Valora la entrada | Rate the post
Comparte | Share
siteground_banner
Divi Cake is a community driven marketplace for Divi Child Themes, Builder Layouts, and Plugins - Shop Now!
creatuwebpymes quien somos

Francisco Brito Diaz

CEO of creatuwebpymes.com, Web Design Company & Digital Marketing in Lanzarote – Canary Islands.