Inleiding tot Palindrome in JavaScript

In algemene zin is Palindrome een woord zoals dat wanneer we dat woord karakter voor karakter van voren lezen, het exact overeenkomt met een woord dat wordt gevormd wanneer hetzelfde woord van achteren wordt gelezen. Bijvoorbeeld: "niveau", "mevrouw" enz. Wanneer het woord "niveau" van achteren wordt geschreven, zal ook het uiteindelijke gevormde woord "niveau" zijn. Dit soort woorden, cijfers, tekenreeksen of reeksen tekens worden geschreven door elke computertaal. Dan wordt dergelijke functionaliteit een palindroom genoemd. In het taalpalindroom van de programmeur staat een reeks tekens, cijfers die niet veranderen, zelfs niet als het vanuit de omgekeerde richting wordt geschreven en een herschikt woord vormt. JavaScript biedt verschillende ingebouwde functies om deze functionaliteit te realiseren. We kunnen ook lussen hebben om hetzelfde resultaat te verkrijgen. In dit artikel gaan we meer onderzoek doen naar palindroom in client-side programmeertaal JavaScript.

Logische uitleg van Palindrome in JavaScript

Hieronder vindt u het codefragment dat ingebouwde functies van javaScript gebruikt om u de logica achter de palindroomreeks uit te leggen:

De functie PTest () is gedefinieerd waarin we de tekenreeks verzenden die moet worden getest op palindroomfunctionaliteit. In het geval dat de string palindroom is, moeten we een tekst ontvangen in de uitvoer die hetzelfde bevestigt andersom. De functie wordt aan het einde genoemd na de functiedefinitie. Hier zijn reverse (), split (), join (), vervangen (), toLowerCase () ingebouwde functies.

  • Vervangen (): deze functie vervangt de speciale tekens en spaties uit de tekenreeks.
  • toLowerCase (): met deze functie wordt de hele tekenreeks in kleine letters weergegeven.
  • Splitsen (): de functie Splitsen splitst de tekenreeks in afzonderlijke tekens.
  • Reverse (): Reverse-functie zal de string omkeren die wordt uitgevoerd door de bovenstaande functie. Dit betekent dat de tekenreeks begint bij het laatste teken dat teken voor teken leest tot het eerste teken.
  • Join (): de Join-functie voegt de tekens samen die in omgekeerde volgorde werden uitgevoerd vanaf de bovenstaande functie.

Code:

Function PTest (TestString) (
var remSpecChar = TestString.replace(/(^A-Z0-9)/ig, "").toLowerCase(); /* this function removes any space, special character and then makes a string of lowercase */
var checkingPalindrome = remSpecChar.split('').reverse().join(''); /* this function reverses the remSpecChar string to compare it with original inputted string */
if(remSpecChar === checkingPalindrome)( /* Here we are checking if TestString is a Palindrome sring or not */
document.write(" "+ myString + " is a Palindrome string "); /* Here we write the string to output screen if it is a palindrome string */
)
else(
document.write(" " + myString + " is not a Palindrome string "); /* Here we write the string to output screen if it is not a palindrome string */
)
)
PTest('"Hello"') /* Here we are calling the above function with the test string passed as a parameter. This function's definition is provided before function calling itself so that it is available for the compiler before actual function call*/
PTest('"Palindrome"')
PTest('"7, 1, 7"') /* This is a Palindrome string */

De palindroomfunctie kan ook worden geschreven met behulp van lussen

In de onderstaande code wordt de for-lus gebruikt om de lus te doorlopen. Hierin wordt elke keer dat de lus het personage vanaf de voorkant heeft uitgevoerd, vergeleken met het personage aan de achterkant. Als ze overeenkomen, retourneert de functie Boolean true. Deze lus wordt uitgevoerd tot de helft van de lengte van de invoertekenreeks. Omdat wanneer we de voorste en achterste tekens van de tekenreeks vergelijken, we de volledige tekenreeks niet hoeven te doorlopen. Het vergelijken van de eerste helft met de laatste helft van de string geeft het resultaat. Dit maakt het programma ruimtebesparend en sneller.

Code:

function Findpalindrome(TestStr) (
var PlainStr= TestStr.replace(/(^0-9a-z)/gi, '').toLowerCase().split("");
for(var i=0; i < (PlainStr.length)/2; i++)(
if(PlainStr(i) == PlainStr(PlainStr.length-i-1))(
return true;
) else
return false;
)
) Findpalindrome("ta11at");

De uitvoer van dit programma geeft true als de invoertekenreeks van dit programma een palindroom is.

Voorbeeld om te controleren of de tekenreeks / het nummer Palindrome is

Hieronder staat de gedetailleerde code in javaScript in een HTML-formulier om af te drukken als de string een palindroom is of niet.

Code:



