Return previous element in an expanding matrix | Javascript program to return previous element in an expanding matrix ; Returns left of str in an expanding matrix of a , b , c and d . ; Start from rightmost position ; If the current character is b or d , change to a or c respectively and break the loop ; If the current character is a or c , change it to b or d respectively ; driver program to test above method
String . prototype . replaceAt = function ( index , replacement ) { return this . substr ( 0 , index ) + replacement + this . substr ( index + replacement . length ) ; } function findLeft ( str ) { let n = str . length ; while ( n > 0 ) { n -- ; if ( str [ n ] == ' ' ) { str = str . replaceAt ( n , ' ' ) ; break ; } if ( str [ n ] == ' ' ) { str = str . replaceAt ( n , ' ' ) ; break ; } if ( str [ n ] == ' ' ) str = str . replaceAt ( n , ' ' ) ; else if ( str [ n ] == ' ' ) str = str . replaceAt ( n , ' ' ) ; } return str ; } let str = " " ; document . write ( " " + str + " " + findLeft ( str ) ) ;