Palindroomi sissejuhatus JavaScripti

Üldises mõttes on Palindrome selline sõna, nagu kui me seda tähemärki tähemärgi järgi ette loeme, siis sobib see täpselt sõnaga, mis moodustub, kui sama sõna loetakse tagantpoolt. Näiteks: “tase”, “proua” jne. Kui sõna “tase” kirjutatakse tagantpoolt, siis moodustatakse ka lõppsõna “tase”. Selliseid sõnu, numbreid, stringi või tähemärgiseeriat, kui need on kirjutatud ükskõik millises arvutikeeles. Siis nimetatakse sellist funktsionaalsust palindroomiks. Programmeerija keeles on palindroom tähemärgiseeria, numbrid, mis ei muutu isegi siis, kui see kirjutatakse vastupidises suunas, moodustades ümberkorraldatud sõna. Selle funktsiooni realiseerimiseks pakub JavaScript mitmesuguseid sisseehitatud funktsioone. Sama tulemuse saamiseks võib meil olla ka silmuseid. Selles artiklis uurime lähemalt palindroomi kliendipoolses programmeerimiskeeles JavaScripti.

Palindroomi loogiline seletus JavaScripti

Allpool on koodilõik, mis kasutab JavaScripti sisseehitatud funktsioone, et selgitada teile palindroomi stringi taga olevat loogikat:

Funktsioon PTest () on määratletud, mille kaudu saadame stringi, mida tuleb palindroomi funktsioneerimiseks testida. Kui string on palindroomne, peaksime väljundina saama teksti, mis kinnitab sama, vastupidi. Funktsioon kutsutakse lõpus pärast funktsiooni määratlust. Siin on sisseehitatud funktsioonid tagurdamine (), poolitamine (), liitumine (), asendamine (), toLowerCase ().

  • Asenda (): see funktsioon asendab stringi erimärgid ja tühikud.
  • toLowerCase (): See funktsioon väiketähti väiketähtedena tervena.
  • Split (): poolitusfunktsioon jagab stringi üksikuteks märkideks.
  • Tagurpidi (): tagurdusfunktsioon pöörab ülaltoodud funktsioonist väljunud stringi ümber. See tähendab, et string algab viimasest tähemärgist, mis algab tähemärgi järgi, kuni esimese tähemärgini.
  • Liitu (): liitumisfunktsioon liitub märkidega, mis väljutati ülaltoodud funktsioonist vastupidisel viisil.

Kood:

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

Palindroomi funktsiooni saab kirjutada ka silmuste abil

Allolevas koodis kasutatakse silmuse iteratsiooniks for for. Selles võrreldakse iga kord, kui silmus on tähemärgi eestpoolt täide viinud, selle tagumises tähemärgis. Kui need vastavad, tagastab funktsioon tõeväärtuse tõese. See silmus jätkab täitmist kuni pooleni sisestusstringi pikkusest. Sest kui võrrelda stringi esi- ja tagumisi märke, ei pea me kogu stringi kaudu iteruma. Esimese poole võrdlus nööri viimase poolega annab tulemuse. See muudab programmi ruumi efektiivsemaks ja kiiremaks.

Kood:

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

Selle programmi väljund annab tõese väärtuse, kui selle programmi sisestusstring on palindroom.

Näide, kuidas kontrollida, kas string / number on palindroom

Allpool on HTML-vormingus javacripti üksikasjalik kood, mida printida, kui string on palindroom või mitte.

Kood:



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äljund:

Järeldus

Seega on Palindroom ülioluline mõiste, mida õpetatakse teadmiste otsijatele kõigis programmeerimiskeeltes. Olgu see siis C, PHP, C ++, Python, Java või mõni muu programmeerimiskeel, näiteks kõigil keeltel on palindroomi toetamiseks nende põhiteegis põhifunktsioonid. Juhul, kui puudub toetav funktsioon, võime selle funktsiooni realiseerimiseks alati luua silmuseid, näiteks for, või juhtimisstruktuure, näiteks If, muul viisil katkestada avaldused.

Soovitatavad artiklid

See on juhend Palindrome'i JavaScriptis. Siin käsitleme loogilist seletust näitega, et kontrollida, kas string / number on palindroom. Lisateabe saamiseks võite vaadata ka järgmisi artikleid -

  1. JavaScripti matemaatikafunktsioonid
  2. Regulaarsed avaldised JavaScriptis
  3. JavaScripti MVC raamistikud
  4. Ühenda Sorteeri JavaScriptis
  5. jQuery querySelector | Näited päringule Selector
  6. Silmused VBScriptis näidetega
  7. Regulaarsed avaldised Java-s
  8. Näited Pythoni sisseehitatud funktsioonidest

Kategooria: