This is actually the same exercise we have done in Module 10. In the original exercise we used a lot of behavior subjects for information that was derived from other subjects. And we used the service, to do the synchronization (whenever we pushed a value to one of the atomic subjects, we also updated the derived ones). This is bad architecture.
In this exercise you will fix the application by replacing the derived behavior subjects with observables that will be created using operators. So they will be automatically updated.
- Using Observables
- Using RxJS operators
- The
_currentQuestion$
and_previousAnswer
subjects are the atomic ones, and they should remain. But we do want to ranme them internally:_questions$
and_answers$
. - Now the implementation of
answerCurrentQuestion
andcreateNewQuestion
are simple:answerCurrentQuestion
only pushes a new answer to the_answers$
subject and then callcreateNewQuestion
createNewQuestion
only pushes a new question to the_questions$
subject
- All the rest of the observables should be derived using operators:
- Implement the
currentQuestion$
observable. (Trivial) - Implement the
previousQuestion$
observable. (Hint - use thebufferCount
operator) - Implement the
previousAnswer$
observable. This is actually quite simple, since this is the latest answer. - Implement the
score$
,rank$
,isPreviousQuestionCorrect$
observables. To do that,- Start by creating a
questionAnswer$
observable of type:Observable<[Question, Answer]>
. This observable will push the next pair of question + answer. (Hint, usezip
to pair together questions and answers) - Now create a
history$
observable of typeObservable<[Question, Answer][]>
. Each event will hold the entire list of questions that were answered as question+answer pair. (Hint, usescan
to derive it fromquestionAnswer$
) - Now you can use
map
to createscore$
which is the accumulated score. - In the same way you can derive
rank$
andisPreviousQuestionCorrect$
from thehistory$
observable using map. Each time implement the mapping function seperatly as a pure function
- Start by creating a
- Implement the