Palindrome using JS
input(
padding: 20px;
)

function TestFunction()
(
//Here we are getting the value from the textbox on browser
var InputStr = document.getElementById('tbox').value; //here we are calling checkingPalindrome() function and passing a string into it
var resStr = checkingPalindrome(InputStr);
alert('The input string "'+InputStr+'" is "'+resStr+'"'); // This will allow a //rectangular box to be popped up on the screen to display result
)
function checkingPalindrome(InputStr)
(
var origString;
//here we are converting the string into a lowercase string
InputStr = InputStr.toLowerCase();
//here we are storing the InputStr in origString for reference
origString= InputStr;
//here we are reversing the entered string
InputStr = InputStr.split(''); //this function will split the input string
InputStr = InputStr.reverse(); //this function will reverse the string
InputStr = InputStr.join(''); //this function will join the reversed string characters
var revString = InputStr;
//here we are checking if both the input string and the reversed string are same
//and based on it the string will be declared palindrome or not
if(origString == revString)(
return 'Palindrome string'; // this will return "Palindrome" if true //otherwise control will flow to else condition
)
else
(
return 'not a Palindrome string';
)
)


Javascript Program to find if the number is Palindrome or not:



Palindrome using JS
input(
padding: 20px;
)

function TestFunction()
(
//Here we are getting the value from the textbox on browser
var InputStr = document.getElementById('tbox').value; //here we are calling checkingPalindrome() function and passing a string into it
var resStr = checkingPalindrome(InputStr);
alert('The input string "'+InputStr+'" is "'+resStr+'"'); // This will allow a //rectangular box to be popped up on the screen to display result
)
function checkingPalindrome(InputStr)
(
var origString;
//here we are converting the string into a lowercase string
InputStr = InputStr.toLowerCase();
//here we are storing the InputStr in origString for reference
origString= InputStr;
//here we are reversing the entered string
InputStr = InputStr.split(''); //this function will split the input string
InputStr = InputStr.reverse(); //this function will reverse the string
InputStr = InputStr.join(''); //this function will join the reversed string characters
var revString = InputStr;
//here we are checking if both the input string and the reversed string are same
//and based on it the string will be declared palindrome or not
if(origString == revString)(
return 'Palindrome string'; // this will return "Palindrome" if true //otherwise control will flow to else condition
)
else
(
return 'not a Palindrome string';
)
)


Javascript Program to find if the number is Palindrome or not:



Palindrome using JS
input(
padding: 20px;
)

function TestFunction()
(
//Here we are getting the value from the textbox on browser
var InputStr = document.getElementById('tbox').value; //here we are calling checkingPalindrome() function and passing a string into it
var resStr = checkingPalindrome(InputStr);
alert('The input string "'+InputStr+'" is "'+resStr+'"'); // This will allow a //rectangular box to be popped up on the screen to display result
)
function checkingPalindrome(InputStr)
(
var origString;
//here we are converting the string into a lowercase string
InputStr = InputStr.toLowerCase();
//here we are storing the InputStr in origString for reference
origString= InputStr;
//here we are reversing the entered string
InputStr = InputStr.split(''); //this function will split the input string
InputStr = InputStr.reverse(); //this function will reverse the string
InputStr = InputStr.join(''); //this function will join the reversed string characters
var revString = InputStr;
//here we are checking if both the input string and the reversed string are same
//and based on it the string will be declared palindrome or not
if(origString == revString)(
return 'Palindrome string'; // this will return "Palindrome" if true //otherwise control will flow to else condition
)
else
(
return 'not a Palindrome string';
)
)


Javascript Program to find if the number is Palindrome or not:

Output:

Conclusie

Daarom is Palindrome een cruciaal concept dat wordt onderwezen aan kenniszoekers in alle programmeertalen. Of het nu C, PHP, C ++, Python, Java of een andere programmeertaal is, alle talen hebben de basisfuncties in hun standaardbibliotheek om palindrome te ondersteunen. In het geval als er geen functie is om te ondersteunen, kunnen we altijd lussen hebben zoals while, for of control-structuren zoals If, anders break-statements om deze functionaliteit te realiseren.

Aanbevolen artikelen

Dit is een gids voor Palindrome in JavaScript. Hier bespreken we de logische uitleg met een voorbeeld om te controleren of de string / nummer een palindroom is. U kunt ook de volgende artikelen bekijken voor meer informatie -

  1. JavaScript-wiskundige functies
  2. Reguliere uitdrukkingen in JavaScript
  3. JavaScript MVC Frameworks
  4. Samenvoegen Sorteren in JavaScript
  5. jQuery querySelector | Voorbeelden voor querySelector
  6. Lussen in VBScript met voorbeelden
  7. Reguliere uitdrukkingen in Java
  8. Voorbeelden van ingebouwde Python-functies