Find last index of a character in a string | Returns last index of x if it is present Else returns - 1. ; String in which char is to be found ; char whose index is to be found
function findLastIndex ( str , x ) { let index = - 1 ; for ( let i = 0 ; i < str . length ; i ++ ) if ( str [ i ] == x ) index = i ; return index ; } let str = " " ; let x = ' ' ; let index = findLastIndex ( str , x ) ; if ( index == - 1 ) document . write ( " " ) ; else document . write ( " " + index ) ;