Find last index of a character in a string | Returns last index of x if it is present . Else returns - 1. ; Traverse from right ; Driver code
function findLastIndex ( str , x ) { for ( let i = str . length - 1 ; i >= 0 ; i -- ) if ( str [ i ] == x ) return i ; return - 1 ; } let str = " " ; let x = ' ' ; let index = findLastIndex ( str , x ) ; if ( index == - 1 ) document . write ( " " ) ; else document . write ( " " + index ) ;