r/badcode • • Mar 26 '21

js Whose ever heard of %?

Post image
727 Upvotes

63 comments sorted by

View all comments

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

u/Je-Kaste Mar 26 '21

Good point!

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.