My function is not identifying the correct person

Viewed 90

This is the result it returns even though the parameters for inspection are very clear. According to the description, the guilty person should be Jaunita Myrle. Please help me identify what the issue is because I can't quite work out what's wrong with my code:

enter image description here

Here are the instructions:

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. 

Here is the code:

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

1 Answers

So close! I think you have typo. You probably meant suspectObj instead of suspectList in testProperties:

function testProperties(suspectObj)
{
    if(suspectObj.age > 48 && suspectObj.hair == "thin blond" && suspectObj.expression == "menacing" && suspectObj.gender == "male" && suspectObj.weight > 52 )
    {
        return true;
    }
    return false;
}

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

function testProperties(suspectObj)
{
    if(suspectObj.age > 48 && suspectObj.hair == "thin blond" && suspectObj.expression == "menacing" && suspectObj.gender == "male" && suspectObj.weight > 52 )
    {
        return true;
    }
    return false;
}

console.log(suspectList.map( s => { return {name: s.name, test: testProperties(s)} }));

You might want to slow down and double check both the code and the data (e.g. is it "Jaunita" or "Juanita", is Jacqueline a male, etc.)

Related