check palindrome js
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 checkPalindrome(inputString) {
return inputString == inputString.split('').reverse().join('');
}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
- convert jquery to string
- Category
- JavaScript
- Title
- grepper not showing
- Category
- JavaScript
- Title
- chart.js label word wrap
- Category
- JavaScript
- Title
- google apps script lock service
- Category
- JavaScript
- Title
- addeventlistener on document in react js
- Category
- JavaScript
- Title
- Elements in iteration expect to have 'v-bind:key' directives
- Category
- JavaScript
- Title
- array unique values javascript
- Category
- JavaScript
- Title
- check row empty array javascript
- Category
- JavaScript
- Title
- capitalize first letter javascript
- Category
- JavaScript
- Title
- dropzone add download button addedfile
- Category
- JavaScript
- Title
- dom ready js
- Category
- JavaScript
- Title
- enter event in jquery
- Category
- JavaScript
- Title
- insert into specific array index that has a value existing on that index
- Category
- JavaScript
- Title
- how to use for of in javascript
- Category
- JavaScript
- Title
- get last item in array js
- Category
- JavaScript
- Title
- disable input angular
- Category
- JavaScript
- Title
- javascript Check if an element is a descendant of another
- Category
- JavaScript
- Title
- how to use the foreach method in javascript
- Category
- JavaScript
- Title
- angular routing url params
- Category
- JavaScript
- Title
- angular list contains property
- Category
- JavaScript
- Title
- bootstrap 4 hide modal via javascript
- Category
- JavaScript
- Title
- datatable on error.dt
- Category
- JavaScript
- Title
- butonc click jquery
- Category
- JavaScript
- Title
- get value javascript
- Category
- JavaScript
- Title
- count occurrences of character in string javascript
- Category
- JavaScript
- Title
- how to get an absolute in js
- Category
- JavaScript
- Title
- get the index of object in array
- Category
- JavaScript
- Title
- for(let [key,val] in obj){ messageBody = messageBody.replace("{"+ key + "}",val) }
- Category
- JavaScript
- Title
- how to pass custom regex in jquery validation
- Category
- JavaScript
- Title
- how to make a inventory in js
- Category
- JavaScript
- Title
- async await react stackoverflow
- Category
- JavaScript
- Title
- convert boolean to string javascript
- Category
- JavaScript
- Title
- component navigation without changin the url react router
- Category
- JavaScript
- Title
- how to send an alert in javascript
- Category
- JavaScript
- Title
- google script get name of google form
- Category
- JavaScript
- Title
- ionic react use yarn
- Category
- JavaScript
- Title
- An unhandled exception occurred: Script file ../node_modules/jquery/dist/jquery.min.js does not exist.
- Category
- JavaScript
- Title
- clear input field value javascript
- Category
- JavaScript
- Title
- import reactdom
- Category
- JavaScript
- Title
- angular lifecycle hooks
- Category
- JavaScript
- Title
- how to delete an element from an array in javascript
- Category
- JavaScript
- Title
- ERROR in ./node_modules/tns-core-modules/ui/core/view/view.js Module not found: Error: Can't resolve '@nativescript/core/ui/core/view/view' in '/home/deepali/projects/supersquad-client/node_modules/tns-core-modules/ui/core/view'
- Category
- JavaScript
- Title
- how to parse header in node.js lambda
- Category
- JavaScript
- Title
- how to set JSON data bootstrap treeview with java
- Category
- JavaScript
- Title
- $ is not defined in jquery error
- Category
- JavaScript
- Title
- change src of iframe jquery
- Category
- JavaScript
- Title
- add elements to an array with splice
- Category
- JavaScript
- Title
- create many items from an array to canvas
- Category
- JavaScript
- Title
- in express how do you set the location header
- Category
- JavaScript
- Title
- date picker javascript not working
- Category
- JavaScript
- Title
- basic javascript
- Category
- JavaScript
- Title
- angular event scroll end of page
- Category
- JavaScript
- Title
- javascript 1 + "1"
- Category
- JavaScript
- Title
- how to convert array to uppercase in javascript
- Category
- JavaScript
- Title
- how to learn javascript
- Category
- JavaScript
- Title
- how to move div using jquery
- Category
- JavaScript
- Title
- how to use hooks react
- Category
- JavaScript
- Title
- how to verify timestamp format in javascript
- Category
- JavaScript
- Title
- how to transpose json data
- Category
- JavaScript
- Title
- How can I refresh a page with jQuery
- Category
- JavaScript
- Title
- if (req.url === "/script.js")
- Category
- JavaScript
- Title
- capturar el valor de un input con jquery
- Category
- JavaScript
- Title
- () = javascript
- Category
- JavaScript
- Title
- Check ratelimit discord js
- Category
- JavaScript
- Title
- fetch post js
- Category
- JavaScript
- Title
- * http://sizzlejs.com/
- Category
- JavaScript
- Title
- column.footer jquery
- Category
- JavaScript
- Title
- date picker type react
- Category
- JavaScript
- Title
- angular router navigate
- Category
- JavaScript
- Title
- how to square a value in javascript
- Category
- JavaScript
- Title
- javascript basic math
- Category
- JavaScript
- Title
- express and node
- Category
- JavaScript
- Title
- Javascript compare two objects
- Category
- JavaScript
- Title
- how to change image source using javascript
- Category
- JavaScript
- Title
- clone an object javascript
- Category
- JavaScript
- Title
- file input disable open file picker javascript
- Category
- JavaScript
- Title
- click on a radio button using jquery
- Category
- JavaScript
- Title
- fs create or edit file
- Category
- JavaScript
- Title
- angularjs left xx characters
- Category
- JavaScript
- Title
- .includes javascript
- Category
- JavaScript
- Title
- elseif javascript
- Category
- JavaScript
- Title
- get random numbers javascript
- Category
- JavaScript
- Title
- bracket notation javascript
- Category
- JavaScript
- Title
- window location in react
- Category
- JavaScript
- Title
- how to draw a triangle using javascript
- Category
- JavaScript
- Title
- Javascript compare two arrays
- Category
- JavaScript
- Title
- find the last occurrence of a character in a string javascript
- Category
- JavaScript
- Title
- destructuring arrays with rest operator
- Category
- JavaScript
- Title
- clear cookies js
- Category
- JavaScript
- Title
- functional component react
- Category
- JavaScript
- Title
- ajax laravel get data
- Category
- JavaScript
- Title
- javascript check if array is in array
- Category
- JavaScript
- Title
- AJAX in reload a div container
- Category
- JavaScript
- Title
- how to use json stringify in javascript
- Category
- JavaScript
- Title
- get attribute value jquery
- Category
- JavaScript
- Title
- AWS SDK for javascript assumerole with proxy
- Category
- JavaScript
- Title
- how to make a calculator websiteJS
- Category
- JavaScript
- Title
- angular.min.js version
- Category
- JavaScript
- Title
- get text selected option jquery
- Category
- JavaScript
- Title
- excel javascript api protect worksheet
- Category
- JavaScript