Okay here's the second post to my guide to understanding the basics of Data Structures and Algorithms with JavaScript series by solving common challenges. On this one, we look at: The infamous Palindrome Challenge.
Question:
Find out if the given string is a palindrome. A palindrome is a string or word that can be read the same in normal and reverse. For example "Madam I'm Adam", "Race Car".
Let's Tackle
isPalindrome
that takes in a string as an argument and returns true if the string is a Palindrome and false if it's not.function isPalindrome(string){
}
function isPalindrome(string){
string = string.toLowerCase()
}
function isPalindrome(string){
string = string.toLowerCase()
let validCharacters = 'abcdefghijklmnopqrstuvwxyz'.split('')
}
function isPalindrome(string){
string = string.toLowerCase()
charactersArray = string.split('')
let validCharacters = 'abcdefghijklmnopqrstuvwxyz'.split('')
let lettersArr = []
charactersArray.forEach(char => {
if(validCharacters.indexOf(char) > -1) lettersArr.push(char)
})
}
lettersArr
into a string and compare it to its reverse and return true if they match or false if they don't.function isPalindrome(string){
string = string.toLowerCase()
charactersArray = string.split('')
let validCharacters = 'abcdefghijklmnopqrstuvwxyz'.split('')
let lettersArr = []
charactersArray.forEach(char => {
if(validCharacters.indexOf(char) > -1) lettersArr.push(char)
})
if(lettersArr.join('') === lettersArr.reverse().join('')) return true;
else return false;
}
And, there we have it. Simple isn't it!
See you at the next one!
Happy Hacking!!
Top comments (0)