How do you detect whether or not a word is a palindrome in javascript
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");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() = "A man, a plan, a canal. Panama".toLowerCase() = "a man, a plan, a canal. panama"
// str.replace(/[\W_]/g, '') = "a man, a plan, a canal. panama".replace(/[\W_]/g, '') = "amanaplanacanalpanama"
// var lowRegStr = "amanaplanacanalpanama";
// 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('') = "amanaplanacanalpanama".split('') = ["a", "m", "a", "n", "a", "p", "l", "a", "n", "a", "c", "a", "n", "a", "l", "p", "a", "n", "a", "m", "a"]
// ["a", "m", "a", "n", "a", "p", "l", "a", "n", "a", "c", "a", "n", "a", "l", "p", "a", "n", "a", "m", "a"].reverse() = ["a", "m", "a", "n", "a", "p", "l", "a", "n", "a", "c", "a", "n", "a", "l", "p", "a", "n", "a", "m", "a"]
// ["a", "m", "a", "n", "a", "p", "l", "a", "n", "a", "c", "a", "n", "a", "l", "p", "a", "n", "a", "m", "a"].join('') = "amanaplanacanalpanama"
// So, "amanaplanacanalpanama".split('').reverse().join('') = "amanaplanacanalpanama";
// And, var reverseStr = "amanaplanacanalpanama";
// Step 3. Check if reverseStr is strictly equals to lowRegStr and return a Boolean
return reverseStr === lowRegStr; // "amanaplanacanalpanama" === "amanaplanacanalpanama"? => true
}
palindrome("A man, a plan, a canal. Panama");
Also in JavaScript:
- Title
- bootstrap 4 form validator with jquery
- Category
- JavaScript
- Title
- comment in react
- Category
- JavaScript
- Title
- gitignore for angular
- Category
- JavaScript
- Title
- angular event scroll end of page
- Category
- JavaScript
- Title
- get data from json using jquery
- Category
- JavaScript
- Title
- expressjs query params
- Category
- JavaScript
- Title
- Heroku H10-App Crashed Error
- Category
- JavaScript
- Title
- how to find the index of a value in an array in javascript
- Category
- JavaScript
- Title
- Javascript - Track mouse position
- Category
- JavaScript
- Title
- detect a click outside an element javascript
- Category
- JavaScript
- Title
- how to remove attrivusre with jquery
- Category
- JavaScript
- Title
- 404 json laravel
- Category
- JavaScript
- Title
- google apps script lock service
- Category
- JavaScript
- Title
- add item to array javascript
- Category
- JavaScript
- Title
- arrow function in javascript w3schools
- Category
- JavaScript
- Title
- javascript button onclick
- Category
- JavaScript
- Title
- how to find id in array javascript
- Category
- JavaScript
- Title
- ad data to js object
- Category
- JavaScript
- Title
- how to use aos in react
- Category
- JavaScript
- Title
- discord.js
- Category
- JavaScript
- Title
- how to delete element in list javascript
- Category
- JavaScript
- Title
- how to generate random string in javascript
- Category
- JavaScript
- Title
- interaction js drag anddrop
- Category
- JavaScript
- Title
- javascript array stackoverflow
- Category
- JavaScript
- Title
- add val to array jquery
- Category
- JavaScript
- Title
- get page resolution jquery
- Category
- JavaScript
- Title
- how to access global scope in to local scope using javascript examples
- Category
- JavaScript
- Title
- electron jquery
- Category
- JavaScript
- Title
- How to rewrite a function using arrow function syntax
- Category
- JavaScript
- Title
- how to Write a program that simulates a coin toss using random method of Javascript Math class
- Category
- JavaScript
- Title
- how to get element by title js
- Category
- JavaScript
- Title
- fetch response json or text
- Category
- JavaScript
- Title
- daterangepicker set maxdate
- Category
- JavaScript
- Title
- include gif in react
- Category
- JavaScript
- Title
- google maps api javascript
- Category
- JavaScript
- Title
- crypto.com api
- Category
- JavaScript
- Title
- ES6: Use class Syntax to Define a Constructor Function
- Category
- JavaScript
- Title
- convert a int to a unicode javascript
- Category
- JavaScript
- Title
- enable disable click on div jquery
- Category
- JavaScript
- Title
- erela client userID
- Category
- JavaScript
- Title
- how to convert audio blocb to base64 string
- Category
- JavaScript
- Title
- check multiple ifield if it's blank in jquery
- Category
- JavaScript
- Title
- error duplicate resources react native
- Category
- JavaScript
- Title
- iframe reload parent page
- Category
- JavaScript
- Title
- Getting the differences between two objects javascript lib
- Category
- JavaScript
- Title
- file input disable open file picker javascript
- Category
- JavaScript
- Title
- get radio button value javascript
- Category
- JavaScript
- Title
- check element exist in jquery
- Category
- JavaScript
- Title
- ionic (Emitted value instead of an instance of Error
- Category
- JavaScript
- Title
- compile angularjs template
- Category
- JavaScript
- Title
- angular run with proxy
- Category
- JavaScript
- Title
- ERROR in ./src/app/pages/auth/auth.module.ngfactory.js Module not found: Error: Can't resolve '@angular/material/core/index'
- Category
- JavaScript
- Title
- getelementbyclass
- Category
- JavaScript
- Title
- javascript add commas to number
- Category
- JavaScript
- Title
- credit card mask js
- Category
- JavaScript
- Title
- download image in nodejs
- Category
- JavaScript
- Title
- javascript Check if an element is a descendant of another
- Category
- JavaScript
- Title
- array.filter in javascript
- Category
- JavaScript
- Title
- get id of clicked element javascript
- Category
- JavaScript
- Title
- generate new component angular
- Category
- JavaScript
- Title
- "npm supertest
- Category
- JavaScript
- Title
- how to make a property important in javascript
- Category
- JavaScript
- Title
- disregard empty field in array
- Category
- JavaScript
- Title
- how to check if browser is firefox in javascript
- Category
- JavaScript
- Title
- comment obtenir l'élément parent javascript
- Category
- JavaScript
- Title
- javascript Arranging Coins
- Category
- JavaScript
- Title
- default props react
- Category
- JavaScript
- Title
- install node modules
- Category
- JavaScript
- Title
- calling function from function object javascript
- Category
- JavaScript
- Title
- extend react.component
- Category
- JavaScript
- Title
- discord.js multiple embeds
- Category
- JavaScript
- Title
- adjust() js
- Category
- JavaScript
- Title
- adonisjs livereload
- Category
- JavaScript
- Title
- express json body
- Category
- JavaScript
- Title
- Is there an “exists” function for jQuery
- Category
- JavaScript
- Title
- how to displayan inteiger to a tenth in javascript
- Category
- JavaScript
- Title
- javascript class click event
- Category
- JavaScript
- Title
- how to send enter event to input field jquery
- Category
- JavaScript
- Title
- how to append select option in jquery
- Category
- JavaScript
- Title
- adonis js sort by
- Category
- JavaScript
- Title
- discord.js login code
- Category
- JavaScript
- Title
- create array with specific size javascript
- Category
- JavaScript
- Title
- how to use the map method in javascript
- Category
- JavaScript
- Title
- append data get array
- Category
- JavaScript
- Title
- document load javascript
- Category
- JavaScript
- Title
- how to change color of font in js
- Category
- JavaScript
- Title
- html table to excel javascript
- Category
- JavaScript
- Title
- javascript canvas gamma correction
- Category
- JavaScript
- Title
- how to add ' in javascript string
- Category
- JavaScript
- Title
- angular open poort in network
- Category
- JavaScript
- Title
- how to extract values from array in javascript
- Category
- JavaScript
- Title
- contains substring javascript
- Category
- JavaScript
- Title
- javascript 1 + "1"
- Category
- JavaScript
- Title
- for of loop syntax javascript
- Category
- JavaScript
- Title
- .shift js
- Category
- JavaScript
- Title
- how to make a show password button
- Category
- JavaScript
- Title
- hot get access_token instead of url
- Category
- JavaScript
- Title
- javascript array of objects
- Category
- JavaScript
- Title
- concantene number in js
- Category
- JavaScript
- Title
- javascript check null undefined empty
- Category
- JavaScript