How do you detect whether or not a word is a palindrome in javascript

function palindrome(str) {
 // Step 1. The first part is the same as earlier
 var re = /[^A-Za-z0-9]/g; // or var re = /[\W_]/g;
 str = str.toLowerCase().replace(re, '');

 // Step 2. Create the FOR loop
 var len = str.length; // var len = "A man, a plan, a canal. Panama".length = 30
 
 for (var i = 0; i < len/2; i++) {
   if (str[i] !== str[len - 1 - i]) { // As long as the characters from each part match, the FOR loop will go on
       return false; // When the characters don't match anymore, false is returned and we exit the FOR loop
   }
   /* Here len/2 = 15
      For each iteration: i = ?    i < len/2    i++    if(str[i] !== str[len - 1 - i])?
      1st iteration:        0        yes         1     if(str[0] !== str[15 - 1 - 0])? => if("a"  !==  "a")? // false
      2nd iteration:        1        yes         2     if(str[1] !== str[15 - 1 - 1])? => if("m"  !==  "m")? // false      
      3rd iteration:        2        yes         3     if(str[2] !== str[15 - 1 - 2])? => if("a"  !==  "a")? // false  
      4th iteration:        3        yes         4     if(str[3] !== str[15 - 1 - 3])? => if("n"  !==  "n")? // false  
      5th iteration:        4        yes         5     if(str[4] !== str[15 - 1 - 4])? => if("a"  !==  "a")? // false
      6th iteration:        5        yes         6     if(str[5] !== str[15 - 1 - 5])? => if("p"  !==  "p")? // false
      7th iteration:        6        yes         7     if(str[6] !== str[15 - 1 - 6])? => if("l"  !==  "l")? // false
      8th iteration:        7        yes         8     if(str[7] !== str[15 - 1 - 7])? => if("a"  !==  "a")? // false
      9th iteration:        8        yes         9     if(str[8] !== str[15 - 1 - 8])? => if("n"  !==  "n")? // false
     10th iteration:        9        yes        10     if(str[9] !== str[15 - 1 - 9])? => if("a"  !==  "a")? // false
     11th iteration:       10        yes        11    if(str[10] !== str[15 - 1 - 10])? => if("c" !==  "c")? // false
     12th iteration:       11        yes        12    if(str[11] !== str[15 - 1 - 11])? => if("a" !==  "a")? // false
     13th iteration:       12        yes        13    if(str[12] !== str[15 - 1 - 12])? => if("n" !==  "n")? // false
     14th iteration:       13        yes        14    if(str[13] !== str[15 - 1 - 13])? => if("a" !==  "a")? // false
     15th iteration:       14        yes        15    if(str[14] !== str[15 - 1 - 14])? => if("l" !==  "l")? // false
     16th iteration:       15        no               
    End of the FOR Loop*/
 }
 return true; // Both parts are strictly equal, it returns true => The string is a palindrome
}

palindrome("A man, a plan, a canal. Panama");

