import { IInputQuestionAnswerFull , TResultInputQuestion } from '../types' ; param userAnswer param answer returns export function checkInputAnswer ( userAnswer , answers ) { let isCorrect = false ; const transformedAnswers = JSON . parse ( JSON . stringify ( answers ) ) ; if ( userAnswer !== undefined ) { for ( let index = 0 ; index < answers . length ; index ++ ) { const [ modifiedUserAnswer , modifiedAnswerText ] = modifyAnswers ( userAnswer , answers [ index ] ) ; transformedAnswers [ index ] . isCorrect = false ; if ( modifiedUserAnswer === modifiedAnswerText ) { isCorrect = true ; transformedAnswers [ index ] . isCorrect = true ; break ; } else { const regex = answers [ index ] . regex ; if ( regex ) { const generatedRegex = new RegExp ( regex . regex , regex . flags ) ; isCorrect = Boolean ( userAnswer . match ( generatedRegex ) ) ; transformedAnswers [ index ] . isCorrect = isCorrect ; if ( isCorrect ) break ; } } } } return [ isCorrect , transformedAnswers ] as const ; } param userAnswer param answer export function modifyAnswers ( userAnswer , answer ) { let answerText = answer . text . trim ( ) ; userAnswer = userAnswer . trim ( ) ; answer . modifiers . forEach ( ( modifier ) => { switch ( modifier ) { case 'IC' : { userAnswer = userAnswer . toLowerCase ( ) ; answerText = answerText . toLowerCase ( ) ; break ; } case 'IS' : { userAnswer = userAnswer . replace ( /\s/g , '' ) ; answerText = answerText . replace ( /\s/g , '' ) ; break ; } } } ) ; return [ userAnswer , answerText ] as const ; } param userAnswers param answers returns export function checkInputAnswers ( userAnswers , answers ) { const transformedUserAnswers = JSON . parse ( JSON . stringify ( answers ) ) ; let isCorrect = false , totalCorrectAnswers = 0 ; for ( let index = 0 ; index < answers . length ; index ++ ) { const result = checkInputAnswer ( userAnswers [ index ] , answers [ index ] ) ; transformedUserAnswers [ index ] = result [ 1 ] ; isCorrect = result [ 0 ] ; if ( ! isCorrect ) break ; else totalCorrectAnswers ++ ; } return [ isCorrect , totalCorrectAnswers , transformedUserAnswers ] as const ; }