Print longest palindrome word in a sentence | Function to check if a word is palindrome ; making the check case case insensitive ; loop to check palindrome ; Function to find longest palindrome word ; to check last word for palindrome ; to store each word ; extracting each word ; Driver code
function checkPalin ( word ) { let n = word . length ; word = word . toLowerCase ( ) ; for ( let i = 0 ; i < n ; i ++ , n -- ) if ( word [ i ] != word [ n - 1 ] ) return false ; return true ; } function longestPalin ( str ) { str = str + " " ; let longestword = " " , word = " " ; let length , length1 = 0 ; for ( let i = 0 ; i < str . length ; i ++ ) { let ch = str [ i ] ; if ( ch != ' ' ) word = word + ch ; else { length = word . length ; if ( checkPalin ( word ) && length > length1 ) { length1 = length ; longestword = word ; } word = " " ; } } return longestword ; } let s = " " + " " ; if ( longestPalin ( s ) == " " ) document . write ( " " + " " ) ; else document . write ( longestPalin ( s ) ) ;