What's wrong here? Intermediate Algorithm Scripting: Everything Be True

What's wrong here? Intermediate Algorithm Scripting: Everything Be True
0

#1

Hello all,

Can someone please assist me in trying to solve this challenge? From what I’m seeing, the code block is fulfilling the set conditions, which is obviously not the case. I’m more looking for an explanation as to what my code is doing wrong than an actual solution, because where’s the fun in that?

function truthCheck(collection, pre) {
  let result;
  for(let i in collection) {
       if(collection[i].hasOwnProperty(pre)) {
         if(collection[i].pre) {
           result = true;
         } else {
           result = false;
         }
       }
     }
     return result;
}

truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex");

#2

pre here contains the actual property name that you want to check, so you should use bracket notation.

The if-else block keeps switching the value in the result variable. The function should return false when it finds one, so subsequent iterations shouldn’t be able to change it back to true.

The function also doesn’t handle the case where collection[i].hasOwnProperty() is false