Length Of Last Word in a String | js program for implementation of simple approach to find length of last word ; String a is ' final ' -- can not be modified So , create a copy and trim the spaces from both sides ; Driver code
function lengthOfLastWord ( a ) { let len = 0 ; x = a . trim ( ) ; for ( let i = 0 ; i < x . length ; i ++ ) { if ( x [ i ] == ' ' ) { len = 0 ; } else { len ++ ; } } return len ; } input = " " ; document . write ( " " + lengthOfLastWord ( input ) ) ;