Position of robot after given movements | function to find final position of robot after the complete movement ; traverse the instruction string ' move ' ; for each movement increment its respective counter ; required final position of robot ; Driver code
function finalPosition ( move ) { let l = move . length ; let countUp = 0 , countDown = 0 ; let countLeft = 0 , countRight = 0 ; for ( let i = 0 ; i < l ; i ++ ) { if ( move [ i ] == ' ' ) countUp ++ ; else if ( move [ i ] == ' ' ) countDown ++ ; else if ( move [ i ] == ' ' ) countLeft ++ ; else if ( move [ i ] == ' ' ) countRight ++ ; } document . write ( " " + ( countRight - countLeft ) + " " + ( countUp - countDown ) + " " ) ; } let move = " " ; finalPosition ( move ) ;