Problem with bug

Tell us what’s happening:
Please what is wrong with my if…else statement?.
It should return false if emp and arr has an element in common. But it is doing the opposite.
Your code so far


function truthCheck(collection, pre) {
var emp=[];
var arr=["", undefined, null, 0, false, []];
collection.forEach(elem=>{
  emp.push(elem[pre])
})

for(let i=0; i<emp.length; i++){
  for(let j=0; j<arr.length; j++){
    if(emp[i]!==arr[j]){
      var result=true
    }else if(emp[i]===arr[j]){
      var result=false
    }
  }
}
//console.log(emp)
return result
}

console.log(truthCheck([{"user": "Tinky-Winky"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex"));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Linux; U; Android 7.0; TECNO P701 Build/NRD90M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/88.0.4324.181 Mobile Safari/537.36 OPR/54.0.2254.56148.

Challenge: Everything Be True

Link to the challenge:

this will give the result for whatever last values i and j have, so your code is at the end checking only the last one, not all of them

This is a better alternative then how you are using arr
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_NOT
This will work similarly, but make sure you read the docs to understand the difference
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean

1 Like

please how do i make it check everything?

helpful. thank you for the link

This will work wih your current use of arr
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

You will however need to use: Number.isNaN() - JavaScript | MDN as (NaN === NaN) would be false.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.