09 Palindromes: Replace Regex Solution (#319)
* Replaces the original regex method, with more familiar methods * Add descriptive comments for clarity
This commit is contained in:
parent
77c5d64b37
commit
9eb942909e
|
@ -1,11 +1,14 @@
|
||||||
const palindromes = function(string) {
|
// Non regex
|
||||||
const processedString = string.toLowerCase().replace(/[^a-z]/g, "");
|
const palindromes = function (string) {
|
||||||
return (
|
let alphabet = 'abcdefghijklmnopqrstuvwxyz'; //Create a variable that holds all the letters of the alphabet
|
||||||
processedString
|
const cleanedString = string // Convert to lowercase, split, & filter only letters, rejoin as new const
|
||||||
.split("")
|
.toLowerCase()
|
||||||
.reverse()
|
.split('')
|
||||||
.join("") == processedString
|
.filter((letter) => alphabet.includes(letter))
|
||||||
);
|
.join('');
|
||||||
|
const reversedString = cleanedString.split('').reverse().join(''); //Create a new const that holds reversed string
|
||||||
|
return cleanedString === reversedString; //Compare cleanedString & reversedString which returns true/false
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Do not edit below this line
|
||||||
module.exports = palindromes;
|
module.exports = palindromes;
|
||||||
|
|
Loading…
Reference in New Issue