logoalt Hacker News

chiakic • 12/09/2024 • 4 replies • view 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

wrigby • 12/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.

ecshafer • 12/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
recursive • 12/09/2024

Hm. No.

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

    true