【问题标题】:My function is not identifying the correct person我的功能没有识别正确的人
【发布时间】: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


    【解决方案1】:

    如此接近!我想你有错字。你可能指的是suspectObj,而不是testProperties中的suspectList:

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

    您可能想要放慢速度并仔细检查代码和数据(例如,它是“Jaunita”还是“Juanita”,Jacqueline 是男性,等等)

    【讨论】:

    • 谢谢!奇怪的是,这是他们告诉我们要使用的功能,所以我只是复制并粘贴了它
    • 明白了。就个人而言,如果是您的代码或其他任何人的代码,当您添加一个新功能时,我建议您养成测试它的习惯。将一些好的数据和坏的数据传递给它,看看它的行为是否符合预期,否则修复/改进,然后继续。这样做的原因是,随着您添加越来越多的功能,发现问题变得越来越困难,除非您确保一切正常工作。如果上面的答案是您问题的解决方案,请随时用绿色复选标记标记它:) 祝你好运!
    猜你喜欢
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    相关资源
    最近更新 更多