Úvod do Palindrome v JavaScriptu

Obecně platí, že Palindrome je slovo, jako například to, že když čteme tento znak slova po znaku dopředu, přesně se shoduje se slovem vytvořeným při čtení stejného slova zezadu. Například: „level“, „madam“ atd. Zde, když se slovo „level“ zapisuje dozadu, bude také výsledným slovem „level“. Tyto druhy slov, čísel, řetězců nebo řad znaků, jsou-li psány jakýmkoli jazykem počítače. Taková funkce se pak nazývá palindrom. V palindromu programátora je řada znaků, čísla, která se nemění, i když jsou psána z obráceného směru a vytvářejí přeskupené slovo. JavaScript poskytuje různé vestavěné funkce k realizaci této funkce. Můžeme mít také smyčky, abychom dosáhli stejného výsledku. V tomto článku se budeme zabývat palindromem v programovacím jazyce JavaScript na straně klienta.

Logické vysvětlení palindromu v JavaScriptu

Níže je fragment kódu, který vám pomocí vestavěných funkcí javaScript vysvětluje logiku za řetězcem palindromu:

Je definována funkce PTest (), ve které pošleme řetězec, který musí být testován na funkčnost palindromu. V případě, že řetězec je palindrom, pak bychom měli obdržet text potvrzující stejný jinak opačně. Funkce je volána na konci po definici funkce. Zde jsou vestavěné funkce reverzní (), split (), join (), Repla (), toLowerCase ().

  • Replace (): Tato funkce nahradí speciální znaky a mezery z řetězce.
  • toLowerCase (): Tato funkce sníží malá písmena celého řetězce.
  • Split (): Funkce Split rozdělí řetězec na jednotlivé znaky.
  • Reverse (): Funkce Reverse obrátí řetězec, který je na výstupu z výše uvedené funkce. To znamená, že řetězec bude začínat od posledního znaku čtení znak po znaku až po první znak.
  • Join (): Funkce Join spojí znaky, které byly na výstupu z výše uvedené funkce.

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

Palindromovou funkci lze také napsat pomocí smyček

V níže uvedeném kódu se smyčka for používá k iteraci smyčkou. V tomto případě se pokaždé, když smyčka provedla znak zepředu, porovná se znakem na zadním konci. Pokud se shodují, funkce vrátí booleovskou hodnotu true. Tato smyčka bude pokračovat v provádění až do poloviny délky vstupního řetězce. Protože když porovnáme přední a zadní znaky řetězce, nemusíme iterovat přes celý řetězec. Porovnáním první poloviny s poslední polovinou řetězce bude výsledek. Díky tomu je program prostorově efektivní a rychlejší.

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 tohoto programu bude platný, pokud je vstupní řetězec tohoto programu palindrom.

Příklad pro kontrolu, zda řetězec / číslo je palindrom

Níže je podrobný kód ve formátu javaScript v podobě HTML, který se má vytisknout, pokud je řetězec palindrom nebo ne.

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ýstup:

Závěr

Palindrom je tedy klíčovým pojmem, který se učí uchazečům o znalosti ve všech programovacích jazycích. Ať už je to například C, PHP, C ++, Python, Java nebo jakýkoli jiný programovací jazyk, všechny jazyky mají ve své standardní knihovně základní funkce pro podporu palindromu. V případě, že neexistuje žádná podporovaná funkce, můžeme mít vždy smyčky typu while, pro nebo kontrolní struktury jako If, else, break příkazy k realizaci této funkce.

Doporučené články

Toto je průvodce po Palindrome v JavaScriptu. Zde diskutujeme logické vysvětlení s příkladem, abychom zjistili, zda je řetězec / číslo palindrom. Další informace naleznete také v následujících článcích -

  1. Matematické funkce JavaScriptu
  2. Regulární výrazy v JavaScriptu
  3. JavaScript MVC Frameworks
  4. Sloučit Seřadit v JavaScriptu
  5. jQuery querySelector | Příklady dotazuSektor
  6. Smyčky ve VBScript s příklady
  7. Regulární výrazy v Javě
  8. Příklady vestavěných funkcí Pythonu

Kategorie: