user6885321
user6885321

Reputation:

the every() method not returning correct value

Can someone kindly tell me why this is returning true? It should return false because in the first iteration value[pre] === 0... Thanks.

function truthCheck(collection, pre) { 
   function check(value){
      if(value.hasOwnProperty(pre)){
         return value[pre] !== null || value[pre] !== undefined || value[pre] !== ""|| value[pre] !== 0; 
      }
   } 
   return collection.every(check);
}    
truthCheck([{"user": "Tinky-Winky", "sex": "male", "age": 0}, {"user": "Dipsy", "sex": "male", "age": 3}, {"user": "Laa-Laa", "sex": "female", "age": 5}, {"user": "Po", "sex": "female", "age": 4}], "age");

Upvotes: 1

Views: 29

Answers (1)

guest271314
guest271314

Reputation: 1

Use && operator

return (value[pre] !== null 
        && value[pre] !== undefined 
        && value[pre] !== "" 
        && value[pre] !== 0);

jsfiddle https://jsfiddle.net/4wcovask/

Upvotes: 1

Related Questions