Change the given string according to the given conditions | Function to proofread the spells ; Loop to iterate over the characters of the string ; Push the current character c in the stack ; Check for Rule 1 ; Check for Rule 2 ; To store the resultant string ; Loop to iterate over the characters of stack ; Return the resultant string ; Given string str ; Function Call
function proofreadSpell ( str ) { var result = [ ] ; str . split ( ' ' ) . forEach ( c => { result . push ( c ) ; var n = result . length ; if ( n >= 3 ) { if ( result [ n - 1 ] == result [ n - 2 ] && result [ n - 1 ] == result [ n - 3 ] ) { result . pop ( ) ; } } n = result . length ; if ( n >= 4 ) { if ( result [ n - 1 ] == result [ n - 2 ] && result [ n - 3 ] == result [ n - 4 ] ) { result . pop ( ) ; } } } ) ; var resultStr = " " ; result . forEach ( c => { resultStr += c ; } ) ; return resultStr ; } var str = " " ; document . write ( proofreadSpell ( str ) ) ;