MY #100 DAYS OF CODE CHALLENGE JOURNEY-DAY 4

in #coding6 years ago

IMG_20190125_193037.jpg

Problem: Checking if a word is a palindrome.

Problem Definition: A palindrome is a word which when spelled from left to right is the same if it is spelt from right to left. This kind of word is referred to as Palindrome.

Algorithm

  1. I converted the given string to lowercase and replace all non word characters with a white space.

  2. I then splitted the converted string, arrange it from last to first and then joined the characters together.

  3. I compared the string in step 2 and 1 above then, returned true if both strings are the same.

JavaScript Code

function palindrome(str){

let palindrome = str.toLowerCase().replace(/[^a-z0-9]/, " ");

if(palindrome === palindrome.split(" ").reverse(" ").join(" ") ) {
return true;}

else{return false}

};

palindrome("eye");