logoalt Hacker News

chiakic12/09/20244 repliesview on HN

But if you consider “liar” to be an object,

> liar.hats.every((hat) => hat.color === "green")

will throw a TypeError: Cannot read properties of undefined. That’s definitely not `true`.


Replies

wrigby12/09/2024

This depends on `liar.hats` being undefined, but what if `liar.hats` is an empty array? That seems like an equally valid way of representing a person that owns no hats.

ecshafer12/09/2024

You implemented the problem wrong, and thus got an error. If hats is a list of colors, then every hat != green is true if the list is empty.

show 1 reply
recursive12/09/2024

Hm. No.

    const liar = { hats: [] };
    liar.hats.every(hat => hat.color === "green")

    true