user16863092
user16863092

Reputation: 3

Function that returns true or false

I am stuck writing out this function, instructions below.

Instructions Write a function named checkForPlagiarism that

takes two arguments: an array of student answers, and snippet of text to check returns true if any of the essay question responses contain the text, and returns false otherwise

For each essay question in the answers array, check whether the response contains the snippet of text. If it does, return true.

Using the example answers below,

checkForPlagiarism(answers, 'spherical vesicles that contain hydrolytic enzymes'); //=> true checkForPlagiarism(answers, 'this string does not appear in the responses'); //=> false Hint: You may want to check out the String .includes() method for this question.

Notes:

Only essay question responses count as plagiarism. If an answer to a non-essay question contains the snippet of text, that doesn't mean you should necessarily return true. Example Quiz Data A student's answers to a quiz are represented as an 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 have so far:

function checkForPlagiarism(answers, snippet){
  for (let i = 0; i < answers.length; i++){
    const currentAnswers = answers[i];
    if(currentAnswers[i].isEssayQuestion === true){
      if(snippet === currentAnswers[i].response){
        return true;
     }
    }
  }
  return false;
}

I have also tried this (which does not test correct either):

function checkForPlagiarism(answers, snippet){
  for (let i = 0; i < answers.length; i++){
    if(answers[i].isEssayQuestion === true){
      if(snippet === answers[i].response){
        return true;
     }
    }
  }
  return false;
}

Upvotes: 0

Views: 1470

Answers (2)

pastaleg
pastaleg

Reputation: 1838

The issue with your first attempt is that you use the index operator [] twice in a row.

const currentAnswers = answers[i];
if(currentAnswers[i].isEssayQuestion === true){
...

so to make the program run, you should remove one of the index operators.

The issue with your second attempt is that you check for strict equality between the snippet and the essay answer. As mentioned in the prompt, the function should also return true for cases when the snippet is a substring of the question answer.

The solution to that is to take the prompt's advice and use includes() to check if one string is a substring of the other.

if( answers[i].response.includes(snippet)){
    return true;
 }

Upvotes: 0

kozikov
kozikov

Reputation: 71

First of all you have a array of data and you must check every element, for this situation usually array iteration methods would help you(e.g. map, filter, some, every e.c.).

Array method some tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.

As you can see you only must write a correct condition in some method of array and it will return true if one element in answers meets the condition.

isEssayQuestion is already boolean and to check if string contains some other string you can use method includes that return boolean value

So it's be Something like this

function checkForPlagiarism(answers, snippet) {
  return answers.some(answer => answer.isEssayQuestion && answer.response.includes(snippet))
}

Upvotes: 1

Related Questions