javascript - 编写一个名为 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);
}
解决方案
您可以使用该.some
方法。另外,您被要求检查它是否是一篇论文问题;否则不算抄袭:
function checkForPlagiarism(array, studentAnswer){
return array.some(q => q.isEssayQuestion && q.response.includes(studentAnswer));
}
推荐阅读
- spring - 使用 AsyncItemProcessor 和 AsyncItemWriter 时数据库连接超时
- python - 对象在没有 session.commit() 的情况下添加到数据库
- google-chrome - 强制 Chrome Inspector 显示真实的源代码而不是它的解释
- php - PHP 上是否有保证的文件资源?
- javascript - 如何装饰 Web 组件类
- node.js - 使用 VSTS 在 Azure Web 应用程序上部署 Node.js 应用程序。成功部署后,应用程序代码文件在 Web 应用程序目录结构中不可用
- python - 如何使用解密密钥加密和解密熊猫数据帧?
- r - (在 R 中)将元数据从向量添加到数据帧的一组列?
- c++ - C ++将未知数量的整数读取到cin并输出总和
- ms-access - 访问查询:将Like "*2018*" 中的2018 替换为当前年份