Can someone help me finding the logical error in the code bellow? When it runs, the program displays all as "not guilty" despite one of the objects in the array satisfies the condition.
var suspectsArray = [
{
"name": "BRIDGET ASHELY",
"glasses": "very thick",
"accessory": "metal briefcase",
"eyes": "pale",
"height": 155,
"age": 63
},
{
"name": "LIANNE NIEMELA",
"glasses": "blue",
"accessory": "plastic box",
"eyes": "brown",
"height": 150,
"age": 47
},
{
"name": "JAUNITA FORSLIN",
"glasses": "dark brown",
"accessory": "laptop bag",
"eyes": "grey",
"height": 182,
"age": 58
},
{
"name": "JULIANA DEAUVILLE",
"glasses": "red",
"accessory": "glass bottle",
"eyes": "green",
"height": 175,
"age": 34
},
{
"name": "LINETTE DORCEY",
"glasses": "light tan",
"accessory": "big black envelope",
"eyes": "blue",
"height": 178,
"age": 43
}
];
var myFont;
var backgroundImg;
function preload() {
myFont = loadFont('SpecialElite.ttf');
backgroundImg = loadImage("Background.png");
}
function setup()
{
createCanvas(640,480);
textFont(myFont);
}
function matchSuspect(suspectObj){
for (var k = 0; k < suspectsArray.length; k++)
{
if(
suspectsArray[k].glasses == "blue" &&
suspectsArray[k].accessory == "plastic box" &&
suspectsArray[k].eyes == "brown" &&
suspectsArray[k].height > 141 &&
suspectsArray[k].age < 49
){
return true;
}
return false;
}
}
function draw()
{
image(backgroundImg, 0, 0);
for(let i = 0 ; i < suspectsArray.length; i++){
if(matchSuspect(suspectsArray[i]) == true){
fill(255,0,0);
text(suspectsArray[i].name + " is guilty!", 60, 60 + i * 20);
}else{
fill(0,155,0);
text(suspectsArray[i].name + " is not guilty", 60, 60 + i * 20 );
}
}
}
If running correctly, it should display the second object in the array in red. I am not sure if the iterations are wrong or what. To experiment with that I tried using || instead of && and the logic was correct (or so the program gave the right answers).
It looks to me like a simple solution, but I spend a very long time on it and couldn't get to it :/