Here I have written a program that will find the first palindromic string in an array, like if the array is ["abs", "car", "ada", "racecar", "cool"], and I want output like "ada" so I have written a program that represents the palindromic logic representation, so here the output is not coming as expected. Can you please rectify and modify that program?
function isPalindrome(str) {
let left=0,right=str.length
while(left<right){
if(str.charAt(left)!=str.charAt(right)){
return false
}
left++;
right--;
}
return true
}
function firstPalindrome(words) {
for (let i=0;i<words.length;i++) {
if (isPalindrome(words[i])) {
return words[i];
}
}
return "";
}
console.log(firstPalindrome(["abc", "car", "ada", "racecar", "cool"]));
Here's a much simpler approach: