Find the direction from given string | Function to find the final direction ; if count is positive that implies resultant is clockwise direction ; if count is negative that implies resultant is anti - clockwise direction ; Driver code
function findDirection ( s ) { let count = 0 ; let d = " " ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( s [ 0 ] == ' ' ) return null ; if ( s [ i ] == ' ' ) count -- ; else { if ( s [ i ] == ' ' ) count ++ ; } } if ( count > 0 ) { if ( count % 4 == 0 ) d = " " ; else if ( count % 4 == 1 ) d = " " ; else if ( count % 4 == 2 ) d = " " ; else if ( count % 4 == 3 ) d = " " ; } if ( count < 0 ) { if ( count % 4 == 0 ) d = " " ; else if ( count % 4 == - 1 ) d = " " ; else if ( count % 4 == - 2 ) d = " " ; else if ( count % 4 == - 3 ) d = " " ; } return d ; } let s = " " ; document . write ( findDirection ( s ) + " " ) ; s = " " ; document . write ( findDirection ( s ) ) ;