Checking Empty Bracket Value

Tell us what’s happening:
I’m having trouble passing one of the test cases on this challenge. Here is the test case:

truthCheck([{name: "freeCodeCamp", users: [{name: "Quincy"}, {name: "Naomi"}]}, {name: "Code Radio", users: [{name: "Camperbot"}]}, {name: "", users: []}], "users") should return true .

What’s confusing me is that as you can see near the top of my code, the value of collection[2][‘users’] is , but if I enter (collection[2][‘users’] == ) I receive a value of false, and only get true when I enter (collection[2][‘users’] == ‘’).

I’m already checking in my code that any collection[collect][pre]= ‘’ will return false so I don’t know why this isn’t working, but I suspect is has something to do with the (seemingly contradictory) behavior outlined above.

  **Your code so far**

function truthCheck(collection, pre) {

console.log(collection[2]['users'])
console.log(collection[2]['users'] == '')
for (let collect in collection){
  if (collection[collect][pre] == false || collection[collect][pre] == '' || (typeof collection[collect][pre] == 'number' && isNaN(collection[collect][pre]) == true) || collection[collect].hasOwnProperty(pre) == false || collection[collect][pre] == null){
    return false;
  } else {
  } 
} return true;

}

console.log(truthCheck([{name: "freeCodeCamp", users: [{name: "Quincy"}, {name: "Naomi"}]}, {name: "Code Radio", users: [{name: "Camperbot"}]}, {name: "", users: []}], "users"));

  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.3 Safari/605.1.15

Challenge: Everything Be True

Link to the challenge:

You’re missing the important idea of truthiness and falsyness. If you use a truthy value in an if statement, if (someTruthyValue), it will behave exactly as if you used if (true).

1 Like

Just to clarify a bit about your question.

  1. == does type coercion. In order to compare an array against a string, it must call toString on the array.
[] == ''
// true

[].toString()
// ''

[1, 2, 3] == '1,2,3'
// true

[] === ''
// false

MDN Equality (==)

If one of the operands is an object and the other is a number or a string, try to convert the object to a primitive using the object’s valueOf() and toString() methods.

  1. An empty array is a truthy value.
Boolean([])
// true
  1. Two arrays are not the same even if they have the same content. They are different objects.
[1, 2, 3] === [1, 2, 3]
// false

[1, 2, 3] == [1, 2, 3]
// false

1 Like

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