text: Total time required to travel a path denoted by a given string | Function to calculate time taken to travel the path ; Stores total time ; Initial position ; Stores visited segments ; Check whether segment is present in the set ; Increment the value of time by 2 ; Insert segment into the set ; Print the value of time ; Driver Code
code: function calcTotalTime ( path ) { var time = 0 ; var x = 0 , y = 0 ; var s = new Set ( ) ; for ( var i = 0 ; i < path . length ; i ++ ) { var p = x ; var q = y ; if ( path [ i ] == ' ' ) y ++ ; else if ( path [ i ] == ' ' ) y -- ; else if ( path [ i ] == ' ' ) x ++ ; else if ( path [ i ] == ' ' ) x -- ; if ( ! s . has ( [ p + x , q + y ] . toString ( ) ) ) { time += 2 ; s . add ( [ p + x , q + y ] . toString ( ) ) ; } else time += 1 ; } document . write ( time ) } var path = " " ; calcTotalTime ( path ) ;