【发布时间】:2020-09-20 18:27:34
【问题描述】:
这是它返回的结果,即使检查的参数很清楚。根据描述,罪魁祸首应该是Jaunita Myrle。请帮我找出问题所在,因为我无法弄清楚我的代码有什么问题:
以下是说明:
Officer: 3357927
CaseNum: 701-1-60140847-3357927
Case 701 - Credible cat thief - stage 2
Kid they need you down at the precinct again.
This time it's a sneaky cat thief who has been absconding with the neighbourhoods felines for some time.
Luckily old Mrs Olivetti caught a glimpse of them as they disappeared over her back fence.
We’ve a bunch of likely characters lined-up but we need your brains to solve the mystery.
Please create a function that takes a suspect object as parameter from the data structure below.
Your function should return a boolean value indicating whether or not they match the witness statement.
You should use conditional statements to compare the suspect's properties to the statement.
It should only return "true" if the suspect matches the description in full.
The function is already being called in draw() but it is your job to implement it.
There are many possible ways of carrying out your duties,
but you should complete this task using ONLY the following
commands:
- function testProperties(suspectObj){}
- if()
Witness statement:
It all started when I was exiting the store. That's when I noticed them. I'm pretty sure they were above the age of 48. They had thin blond hair. Their expression seemed menacing. It's so hard to remember right now. The person I saw was male. They wore red glasses. They were quite big, they probably weigh more than 52 Kg. I'm not quite sure. That's all I can remember about them.
代码如下:
var suspectList = [
{
"name": "JENIFFER GOODBURY",
"gender": "female",
"expression": "blank",
"hair": "thick black",
"weight": 64,
"age": 62
},
{
"name": "JAUNITA MYRLE",
"gender": "male",
"expression": "menacing",
"hair": "thin blond",
"weight": 62,
"age": 58
},
{
"name": "TAMICA DURANTS",
"gender": "female",
"expression": "sad",
"hair": "no",
"weight": 100,
"age": 43
},
{
"name": "LESLEY PORTOS",
"gender": "female",
"expression": "angry",
"hair": "long white",
"weight": 92,
"age": 47
},
{
"name": "JACQUELINE SILVEIRA",
"gender": "male",
"expression": "empty",
"hair": "shaved",
"weight": 70,
"age": 21
}
];
var myFont;
var backgroundImg;
function preload() {
myFont = loadFont('SpecialElite.ttf');
backgroundImg = loadImage("Background.png");
}
function setup()
{
createCanvas(640,480);
textFont(myFont);
}
// Declare your function here
function testProperties(suspectObj)
{
if(suspectList.age > 48 && suspectList.hair == "thin blond" && suspectList.expression == "menacing" && suspectList.gender == "male" && suspectList.weight > 52 )
{
return true;
}
return false;
}
function draw()
{
//You don't need to alter this code
image(backgroundImg, 0, 0);
for(let i = 0 ; i < suspectList.length; i++){
if(testProperties(suspectList[i]) == true){
fill(255,0,0);
text(suspectList[i].name + " is guilty!", 60, 60 + i * 20);
}else{
fill(0,155,0);
text(suspectList[i].name + " is not guilty", 60, 60 + i * 20 );
}
}
}
【问题讨论】:
标签: javascript function object p5.js