Miomir Dancevic
Miomir Dancevic

Reputation: 6852

Comparing two objects typescript and check if there three or more value same

Hello all can somebody help me I don't know even where to start, little help is great. I have object like this

const Questions = {
 five: "c"
 four: "c"
 one: "a"
 three: "a"
 two: "a"
};

And I have some object like this

const Answers = {
 five: "a"
 four: "a"
 one: "a"
 three: "a"
 two: "a"
};

I need to compare these two objects and see if there are 3 or more matches correct, and based on that to show a different message. I know I can compare two objects like this

JSON.stringify(Questions) === JSON.stringify(Answers);

It will return me just true or false, I have tried also to return just corrected answer but again I had no luck, something like this

  checkCorrectAnswers(obj1: any, obj2: any): any {
    const keys1 = [];
    const values1 = [];
    Object.keys(obj1).forEach((element) => {
      keys1.push(element);
    });
    Object.values(obj1).forEach((element) => {
      values1.push(element);
    });
    const keys2 = [];
    const values2 = [];
    Object.keys(obj2).forEach((element) => {
      keys2.push(element);
    });
    Object.values(obj2).forEach((element) => {
      values2.push(element);
    });
    const obj = {};
    keys1.forEach((element, i) => {
      for (let index = 0; index < keys2.length; index++) {
        if (element === keys2[index]) {
          if (values1[i] !== values2[index]) {
            const jsonKey = element;
            obj[jsonKey] = values1[i];
          }
          break;
        }
      }
    });
    return obj;
  }

Thanks again on the answer.

Upvotes: 1

Views: 923

Answers (3)

Karma Blackshaw
Karma Blackshaw

Reputation: 942

You can just loop over the questions and check for the answers like so:

const Answers = {
 five: "a",
 four: "a",
 one: "a",
 three: "a",
 two: "a"
};

const Questions = {
 five: "c",
 four: "c",
 one: "a",
 three: "a",
 two: "a"
};

const isPass = (passingScore) => {
  let score = 0

  for (const questionKey in Questions) {
    const questionValue = Questions[questionKey];
    const answerValue = Answers[questionKey]

    const isCorrect = answerValue === questionValue

    if (!answerValue) {
      continue
    }

    if (isCorrect) {
      score++
    }
  }

  return score >= passingScore
}

What's good about this is it only costs one loop.

Upvotes: 1

BaiJiFeiLong
BaiJiFeiLong

Reputation: 4665

const Questions = {five: "c", four: "c", one: "a", three: "a", two: "a"};
const Answers = {five: "a", four: "a", one: "a", three: "a", two: "a"};
rightAnswers = Object.fromEntries(Object.entries(Answers).filter(([k, v]) => Questions[k] === v));
wrongAnswers = Object.fromEntries(Object.entries(Answers).filter(([k, v]) => Questions[k] !== v));
console.log("Right answers:", rightAnswers);
console.log("Wrong answers:", wrongAnswers);
console.log("Passed:", Object.keys(rightAnswers).length >= 3);

Output

Right answers: { one: 'a', three: 'a', two: 'a' }
Wrong answers: { five: 'a', four: 'a' }
Passed: true

Upvotes: 0

Nguyễn Văn Phong
Nguyễn Văn Phong

Reputation: 14228

You can calculate the correct answers in this way.

const Questions = {five:"c",four:"c",one:"a",three:"a",two:"a"};
const Answers = {five:"a",four:"a",one:"a",three:"a",two:"a"};

const props = Object.keys(Questions);
const number_Of_Correct_Answers = props.reduce((acc, curr_prop) => {
  if(Questions[curr_prop] === Answers[curr_prop]) acc++;
  
  return acc;
}, 0);

console.log(number_Of_Correct_Answers);

Upvotes: 0

Related Questions