this is my first post.
I’m trying to write a palindrome checker using recursive (w3resource exercise) . The function pulls up first and last indexes of an array-transformed string if they match and reruns itself.
//checking if array is palindrome
function isPal(arr){
//array contains one or no char: this is palindrome
if(arr.length <= 1) return true;
//if first and last chars are the same
if(arr[0] == arr[arr.length - 1]){
//new array without these
let newArr = arr.slice(1, arr.length -1);
//rerun isPal with shortened arr
isPal(newArr)
}else{
return false;
}
}
isPal(strArray);
}
console.log(palindromeRec("A man, a plan, a canal - Panama"))
All works until array contains one or zero item, when function returns undefined instead of TRUE (base case).
Could someone help?
New contributor
gerard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1