1

How would I write a function that takes two arguments (the array responses and a string), and for the isEssayQuestions that are true, compares the string parameter to the response key value and returns true if they match anyone and false if it doesn't match any.

const responses = [
  {
    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
  }
];

// example
checkForPlagiarism(responses, 'spherical vesicles that contain hydrolytic enzymes'); //> true
checkForPlagiarism(responses, 'this string does not appear in the responses'); //> false

// my code
function checkForPlagiarism(test, answerKey) {
  for (let i = 0; i < test.length; i++) {
    let question = test[i];
    if (question.isEssayQuestion) {
      let response = question.response;
      return response.includes(answerKey);
    }
  }
}
Z_Cason
  • 25
  • 5
  • `const checkResponses = (resps, checkStr) => resps.filter(r => r.isEssayQuestion).some(r => r.response.includes(checkStr))` – CRice Jun 16 '20 at 18:30
  • Does this answer your question? [How do I loop through or enumerate a JavaScript object?](https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object) – imvain2 Jun 16 '20 at 18:30

2 Answers2

0

You don't need to iterate over the object, as the given response is an array itself.

What you've done seems pretty fine to me except for return statement. So add your return statement in the if of string match logic.

function checkForPlagiarism(test, answerKey) {
  for (let i = 0; i < test.length; i++) {
    let question = test[i];
    if (question.isEssayQuestion) {
      let response = question.response;
      if (response.includes(answerKey)) return true;
    }
  }
  return false
}
Siraj Alam
  • 9,217
  • 9
  • 53
  • 65
  • Thanks a lot. I see when I returned `response.includes(answerKey)` it would just return true or false instead looping through the whole list of responses. Right? – Z_Cason Jun 16 '20 at 18:45
  • Yes. your `return` condition wasn't the string match, but only `isEssayQuestion` flag – Siraj Alam Jun 16 '20 at 18:47
0

Your code is already returning the correct values (true if a match was found or false if no match has been found), just add a return false after the for loop if no match was found (with no isEssayQuestion: true). Do a console.log() on the values returned by your function calls to check it out.

Examples:

console.log(checkForPlagiarism(responses, 'spherical vesicles that contain hydrolytic enzymes')) console.log(checkForPlagiarism(responses, 'this string does not appear in the responses'))

Hatim Lagzar
  • 52
  • 1
  • 8