0

So i have two sets of data:

The first data is the answer key of an exam.

The second data is the answers from students whom are doing the exam.

From those two data, i tried to array.map them both.

const answerKey = [{
    id: "bida01",
    answerKey: "b",
    category: "bida",
    nomorSoal: "01",
    score: 20
  },
  {
    id: "bida02",
    answerKey: "b",
    category: "bida",
    nomorSoal: "02",
    score: 30
  }
]



const participants1 = [{
    answers: {
      bida01: "a",
      bida02: "b",
      bida03: "c",
    },
    category: "bida",
    firstName: "John",
    lastName: "Collins",
    school: "Something Junior High",
    address: "Somewhere",
    email: "[email protected]"
  },
  {
    answers: {
      bida01: "b",
      bida02: "b",
      bida03: "a",
    },
    category: "bida",
    firstName: "Jennifer",
    lastName: "Harley",
    school: "Somewhere Junior High",
    address: "Also somewhere",
    email: "[email protected]"
  }
]




const answerKeyData = answerKey.map((elem) => {
  console.log(elem.id, elem.answerKey, elem.score);
})

const participantData = participants1.map((elem, idx) => {
  console.log(elem.answer, elem.firstName, elem.middleName, elem.lastName, elem.school);

});

Now i can see from the result of those console.logs that i can get the values i want to compare. My purpose here is to compare student's answer to the answer keys, and then if they are a match:

return score += elem.score

But how do i access that elem.id, elem.answerKey, elem.score? Should i make a new object (lets say objectAnswerKey) with them? How can i do that?

The same with elem.firstName, elem.middleName, elem.lastName, elem.school, and elem.answer. Should i make a new object (lets say objectAnswer) and then compare objectAnswer and objectAnswerKey?

Or is there a more simple way?

2
  • I made you a snippet . Please make it a minimal reproducible example Commented Nov 17, 2021 at 16:04
  • 1
    You could do a nested loop. First, loop through the participants, then through their answers, then through the questions and check if the participants answer was equal to the correct answer you get from the question array. if yes, you add the score to the user. Commented Nov 17, 2021 at 16:06

2 Answers 2

1

You can use nested map/forEach/reduce array methods to compute scores by comparing question id and answerKey like the one below,

const participantsWithScore = participants1.map(student => {
    const answers = student.answers;
    const questions = Object.keys(answers);
    let score = 0;
    questions.forEach(question => {
        const answer = answers[question];
        const correctAnswer = answerKey.find(key => key.id === question);
        if(correctAnswer && correctAnswer.answerKey === answer) {
            score += correctAnswer.score;
        }
    });
    student.score = score;
    return student;
});
console.log(participantsWithScore);

In the console, you can see all the participants having the computed score property.

Sign up to request clarification or add additional context in comments.

Comments

1

const answerKey = [
  {
    id: "bida01",
    answerKey: "b",
    category: "bida",
    nomorSoal: "01",
    score: 20
  },
  {
    id: "bida02",
    answerKey: "b",
    category: "bida",
    nomorSoal: "02",
    score: 30
  }
];

const participants1 = [
  {
    answers: {
      bida01: "a",
      bida02: "b",
      bida03: "c"
    },
    category: "bida",
    firstName: "John",
    lastName: "Collins",
    school: "Something Junior High",
    address: "Somewhere",
    email: "[email protected]"
  },
  {
    answers: {
      bida01: "b",
      bida02: "b",
      bida03: "a"
    },
    category: "bida",
    firstName: "Jennifer",
    lastName: "Harley",
    school: "Somewhere Junior High",
    address: "Also somewhere",
    email: "[email protected]"
  }
];

const handleCalculate = () => {
    const studensScore = [];
    participants1.forEach((student) => {
      let score = 0;
      answerKey.forEach((answerKeyItem) => {
        const userAnswer = student.answers[answerKeyItem.id];
        if (userAnswer === answerKeyItem.answerKey) {
          score += answerKeyItem.score;
        }
      });
      const studentData = {
        fullname: student.firstName + " " + student.lastName,
        score: score
      };
      studensScore.push(studentData);
    });
    return studensScore;
  };
  
  console.log(handleCalculate())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.