5
1

                                    function palindrome(str) {
  // Step 1. Lowercase the string and use the RegExp to remove unwanted characters from it
  var re = /[\W_]/g; // or var re = /[^A-Za-z0-9]/g;
  
  var lowRegStr = str.toLowerCase().replace(re, '');
  // str.toLowerCase() = &quot;A man, a plan, a canal. Panama&quot;.toLowerCase() = &quot;a man, a plan, a canal. panama&quot;
  // str.replace(/[\W_]/g, '') = &quot;a man, a plan, a canal. panama&quot;.replace(/[\W_]/g, '') = &quot;amanaplanacanalpanama&quot;
  // var lowRegStr = &quot;amanaplanacanalpanama&quot;;
     
  // Step 2. Use the same chaining methods with built-in functions from the previous article 'Three Ways to Reverse a String in JavaScript'
  var reverseStr = lowRegStr.split('').reverse().join(''); 
  // lowRegStr.split('') = &quot;amanaplanacanalpanama&quot;.split('') = [&quot;a&quot;, &quot;m&quot;, &quot;a&quot;, &quot;n&quot;, &quot;a&quot;, &quot;p&quot;, &quot;l&quot;, &quot;a&quot;, &quot;n&quot;, &quot;a&quot;, &quot;c&quot;, &quot;a&quot;, &quot;n&quot;, &quot;a&quot;, &quot;l&quot;, &quot;p&quot;, &quot;a&quot;, &quot;n&quot;, &quot;a&quot;, &quot;m&quot;, &quot;a&quot;]
  // [&quot;a&quot;, &quot;m&quot;, &quot;a&quot;, &quot;n&quot;, &quot;a&quot;, &quot;p&quot;, &quot;l&quot;, &quot;a&quot;, &quot;n&quot;, &quot;a&quot;, &quot;c&quot;, &quot;a&quot;, &quot;n&quot;, &quot;a&quot;, &quot;l&quot;, &quot;p&quot;, &quot;a&quot;, &quot;n&quot;, &quot;a&quot;, &quot;m&quot;, &quot;a&quot;].reverse() = [&quot;a&quot;, &quot;m&quot;, &quot;a&quot;, &quot;n&quot;, &quot;a&quot;, &quot;p&quot;, &quot;l&quot;, &quot;a&quot;, &quot;n&quot;, &quot;a&quot;, &quot;c&quot;, &quot;a&quot;, &quot;n&quot;, &quot;a&quot;, &quot;l&quot;, &quot;p&quot;, &quot;a&quot;, &quot;n&quot;, &quot;a&quot;, &quot;m&quot;, &quot;a&quot;]
  // [&quot;a&quot;, &quot;m&quot;, &quot;a&quot;, &quot;n&quot;, &quot;a&quot;, &quot;p&quot;, &quot;l&quot;, &quot;a&quot;, &quot;n&quot;, &quot;a&quot;, &quot;c&quot;, &quot;a&quot;, &quot;n&quot;, &quot;a&quot;, &quot;l&quot;, &quot;p&quot;, &quot;a&quot;, &quot;n&quot;, &quot;a&quot;, &quot;m&quot;, &quot;a&quot;].join('') = &quot;amanaplanacanalpanama&quot;
  // So, &quot;amanaplanacanalpanama&quot;.split('').reverse().join('') = &quot;amanaplanacanalpanama&quot;;
  // And, var reverseStr = &quot;amanaplanacanalpanama&quot;;
   
  // Step 3. Check if reverseStr is strictly equals to lowRegStr and return a Boolean
  return reverseStr === lowRegStr; // &quot;amanaplanacanalpanama&quot; === &quot;amanaplanacanalpanama&quot;? =&gt; true
}
 
palindrome(&quot;A man, a plan, a canal. Panama&quot;);

