Why does the not not return true in both cases

I've always managed to misuse the fact that 0 == boolean false and all other numbers == boolean true

But it has failed me in this case using my custom max reporter

image

image

What is the reason for it failing in this case?

Is 0 cast to a string for some reason? In JS, !(0) is true while !("0") is false (on string, truthiness is just "length ≥ 1")

You may be onto something as this returns false as well as mine

image

The only thing is, that my reporter is just returning the inputs (which are both numbers)

So that implies that maybe the if reporter is doing some sort of type conversion???

PS before anyone says - I understand that assuming 0 is false isn't a defensive way of programming and I won't do it again in future :slight_smile: But it would be nice to know why its not working in this case :slight_smile:

But of course 0 (and -6) are strings! Either you edit the block to take numbers, or it is like you're running max(a: any, b: any) = if (a as number > b as number) a else b, so the return value (either a or b) is a string (any, really)

Of course :slight_smile:

image

And I've modified my reporter to convert the output to a number even if the inputs are numerical strings

image

That's not always what you need, however

In what scenario?

iirc JS's native Math.max returns not cast, and probably someone has usage for that. If I do maxThatCastsToNumbers('15', 10) === '15', that's false.

Snap! just uses JS as it's engine - it doesn't/shouldn't just follow what JS would do. :slight_smile:

I think a max function in Snap! should probably follow same convention as the other maths operators and return a number as this example does

image

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