theNeighbourhoodGhost theNeighbourhoodGhost - 2 months ago 6
Javascript Question

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

Answer

Use && operator

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

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