4
u/buffer_overfloooooow Mar 26 '21
Also dumb because it doesn't work for even one digit numbers
7
u/Vercyx Mar 26 '21
to be fair, it actually does since its < and not <=
so 4/2 is going to have a length of 1, which is not less than a length of 1
it technically works for all numbers but it's quite stupid otherwise
6
u/Andersmith Mar 27 '21
Technically it won’t work if given a non integer value (like .2), but input sanitation seems a bit too nitpicky of an issue for this sub.
3
u/Vercyx Mar 27 '21
I guess it depends if you consider .2 even or odd.
.2/2 = .1 which is a string of the same length, therefore it should be even
.3/2 = .15 which is longer, therefore should be odd
JS has implicit type casting so it doesn't really matter what the input is, it will return SOMETHING
7
u/ghillerd Mar 27 '21
parity is a property of integers, so an `isEven` function should return false for a non-integer, as should `isOdd`.
2
u/Vercyx Mar 27 '21
I guess that makes sense, I assume I figured I'd never pass a float into it LOL
2
u/Andersmith Mar 27 '21
Yeah that’s why I categorized my nitpick as input sanitation. These functions aren’t supposed to be applied to anything other than ints. And to it’s credit, it does work on all ints.
3
u/bphase Mar 27 '21
One could also argue for an exception, as you're violating the precondition of the number being an integer if you call it with a float.
1
u/ghillerd Mar 27 '21
True. If the function is supposed to be something like, getParity, and returns 0 or 1 depending on if it's even or odd, then it should definitely throw if you give it something other than an integer. With isEvenNumber, it's closer, because a string or a function or a decimal or whatever isn't an even number.
2
u/jarfil Mar 27 '21 edited May 12 '21
CENSORED
1
u/ghillerd Mar 27 '21
Bonus points for De Morgan's law, but I think you're missing an inversion? Or is that just what makes this a true horror show...
1
2
u/F54280 Mar 27 '21
parity is a property of integers, so an
isEvenfunction should return false for a non-integer, as shouldisOdd.I would argue that it should raise an error, or in a more JavaScript way, return FileNotFound
More seriously, I really don’t like the idea that
isOddis not!isEven. But that may be me.
177
u/Je-Kaste Mar 26 '21
Oh my god, for the last time it's
return (number).toString().length >= (number/2).toString().length;Ffs
60
u/KlaireOverwood Mar 26 '21
You mean
return (number/1).toString().length >= (number/2).toString().length;
16
23
u/Hydroxylic-Acid Mar 27 '21
Modulo is also bad code, bruh just use
return (number & 1)/s
5
u/sixtyfifth_snow Mar 27 '21
BTW, is it working? I haven't tried JS and heard JS only supports floating points, so, does JS supports bit-wise operator on floating point numbers?
4
u/ewpratten Mar 27 '21
Not a js programmer, but I remember reading something about bitwise operations auto-casting to an integer.
7
u/TinyBreadBigMouth Mar 27 '21
A 32-bit signed integer, specifically. JS is wonk.
1
u/AnIndecisiveOrange Mar 28 '21
JS is wonk
Truer words have never been spoken
If you really need an unsigned int btw you can do a zero fill right shift by 0 (x >>> 0)
19
u/Ninjakannon Mar 27 '21 edited Mar 27 '21
For those wondering, the traditional approach is:
return number % 2 === 0;Yep, that's it. If even, return true.
Edit: I checked for odd first, thanks for pointing it out people!
8
u/Needleroozer Mar 27 '21
Yeah, but some unknown other person wrote that "%" thing, so who knows what it does? But OP wrote the example code, so he's sure it works.
2
Mar 27 '21
I had to write below to process what the code does
number = 50
number / 2 = 25
number.length = 2
(number / 2).length = 2
number.length < (number / 2).length
falsethe code was actually harder than expected to process. it would be I guess, hard to understand, which is a bad practice, and isn't even checking if it's even.
1
1
u/AutoModerator Mar 27 '21
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/JacksonChen666, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
You can find some examples in the reddit help documentation.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
29
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 26 '21
Image Transcription: Code
if(number.toString().length <
(number/2).toString().length){
return false;
}else{
return true;
}I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
23
16
u/shirleyquirk Mar 27 '21
function isEven(number){return number.toString().length >= (number/2).toString().length;}
isEven(0.1+0.2) //true
isEven(0.3) //falsechecks out
8
8
2
1
3
2
1
u/SpeckyYT sadistic Mar 27 '21
Took me 5 minutes to understand why that code should work. Tbh it was better if I didn't understand that.
1
u/nermid Mar 27 '21
I'll admit it: I had never heard of modulus until I started programming and was asked to do FizzBuzz.
-2
u/SilliestOfGeese Mar 27 '21
Whose ever heard of “who’s”?
2
u/Vercyx Mar 27 '21
ACKSHUALLY, "whose" is possesive, while "who's" is a contraction of "who" and "is"
it's dumb though to be honest, since most people associate
'swith possesion3
0
u/SilliestOfGeese Mar 28 '21
I’m making fun of your title, my man. It should be “who’s” for “who has.” “Whose” is very much incorrect.
-1
-3
u/cphoover Mar 27 '21 edited Apr 24 '21
Why why do people return explicit booleans....
EDIT: Why the downvotes?
The equality operator evaluates to a boolean so you don't need to explicitly return a boolean literal. https://ntgard.medium.com/returning-boolean-literals-is-a-code-smell-7a39531d6b60
Don't you think this is cleaner/easier to read?
const isEven = (x: number): boolean => x % 2 === 0;
64
u/polaris0312 Mar 26 '21
i am super confused, what does this code trying to achieve?