As a newcomer to JS app development, I am currently focused on studying existing code and attempting to replicate it while playing around with variable names to test my understanding.
I have been working on this JS quiz code built with KO.js... Here is my version (with minimal changes).
The issue I'm facing is that when running the code, I encounter an "isCorrect is undefined" error not in my script, but within the KO library. If I comment out the lines containing isCorrect in the HTML, I then receive a "correctNbrAnswers is undefined" error, still within the KO library...
It seems like KO is having difficulty recognizing the data parameters I'm using to reference the appropriate templates...
<script id="questionView" type="text/html">
<div>Question <span data-bind="text: index"></span></div>
<div data-bind="visible: selectedAnswer() === undefined">
<div data-bind="text: questionText"></div>
<ul data-bind="template: { name: 'answerView', foreach: answers }"></ul>
</div>
<div data-bind="visible: selectedAnswer() !== undefined">
<div data-bind="template: { name: 'questionResultView', data: selectedAnswer }"></div> <!-- isCorrect id defined for answers, so if KO respects this scope, this should work... But it doesn't! -->
<a href="#" data-bind="click: $parent.nextQuestion">Next</a>
</div>
</script>
<script id="questionResultView" type="text/html">
<div data-bind="visible: isCorrect">Correct Answer!</div> <!-- Returns isCorrect is undefined (knockout.js line blablabla) -->
<div data-bind="visible: !isCorrect">Incorrect. The correct answer was <span data-bind="text: $parent.correctAnswer.answerText"></span></div>
</script>
If anyone could provide insights into this matter, I would greatly appreciate it...
Thank you!
Ari ;o)