Uvod v Palindrome v JavaScript

V splošnem smislu je Palindrome beseda, kakršna je, da se, ko besedo beremo po besedi od besed naprej naprej, natančno ujema z besedo, ki je nastala, ko se ista beseda bere od nazaj. Na primer: "nivo", "gospa" itd. Tukaj, ko je beseda "nivo" napisana od nazaj, bo tudi končna beseda "raven". Te vrste besed, številk, vrstic ali nizov znakov, kadar jih piše kateri koli računalniški jezik. Potem se takšna funkcionalnost imenuje palindrom. V jeziku programerja je palindrom niz znakov, številk, ki se ne spremenijo, tudi če je napisano iz obratne smeri in tvori preurejeno besedo. JavaScript zagotavlja različne vgrajene funkcije za uresničitev te funkcionalnosti. Za enak rezultat lahko imamo tudi zanke. V tem članku bomo podrobneje raziskali palindrome v programskem jeziku na strani odjemalca.

Logična razlaga Palindroma v JavaScript

Spodaj je delček kode z uporabo vgrajenih funkcij javaScript, da vam razložim logiko nizov palindroma:

Definirana je funkcija PTest (), v kateri bomo poslali niz, ki ga je treba preizkusiti za funkcionalnost palindroma. V primeru, da je niz palindrom, bi morali dobiti besedilo v izhodu, ki potrjuje isto, drugače obratno. Po definiciji funkcije se funkcija pokliče na koncu. Tu so vgrajene funkcije reverse (), split (), join (), substituting (), toLowerCase ().

  • Zamenjaj (): Ta funkcija bo nadomestila posebne znake in presledke iz niza.
  • toLowerCase (): Ta funkcija bo z nizkim začetkom zapisala celoten niz.
  • Split (): Split funkcija bo niz razdelila na posamezne znake.
  • Reverse (): funkcija vzvratne vožnje obrne niz, ki je izhod iz zgornje funkcije. To pomeni, da se niz začne od zadnjega branja znaka do znaka do prvega.
  • Join (): Funkcija pridruži se pridruži znakom, ki so bili iz zgornje funkcije izvlečeni obratno.

Koda:

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

Funkcijo palindroma lahko napišete tudi z uporabo zank

V spodnji kodi je zanka za zanko uporabljena za ponavljanje skozi zanko. Pri tem se vsakič, ko je zanka izvedla lik, spredaj primerjamo z znakom na zadnjem koncu. Če se ujemata, bo funkcija vrnila Boolean true. Ta zanka se bo izvajala do polovice dolžine vhodnega niza. Ker če primerjamo sprednji in zadnji znak niza, nam ni treba iterati skozi celoten niz. Primerjava prvega polčasa z zadnjo polovico niza bo dala rezultat. Zaradi tega je program prostorsko učinkovit in hitrejši.

Koda:

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

Izhod tega programa bo resničen, če je vhodni niz tega programa palindrom.

Primer, da preverite, ali je niz / številka Palindrome

Spodaj je podrobna koda v javaScript v obliki HTML za tiskanje, če je niz palindrom ali ne.

Koda:



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:

Izhod:

Zaključek

Zato je Palindrome ključni koncept, ki ga iskalci znanja naučijo v vseh programskih jezikih. Naj bo to C, PHP, C ++, Python, Java ali kateri koli drug programski jezik, na primer vsi jeziki imajo osnovne funkcije v svoji standardni knjižnici za podporo palindromu. V primeru, da ni funkcije, ki bi jo lahko podpirala, imamo lahko vedno zanke, medtem ko, za ali upravljamo strukture, kot je If, ​​else, prekinite izjave, da uresničimo to funkcionalnost.

Priporočeni članki

To je navodila za Palindrome v JavaScript. Tukaj razpravljamo o logični razlagi s primerom, da preverimo, ali je niz / številka palindrom. Če želite izvedeti več, si oglejte tudi naslednje članke -

  1. JavaScript matematične funkcije
  2. Redni izrazi v JavaScript
  3. Okviri MVC JavaScript
  4. Spajanje Razvrsti v JavaScript
  5. jQuery querySelector | Primeri za querySelector
  6. Zanke v VBScript s primeri
  7. Redni izrazi na Javi
  8. Primeri vgrajenih funkcij Python-a