|
1 | | -/** |
2 | | - * alphaNumericPlaindrome should return true if the string has alphanumeric characters that are palindrome irrespective of special characters and the letter case |
3 | | - * @param {string} str the string to check |
4 | | - * @returns `Boolean` |
5 | | - */ |
6 | | - |
7 | 1 | /***************************************************************************** |
8 | | - * What is a palindrome? https://en.wikipedia.org/wiki/Palindrome |
9 | | - * |
| 2 | + * @function alphaNumericPlaindrome |
| 3 | + * @description alphaNumericPlaindrome should return true if the string has alphanumeric characters that are palindrome irrespective of special characters and the letter case. |
| 4 | + * @param {string} str the string to check |
| 5 | + * @returns {Boolean} |
| 6 | + * @see [Factorial](https://en.wikipedia.org/wiki/Palindrome) |
| 7 | + * @example |
10 | 8 | * The function alphaNumericPlaindrome() receives a string with varying formats |
11 | 9 | * like "racecar", "RaceCar", and "race CAR" |
12 | 10 | * The string can also have special characters |
|
16 | 14 | * are palindrome i.e remove spaces, symbols, punctuations etc |
17 | 15 | * and the case of the characters doesn't matter |
18 | 16 | * |
19 | | - * This is one of the questions/projects that we have to solve for the |
20 | | - * JavaScript Algorithms and Data Structures course on https://www.freecodecamp.org |
21 | | - * |
22 | | - * Author -- Syed Fasiuddin |
23 | | - * https://github.com/SyedFasiuddin |
24 | | - * |
25 | 17 | ****************************************************************************/ |
26 | 18 |
|
27 | 19 | const alphaNumericPlaindrome = (str) => { |
28 | 20 | // removing all the special characters and turning everything to lowercase |
29 | 21 | const newStr = str.replace(/[^a-zA-Z0-9]*/g, '').toLowerCase() |
30 | | - // the newStr variable is a string and only has alphanumeric characters all in lowercase |
31 | | - |
32 | | - // making an array of individual characters as it's elements |
33 | | - const arr = newStr.split('') |
34 | | - |
35 | | - // setting a variable to see if change occurs to it |
36 | | - let palin = 0 |
37 | | - |
38 | | - // making a copy of arr with spread operator |
39 | | - const arrRev = [...arr] |
40 | | - // you can use arrRev.reverse() to reverse the array |
41 | | - // or else you can use the below method |
42 | 22 |
|
43 | | - // iterate through the arr and check the condition of palindrome |
44 | | - for (let i = 0; i < arr.length; i++) { |
45 | | - if (arr[i] !== arrRev[arr.length - 1 - i]) { |
46 | | - // if the string is not palindrome then we change palin variable to 1 |
47 | | - palin = 1 |
| 23 | + for (let i = 0; i < newStr.length; i++) { |
| 24 | + if (newStr[i] !== newStr[newStr.length - 1 - i]) { |
| 25 | + return false |
48 | 26 | } |
49 | 27 | } |
50 | 28 |
|
51 | | - // if the string is palindrome then palin variable is never changed |
52 | | - if (palin === 0) return true |
53 | | - else return false |
| 29 | + return true |
54 | 30 | } |
55 | 31 |
|
56 | 32 | export { alphaNumericPlaindrome } |
0 commit comments