To solve this problem, I'm tasked with creating a function called findAnswers(answers, questions). The function should return the item in the array that corresponds to the given question. If none of the student's answers match the question, the function should return undefined. In the case where more than one student's answer matches the question, the function should return the first one in the array.
Here is an example output:
findAnswer(answers, "True or False: Prostaglandins can only constrict blood vessels."); /*=>
{
question: 'True or False: Prostaglandins can only constrict blood vessels.',
response: 'True',
isCorrect: false,
isEssayQuestion: false
}
*/
Given the array:
let answers = [
{
question: 'What is the phase where chromosomes line up in mitosis?',
response: 'Metaphase',
isCorrect: true,
isEssayQuestion: false
},
{
question: 'What anatomical structure connects the stomach to the mouth?',
response: 'Esophagus',
isCorrect: true,
isEssayQuestion: false
},
{
question: 'What are lysosomes?',
response: 'A lysosome is a membrane-bound organelle found in many animal cells. They are spherical vesicles that contain hydrolytic enzymes that can break down many kinds of biomolecules.',
isCorrect: true,
isEssayQuestion: true
},
{
question: 'True or False: Prostaglandins can only constrict blood vessels.',
response: 'True',
isCorrect: false,
isEssayQuestion: false
}
];
Here is what I've attempted so far:
function findAnswer(answers, question) {
let result = {};
for (let i = 0; i < answers.length; i++) {
let studentAnswers = answers[i];
if (studentAnswers === answers) {
result[answers[i].question]
}
}
return result;
}
I'm aware that the current code is not optimal, and I'm struggling with returning an array of matching responses.