Wprowadzenie do Palindrome w JavaScript

W ogólnym sensie Palindrom jest słowem takim jak to, że kiedy czytamy to słowo znak po znaku od przodu, pasuje dokładnie do słowa utworzonego, gdy to samo słowo jest czytane od tyłu. Na przykład: „poziom”, „pani” itp. Tutaj, gdy słowo „poziom” jest zapisywane od tyłu, wówczas również utworzone słowo końcowe będzie „poziom”. Tego rodzaju słowa, cyfry, ciąg znaków lub seria znaków, gdy są napisane w dowolnym języku komputerowym. Następnie taka funkcjonalność nazywa się palindromem. W palindromie języka programisty jest seria znaków, liczb, które nie zmieniają się, nawet gdy są zapisywane z odwrotnego kierunku, tworząc ponownie ustawione słowo. JavaScript udostępnia różne wbudowane funkcje do realizacji tej funkcji. Możemy również mieć pętle, aby uzyskać ten sam wynik. W tym artykule zajmiemy się bardziej palindromem w języku programowania JavaScript po stronie klienta.

Logiczne wyjaśnienie palindromu w JavaScript

Poniżej znajduje się fragment kodu wykorzystujący wbudowane funkcje javaScript do wyjaśnienia logiki stojącej za ciągiem palindromu:

Zdefiniowana jest funkcja PTest (), w której wyślemy ciąg, który należy przetestować pod kątem działania palindromu. W przypadku, gdy ciąg jest palindromem, powinniśmy otrzymać tekst wyjściowy potwierdzający to samo, w przeciwnym razie odwrotnie. Funkcja jest wywoływana na końcu po definicji funkcji. Tutaj reverse (), split (), join (), replace (), toLowerCase () są wbudowanymi funkcjami.

  • Replace (): Ta funkcja zastąpi znaki specjalne i spacje z łańcucha.
  • toLowerCase (): Ta funkcja spowoduje zapisanie całego łańcucha małymi literami.
  • Split (): Funkcja Split podzieli ciąg na poszczególne znaki.
  • Reverse (): Funkcja Reverse odwróci ciąg, który jest wyprowadzany z powyższej funkcji. Oznacza to, że ciąg będzie zaczynać się od ostatniego znaku czytającego znak po znaku do pierwszego znaku.
  • Join (): Funkcja Join połączy znaki, które zostały wypisane odwrotnie z powyższej funkcji.

Kod:

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 */

Funkcję palindromu można również zapisać za pomocą pętli

W poniższym kodzie pętla for służy do iteracji w pętli. W tym przypadku za każdym razem, gdy pętla wykonuje znak, postać z przodu jest porównywana z postacią z tyłu. Jeśli są zgodne, funkcja zwróci wartość logiczną true. Ta pętla będzie wykonywana do połowy długości ciągu wejściowego. Ponieważ, gdy porównamy przednie i tylne znaki łańcucha, nie musimy iterować całego łańcucha. Porównanie pierwszej połowy z ostatnią połową łańcucha da wynik. Dzięki temu program zajmuje mało miejsca i jest szybszy.

Kod:

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");

Wyjście tego programu da wartość true, jeśli ciągiem wejściowym tego programu jest palindrom.

Przykład, aby sprawdzić, czy ciąg / liczba to palindrom

Poniżej znajduje się szczegółowy kod w javaScript w formularzu HTML do wydrukowania, jeśli ciąg jest palindromem, czy nie.

Kod:



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:

Wynik:

Wniosek

Stąd Palindrom jest kluczową koncepcją nauczaną dla osób poszukujących wiedzy we wszystkich językach programowania. Czy to na przykład C, PHP, C ++, Python, Java lub inny język programowania, wszystkie języki mają podstawowe funkcje w swojej standardowej bibliotece do obsługi palindromu. W przypadku, gdy nie ma żadnej funkcji do obsługi, zawsze możemy mieć pętle takie jak while, for lub struktury kontrolne, takie jak If, else, instrukcje break, aby zrealizować tę funkcjonalność.

Polecane artykuły

To jest przewodnik po Palindrome w JavaScript. Tutaj omawiamy logiczne wyjaśnienie na przykładzie, aby sprawdzić, czy ciąg / liczba jest palindromem. Możesz także przejrzeć następujące artykuły, aby dowiedzieć się więcej -

  1. Funkcje matematyczne JavaScript
  2. Wyrażenia regularne w JavaScript
  3. JavaScript MVC Frameworks
  4. Scal sortowanie w JavaScript
  5. jQuery querySelector | Przykłady dla querySelector
  6. Pętle w VBScript z przykładami
  7. Wyrażenia regularne w Javie
  8. Przykłady wbudowanych funkcji Pythona