Solve the Logical Expression given by string | Function to evaluate the logical expression ; traversing string from the end . ; for NOT operation ; for AND and OR operation ; Driver code
function logicalExpressionEvaluation ( str ) { let arr = [ ] ; for ( let i = str . length - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == ' ' ) { let s = [ ] ; while ( arr [ arr . length - 1 ] != ' ' ) { s . push ( arr [ arr . length - 1 ] ) ; arr . pop ( ) ; } arr . pop ( ) ; if ( s . length == 3 ) { arr . push ( s [ 2 ] == ' ' ? ' ' : ' ' ) ; } else if ( s . length == 5 ) { let a = s [ 0 ] . charCodeAt ( 0 ) - 48 , b = s [ 4 ] . charCodeAt ( 0 ) - 48 , c ; if ( s [ 2 ] == ' ' ) { c = a & b ; } else { c = a | b ; } arr . push ( String . fromCharCode ( c + 48 ) ) ; } } else { arr . push ( str [ i ] ) ; } } return arr [ arr . length - 1 ] ; } let str = " " ; document . write ( logicalExpressionEvaluation ( str ) ) ;