views
Palindrome in JavaScript - javatpoint
In this topic, we will learn about Palindrome and validate that the given numbers or strings are Palindrome or not in JavaScript.
A palindrome is used to verify a sequence of numbers, strings, or letters that are read left to right and right to left to match the same characters or return the same sequence of characters. In simple terms, when the numbers, strings, or characters are reversed that return the same result as the original numbers or characters, it is called a Palindrome. For example, NITIN, 123454321, madam, etc. Suppose we have a word, madam. When we read the word madam from the forward and the backward end, it returns the same string. Therefore, we can refer to the string or number as the Palindrome.
Following are the steps to get the Palindrome in JavaScript, as follow:
string.html
Output
When the above code is executed, it shows the below image:
Now we enter the number or string on the prompt alert box and then click the OK button to validate the given string is Palindrome or not.
After that, it shows the below Output.
If the given string is not a Palindrome, it shows the string is not a Palindrome. Similarly, we can enter the number and validate whether the given number is Palindrome.
In this program, we use the built-in function like the split () method, reverse () method, and join () method to find the Palindrome of the number or string. Let's consider a JavaScript program to find the Palindrome of the given number using the built-in function.
Program2.html
Output
When we execute the above code, it shows the below image:
In the above images, we entered a string MADAM and then click the OK button to check the given string is a Palindrome or not.
After that, it shows the below image:
Following are the built-in function to check the palindrome in JavaScript.
Let's create a program to check whether the entered number is a Palindrome in JavaScript.
Str.html
Output
When the above programming code is executed, it returns the below images:
We enter the numbers and click on the Check button to validate the given number is Palindrome, as shown below.
In the above image, it shows that "It is a Palindrome Number." And if the reversed number is not the same as the original number, it shows that the entered number is not a Palindrome.
Let's create a program to check whether the entered string is a Palindrome in JavaScript.
Str2.html
Output
When the above programming code is executed, it returns the below image:
We entered the string "MADAM" and then click on the Submit button in the above output. It displays the below result, as shown:
Explanation of the code: When we entered the "MADAM" String in the textbox and then click on the Submit button. After that, the original string "MADAM" is reversed and compares each string's character with the original one. If both strings are equal, it shows that "It is a Palindrome String".
In the above output, when we entered the string JOHN, it displays the string is not a Palindrome string as it does not match the reversed characters of the string with the original.
SoapUI
RPA
Manual T.
Cucumber
Appium
PostgreSQL
Solr
MongoDB
Gimp
Verilog
Teradata
PhoneGap
Aptitude
Reasoning
Verbal A.
Interview
Company
AI
AWS
Selenium
Cloud
Hadoop
ReactJS
D. Science
Angular 7
Blockchain
Git
ML
DevOps
DBMS
DS
DAA
OS
C. Network
Compiler D.
COA
D. Math.
E. Hacking
C. Graphics
Software E.
Web Tech.
Cyber Sec.
Automata
C
C++
Java
.Net
Python
Programs
Control S.
Data Mining
Hindi100
Lyricsia
Website Development
Android Development
Website Designing
Digital Marketing
Summer Training
Industrial Training
College Campus Training
Address: G-13, 2nd Floor, Sec-3
Noida, UP, 201301, India
Contact No: 0120-4256464, 9990449935
© Copyright 2011-2018 www.javatpoint.com. All rights reserved. Developed by JavaTpoint.