5 (1 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
How do you detect whether or not a word is a palindrome in javascript how to check if a string is a palindrome javascript see if the word is a palindrome js word is palindrome in javascript find palindrome word from below string array in javascript javascript Implement a method which receives a string and checks whether it is palindrome on not. js functrion word palindrome check if the word is palidrome in javascript Write a program print all the palindrome words in a string using javascript extract palindrome word in a sentence using javascript how to extract palindrome word in an sentence using javascript Write a program to find all the palindrome words in a string using javascript 3. Write a program to find whether a string is palindrome or not. in javascript check to see if a word is a palindrome js writing a function to check palindrome in javascript function to check if string is palindrome javascript 7. Write a function which will check if a given string is palindrome or not javascript check if a string is palidrome or not in javascript given word is palindromw or not in javascript how to check if word is palindrome in javascript recursion A palindrome is a word, phrase, or number that is spelled the same forward and backward. javascript Program to find occurrences of palindrome words in string using javascript count palindrome words in a sentence using javascript how to find palindrome word in a sentence using javascript check if sentence is palindrome or not in js check if a word is a palindrome javascript how to check something is palindrome word in js entered string is palindrome or not. in javascript palindrome word in javascript check a string is palindrome or not js function palindrome(word) javascript Create a recursive function that determines whether a word is a palindrome or not. javascript is word a palindrome javascript check if a string is palindrome in javascript without using string functions string is palindrome or not in javascript check if words are palindrome js how to check if a string is a palindrom js write a function that checks if a given word is a palindrome javascript javascript recursive check if word is palindrome how to check given string is palin drome or not in js check if word is palindrome javascript js check if a string can be a palindrome javascript If a sentence has the letters mirrored but has different spaces, then it's not a pure palindrome function to check if a substring contains a palindrome in javascript Palindrome check in jquery function checkPalindrome(inputString) checking palindrome with map a function that determines whether or not a given string is a palindrome js palindrome checker in one line code javascript js checck if string is palindrome JavaScript Write a function called isPalindrome that accepts a single string argument, then returns true or false depending upon whether or not the string is a palindrome. ispalindrome js for loop javascript how to check if its a palindrome how to check palindrome javascript check if a string is a palindrome javascript palindrome javascript question sentence how to find palindrome on string javascript palindrone javascript js check if string is pallindrom javascript application that check if word is palindrome javascript palindrome without regex is a phrase polindrome js palandrom in js checkPalindrome js function javascript get palindrome node js check for palindrome palindromes in javascript apa itu palindrome di js node js wap to find palindrome node js wap to find palindrome withaapp what is the difference between reverse and palindrome in js how to check for a palindrome in javascript write a javascript program to check palindrome words js palindrome checker js check if palindrome palindrome or string js js ispalindrome palindrome javascript true javascript palindrome question efficient palindrome algorithm javascript javascript check for a palindrome find palindrome in string javascript palindrom js find palindrome javascript palindrome with javascript write a function that tests whether a string is a palindrome javascript pseudocode palindrome js function javascript find palindrome map javascript find palindrome ispalindrome js using loop ispalindrome js how to check palindrome in javascript palindrome node js best way to check the string is palindrome or not in typescript Create a toPalindrome method that creates a palindrome out in javascript parlindrom in javacript javascript palindrome detector palindrome program in javascript javascript function that returns true if the given string is a palindrome. Otherwise palindrome solution javascript javascript check if a string is a palindrome how to ispalindrome javascript checking palindrome in javascript javascript is palindrome javascript ispalindrome palindrom in js javascript code palindrome js palindrome code javascript palindrome example string palindrome in javascript check for palindrome javascript js palindrome check js is palindrome javascript Check if String is a palindrome palindrome numbers true js check if string is palindrom javascritp palindrome checker in javascript how to check if a string is palindrome in javascript check for palindrome in javascript palindrom javascript palindrome function in javascript making a palindrome javascript tutorial Write a function checkPalindrome that accepts a single argument, a string. The function should return true (Boolean) if the string is a palindrome, false if it is not. Make sure your function will give the correct answer for words with capital letters. palindrom question javascript check if a string is palindrome in javascript javascript function pallindrome palindrom checker js is palindrome algorithm javascript every() explained palindrome javascript program javascript palindrome function breaking a palindrome javascript palindrome ins js javascript palindrome checker palindrome checker js palindrome checker javascript javascript palindrome algoritham Palindrome in javascript js find palindrome palndrome js palindrome check javascript how to find palindrome in javascript palindrome program in javascript without using reverse function palindrome program in javascript palindrome algorithm javascript palindrome code javascript ispalindrome javascript check palindrome string in javascript check if string is palindrome js check string is palindrome js string palindrome js js palindrome is valid javascript code that determines a palindrome word in a sentence javascrpit code that determines a palindrome word in a sentence how to check if not a palindrome javascript palindrome string javascript js palindrome words js palindrome while string files on server what should we do if the file names are not unique javascript if and or how to say or in c# if element not found do this action in selenium If we want define style for an unique element, then which css selector will we use ? what happens if you try and split a string at something that doesn't exist java what happens when you encrypt an empty string The file you asked for does not exist javascript how do we ensure if a port is open in linux pallindrome in javascript how to make a palindrome in javascript with out javascript methods given the string check if it is a palindrome javascript palindrome javascript dev return reversed palindrome phrase js is palindrome check in javascript learn palindrome code javascript how to check given string is palindrome or not in javascript javascript check for palidrome check palindrome in javascript palindrome examples in javascript palindrome word javascript palindrome in javascript without using string function check palindrome javascript check if string is palindrome javascript how to return every palindromes from a string js how to return palindromes out of a string js how to find palindromws js how to get a palindrome js js ispalindrome for loop javascript palindrome string palindrome function javascript javascript check if palindrome javascript palindrome palindrome javascript valid palindrome javascript palindrome js check palindrome js js function that takes in a single word as a string and returns true if it&rsquo;s a palindrome palindrome javascript function
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source