Úvod do Palindrómu v JavaScripte

Vo všeobecnosti je Palindrome slovo také, že keď čítame tento znak od znaku dopredu, presne sa zhoduje so slovom vytvoreným pri čítaní toho istého slova dozadu. Napríklad: „level“, „madam“ atď. Keď sa slovo „level“ zapisuje dozadu, potom aj výsledné slovo bude „level“. Tieto druhy slov, čísiel, reťazcov alebo sérií znakov, ak sú napísané akýmkoľvek počítačovým jazykom. Potom sa takáto funkcia nazýva palindróm. V palindróme jazyka programátora je séria znakov, čísiel, ktoré sa nemenia, aj keď sú napísané z opačného smeru a tvoria preusporiadané slovo. JavaScript poskytuje rôzne vstavané funkcie na realizáciu tejto funkcie. Môžeme mať tiež slučky, aby sme dosiahli rovnaký výsledok. V tomto článku sa pozrieme bližšie na palindróm v programovacom jazyku JavaScript na strane klienta.

Logické vysvetlenie palindrómu v JavaScripte

Nasleduje útržok kódu, ktorý vám pomocou vstavaných funkcií javaScript vysvetľuje logiku za reťazcom palindrómu:

Je definovaná funkcia PTest (), v ktorej pošleme reťazec, ktorý je potrebné otestovať na funkčnosť palindrómu. V prípade, že reťazec je palindróm, mali by sme dostať text na výstupe potvrdzujúci to isté inak naopak. Funkcia sa nazýva na konci po definícii funkcie. V tomto prípade sú vstavané funkcie reverse (), split (), join (), Repla (), toLowerCase ().

  • Replace (): Táto funkcia nahradí špeciálne znaky a medzery z reťazca.
  • toLowerCase (): Táto funkcia zníži veľkosť celého reťazca.
  • Split (): Funkcia Split rozdelí reťazec na jednotlivé znaky.
  • Reverse (): Funkcia Reverse obráti reťazec, ktorý je výstupom z vyššie uvedenej funkcie. To znamená, že reťazec sa začne od posledného znaku na čítanie znakov po znak až po prvý znak.
  • Join (): Funkcia Join spojí znaky, ktoré boli na výstupe z vyššie uvedenej funkcie.

kód:

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

Funkciu palindrómu je možné písať aj pomocou slučiek

V nižšie uvedenom kóde sa slučka for for používa na iteráciu slučkou. V tomto prípade sa zakaždým, keď slučka vykonala znak spredu, porovná znak so zadným koncom. Ak sa zhodujú, funkcia vráti booleovskú hodnotu true. Táto slučka bude pokračovať vo vykonávaní až do polovice dĺžky vstupného reťazca. Pretože keď porovnávame predné a zadné znaky reťazca, nemusíme opakovať celé reťazce. Porovnaním prvej polovice a poslednej polovice reťazca získate výsledok. Vďaka tomu je program priestorovo efektívny a rýchlejší.

kód:

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

Výstup tohto programu sa prejaví, ak je vstupný reťazec tohto programu palindróm.

Príklad na kontrolu, či je reťazec / číslo palindróm

Nižšie je uvedený podrobný kód vo formáte javaScript vo forme HTML, ktorý sa má vytlačiť, ak je reťazec palindróm alebo nie.

kód:



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:

Výkon:

záver

Palindróm je preto kľúčovým pojmom, ktorý sa učí záujemcov o znalosti vo všetkých programovacích jazykoch. Napríklad, C, PHP, C ++, Python, Java alebo akýkoľvek iný programovací jazyk, všetky jazyky majú vo svojej štandardnej knižnici základné funkcie na podporu palindrómu. V prípade, že neexistuje žiadna podporná funkcia, môžeme mať vždy slučky typu while, pre alebo kontrolné štruktúry ako If, inak prerušte príkazy, aby ste si túto funkciu uvedomili.

Odporúčané články

Toto je príručka pre Palindrome v JavaScripte. Tu diskutujeme logické vysvetlenie s príkladom, aby sme skontrolovali, či je reťazec / číslo palindróm. Ďalšie informácie nájdete aj v nasledujúcich článkoch -

  1. Matematické funkcie JavaScriptu
  2. Regulárne výrazy v JavaScripte
  3. JavaScript MVC Frameworks
  4. Zlúčiť Zoradiť v JavaScripte
  5. jQuery querySelector | Príklady dotazuSektor
  6. Slučky vo VBScript s príkladmi
  7. Regulárne výrazy v Jave
  8. Príklady zabudovaných funkcií Pythonu

Kategórie: