Update palindromes.js

This commit is contained in:
Hifilo 2023-01-17 13:02:49 -05:00 committed by GitHub
parent 8df33b0be3
commit 78c89e9fd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 21 deletions

View File

@ -1,28 +1,28 @@
//This is a method using Regular Expressions, and is probably the easiest if you know how to use them //Regex Method
// const palindromes = function (string) {
// const processedString = string.toLowerCase().replace(/[^a-z]/g, '');
// return processedString.split('').reverse().join('') == processedString;
// };
//there are many ways of checking the solution for palindromes without using regex. Here is one.
const palindromes = function (string) { const palindromes = function (string) {
let alphabet = 'abcdefghijklmnopqrstuvwxyz'; //create a variable that holds all the letters of the alphabet const processedString = string.toLowerCase().replace(/[^a-z]/g, '');
return processedString.split('').reverse().join('') == processedString;
//Take the input string, convert to lowercase, split, reverse, & filter only letters, before rejoining them together as the constant cleanedString.
const cleanedString = string
.toLowerCase()
.split('')
.filter((letter) => alphabet.includes(letter))
.join('');
//make a new const from the cleaned string, and reverse it.
const reversedString = cleanedString.split('').reverse().join('');
//we then finally compare cleanedString & reversedString which returns true/false
return cleanedString === reversedString;
}; };
//Method without regex
// const palindromes = function (string) {
// let alphabet = 'abcdefghijklmnopqrstuvwxyz'; //create a variable that holds all the letters of the alphabet
// //Take the input string, convert to lowercase, split, reverse, & filter only letters, before rejoining them together as the constant cleanedString.
// const cleanedString = string
// .toLowerCase()
// .split('')
// .filter((letter) => alphabet.includes(letter))
// .join('');
// //make a new const from the cleaned string, and reverse it.
// const reversedString = cleanedString.split('').reverse().join('');
// //we then finally compare cleanedString & reversedString which returns true/false
// return cleanedString === reversedString;
// };
//check with console.log statements below in your editor/ //check with console.log statements below in your editor/
// console.log(palindromes('A car, a man, a maraca.')); // console.log(palindromes('A car, a man, a maraca.'));