Skip to main content
16 events
when toggle format what by license comment
Dec 1, 2019 at 10:22 vote accept thadeuszlay
Nov 26, 2019 at 21:15 comment added slepic It is about math, since it is about integers. In what way is integer related to javascript? There are just numbers in js and they are capable of representing a superset (+nonintegers) of a subset (not all integers are representable) of all integers. By your argument, NaN is also a number. Anyway for me Infinity is of the number type in JavaScript, but nowhere it is an integer. Anyway I would like to ask you to delete (or at least modify) the paragraph which causes all this confusion. Infinity being a number (js type) is irrelevant and misleading in context of task involving integers.
Nov 26, 2019 at 20:45 comment added Christian Ulbrich @slepic The thing is, it is not about math; it is about JavaScript: typeof (Infinity + Infinity) === typeof (1+2) For me Infinity is a number in JavaScript, if it is not for you - I am totally fine with that, be my guest! The test that OP wrote, were not for testing a solution for a math problem, but for a solution for a JS problem. I'd think we exchanged arguments pretty well...
Nov 26, 2019 at 17:17 comment added slepic I am not arguing at all against your code. Your code is not checking for infinity, it checks only for zeros or anything else and that fits well the definition of the task that is to accept array of integers and undefined behaviour otherwise. I argue that infinity is not an integer nor a number. You can think whatever you want, but this is true mathematical statement. I can give you another dozen proofs that confirm that. For example every integer is sum of two other nonzero integers. Infinity is not.
Nov 26, 2019 at 16:30 comment added Christian Ulbrich @slepic I disagree with you and that proves to me, that the original task is simply not precise enough. Anyway it is not the scope of the question and both solutions - mine and OP's will gracefully work with Infinity as input and they will also work without. But what is life without Infinity, I'd say!
Nov 26, 2019 at 10:28 comment added slepic All I'm trying to say Is that you should not treat Infinity as an edge case integer but an invalid input instead. Let me prove it by saying this. For any integer a the following equality Is Always true a == a. Because Infinity does not hold this equality, it Is not an integer. It Is actualy not a number at all by the same argument.
Nov 26, 2019 at 10:10 comment added slepic And isNaN(NaN) is true, And yet typeof NaN is number. The task states that input Is Array of integers. That said, Infinity or NaN in the input array means the behaviour Is undefined And as such can be anything you want. Throwing error might be appropriate...
Nov 26, 2019 at 9:40 comment added Christian Ulbrich @slepic isNaN(Infinity) evaluates to false and the typeof Infinity is number and I think it should be covered as an edge case. It is not about math, but about JavaScript (0.1 * 0.2). The original task is not precise enough and this is what I am alluding to.
Nov 26, 2019 at 9:36 comment added Christian Ulbrich @Clashsoft I like my code being descriptive and I find Array.from({length: 4}) very easy to grasp. I won't argue with you over elegance feel free to submit your own elegant answer.
Nov 25, 2019 at 14:47 comment added RoToRa @Clashsoft Not @ChristianUlbrich, but: From a readability standpoint Array(numberOfZeros) is ambiguous. For example, Array(a, b, c) creates an array containing a, b and c, so one could expect Array(a) to create an array containing just a - which is does unless a is an integer, in which case it creates an array of the length a instead. So using Array.from(...) like this makes it more obvious what is happening. That said: In this case I myself would prefer Array(numberOfZeros).
Nov 25, 2019 at 13:42 comment added JollyJoker @Clashsoft Or just go [...arr.filter(n => n === 0), ...arr.filter(n => n !== 0)], unless I'm missing some weird special case. If they're === 0, aren't they always, well, 0?
Nov 25, 2019 at 8:26 comment added Clashsoft Also, isn't Array.from({length: numberOfZeros}) the same as Array(numberOfZeros)? Please correct me if there's a subtle difference
Nov 25, 2019 at 8:26 comment added Clashsoft I think doing two filters is not very elegant. You could compute nonZeroArray first and then numberOfZeros = unsortedArray.length - nonZeroArray.length.
Nov 25, 2019 at 7:00 comment added slepic Can you explain what you mean by "How about Infinity and -Infinity? Are they numbers? :) isNaN(Infinity)?"? That's a bit misleading to say at least. isNaN(x) === false does not imply x is number. Well, depends what you mean by "being a number". If you mean being of js data type Number then Inf, -Inf and NaN are Numbers. If you mean mathematical sense, then nor inf nor -inf nor nan are numbers. You wont find infinity on the real number line, nor in the complex plane. And definitely not in the set of all integers.
Nov 25, 2019 at 1:15 review First posts
Nov 25, 2019 at 2:27
Nov 25, 2019 at 1:12 history answered Christian Ulbrich CC BY-SA 4.0