palindrome recursion explained in ruby code example
Example 1: palindrome recursion explained in ruby
function palindrome(str) {//\W removes all non-alphanumeric characters, 'g' flag stands for global search var re = /[\W_]/g; var strLow = str.toLowerCase().replace(re, ''); var reverseStr = strLow.split('').reverse().join(''); if (strLow === reverseStr) { return true; } else { return false; }}palindrome("eye");
Example 2: palindrome index explained in ruby
# Complete the palindromeIndex function below. def palindromeIndex(s) # slen = s.length splt = s.split('') n = s.reverse nsplt = n.split('') if n == s return -1 elsif n != s splt.each do |p| if nsplt[p] != splt[p] return p end end else return -1 end end