Using .includes()

随声附和 提交于 2020-12-16 03:43:13

问题


So here is the question for the problem.

Write a function named checkForPlagiarism that takes two arguments: an array of all of the responses for a given person a string of text that represents an external source For each essay question in the listed responses, check whether or not the response value contains the given string. If it does, return true; otherwise, return false.

The function is supposed to check the response in the argument with the responses in the responses array. Can't seem to get it to return the correct boolean

So here is the repl.it. Prompt 3 is the one I am completely stuck on.

https://repl.it/@AngeloLongoria/Take-Home-Science-Quiz


回答1:


You can do it like this(using some):

text="lysosome";
getResult = responses.some((elem)=>elem.response.toUpperCase().includes(text.toUpperCase()));

Since you just have to return the boolean value if it is present in the array or not, some will do this for you.

I hope this helps. Thanks!




回答2:


I think the issue is with your container array.

After looping the value of container is an array of string.

[
    "Metaphase",
    "Esophagus",
    "A lysosome is a membrane-bound organelle found in …s that can break down many kinds of biomolecules.",
    "True"
]

include is looking for an exact match. See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes.




回答3:


I suggest that you consider using array.filter instead of looping.

function checkForPlagiarism(responses, input) {
    return responses.filter(x => x.response === input);
}

result = checkForPlagiarism(response, "Esophagus") // 1

if (result.length) {
    console.log("That answer has already been given")
} 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

As gorak suggests, filter in the above example could be replaced by some, which would return a boolean rather than a number of matches, and would also stop searching the array once a match was found, which would be more efficient.




回答4:


Without discussing the strange definition of plagiarism, Array.find would do what is asked I suppose. Snippet for exact match and "the response value contains the given string" (fuzzy).

const responses = getResponses();
// strict equality check
const checkForPlagiarism = (responses, answer) =>
  responses.find(v => v.response === answer) ? true : false;
// check substring in some response
const checkForPlagiarismFuzzy = (responses, answer) => 
  responses.find(v => new RegExp(answer, "gi").test(v.response)) ? true : false;

console.log('[exact] "lysosomes are cellular organelles" =>',
  checkForPlagiarism(responses, 'lysosomes are cellular organelles'));
console.log('[exact] "Esophagus" =>', checkForPlagiarism(responses, 'Esophagus'));
console.log('[exact "True" =>', checkForPlagiarism(responses, 'True'));
console.log('[fuzzy] "a membrane-bound organelle" =>',
  checkForPlagiarismFuzzy(responses, 'a membrane-bound organelle'));
console.log('[fuzzy] "a membrane-bound stomach" =>', checkForPlagiarismFuzzy(responses, 'a membrane-bound stomach'));
console.log('[fuzzy] "Tru" =>', checkForPlagiarismFuzzy(responses, 'Tru'));

function getResponses() {
  return [{
      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
    }
  ];
}
.as-console-wrapper { top: 0; max-height: 100% !important; }


来源:https://stackoverflow.com/questions/61745869/using-includes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!