MCPcopy Create free account
hub / github.com/TheAlgorithms/JavaScript / alphaNumericPalindrome

Function alphaNumericPalindrome

String/AlphaNumericPalindrome.js:17–34  ·  view source on GitHub ↗
(str)

Source from the content-addressed store, hash-verified

15 * and the case of the characters doesn't matter
16 */
17const alphaNumericPalindrome = (str) => {
18 if (typeof str !== 'string') {
19 throw new TypeError('Argument should be string')
20 }
21
22 // removing all the special characters and turning everything to lowercase
23 const newStr = str.replace(/[^a-z0-9]+/gi, '').toLowerCase()
24 const midIndex = newStr.length >> 1 // x >> y = floor(x / 2^y)
25
26 for (let i = 0; i < midIndex; i++) {
27 if (newStr.at(i) !== newStr.at(~i)) {
28 // ~n = -(n + 1)
29 return false
30 }
31 }
32
33 return true
34}
35
36export default alphaNumericPalindrome

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected