首页 > 解决方案 > 编写一个名为 checkForPlagiarism 的函数,它接受两个参数:学生答案数组和文本片段

问题描述

/*编写一个名为 checkForPlagiarism 的函数,它接受两个参数:学生答案数组和要检查的文本片段,如果任何论文问题的答案包含文本,则返回 true,否则返回 false。对于 answers 数组中的每个论文问题,检查响应是否包含文本片段。如果是,则返回 true。

使用下面的示例答案,

checkForPlagiarism(answers, '含有水解酶的球形囊泡'); //=> true checkForPlagiarism(answers, '这个字符串没有出现在回复中'); //=> false 提示:您可能需要查看该问题的 String .includes() 方法。

笔记:

只有论文问题的回答算作抄袭。如果非论文问题的答案包含文本片段,这并不意味着您必须返回 true。*/

在下面的代码中,我试图遍历 array.response 部分以查看是否找到了 studentAnswer 字符串,但它一直返回 false。我什至尝试了另一种选择并得到了相同的结论。

 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
 }
 ];


 function checkForPlagiarism(array, studentAnswer){
 for(let i = 0; i<array.length;i++)
 return array[i].response.includes(studentAnswer);
 }

 function checkForPlagiarism(array, studentAnswer){
 return array.includes(array => array.response === studentAnswer);
 }

标签: javascript

解决方案


您可以使用该.some方法。另外,您被要求检查它是否是一篇论文问题;否则不算抄袭:

function checkForPlagiarism(array, studentAnswer){
    return array.some(q => q.isEssayQuestion && q.response.includes(studentAnswer));
}

推荐阅读