const str = 'scasdcdddsssdsdsdd'; let res = ''; const lengestPalindrome = function(s){ let len = s.length-1; if(len < 2){ return s; } for(let i = 0; i <= len; i++){ helpFn(i,i); helpFn(i,i+1) } function helpFn(m,n){ while(m >= 0&&m <= len&&s[m] == s[n]){ m--; n++; } if(n - m - 1 > res.length){ res = s.slice(m+1,n) } } return res; } console.log(lengestPalindrome(str))