r/badcode • • Mar 26 '21

js Whose ever heard of %?

Post image
727 Upvotes

63 comments sorted by

64

u/polaris0312 Mar 26 '21

i am super confused, what does this code trying to achieve?

81

u/Vercyx Mar 26 '21

it determines whether a number is even or odd in a very.... interesting way

67

u/polaris0312 Mar 26 '21

ah i see, because if its an odd number then it will has decimal points when divided by 2, hence original length < divived by 2 length

definitely confusing lol

35

u/orokro Mar 27 '21

"10" is 2 long

10/2 is 5

"5" is one long.

10 is odd

7

u/not_noobie Mar 27 '21

Na . If it returns false it means number is EVEN.

But let's see for 1001

1001 is 4 long

1001/2 is 500.5

500.5 is also 4 long

4< 4 returns false.

1001 is even?

20

u/DaveDuck2 Mar 27 '21

Code works as intended: "500.5" is 5 long

12

u/not_noobie Mar 27 '21

Oh shit yes. Not my smartest moment :(

9

u/jarfil Mar 27 '21 edited May 12 '21

CENSORED

-31

u/veryusedrname Mar 26 '21

Calling this code confusing is... Confusing.

22

u/RFC793 Mar 27 '21

It is confusing. If number is an integer type, then this code doesn’t work at all.

14

u/aj-ric Mar 27 '21 edited Mar 27 '21

Depending on the language. I'm guessing this is javascript which doesn't have a concept of an integer.

Edit: but you're right, it's very confusing to read since it's hard to tell whether integer division is happening.

6

u/mfb- Mar 27 '21

it's very confusing to read since it's hard to tell whether integer division is happening.

Or, you know, the whole idea of doing number checks with strings.

1

u/RFC793 Mar 27 '21

Ah, good point. I thought it could be Java, but then length would have to be a function and not an attribute. It’s been about 7 years since I’ve regularly programmed in either.

1

u/Needleroozer Mar 27 '21

And if it's a real type it doesn't work at all. It's successfully failed every test so far.

10

u/Poddster Mar 27 '21

In this language does number / 2 perform floating point division?

edit: Ah, its javascript.

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

u/jarfil Mar 27 '21 edited May 12 '21

CENSORED

2

u/F54280 Mar 27 '21

parity is a property of integers, so an isEven function should return false for a non-integer, as should isOdd.

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 isOdd is 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

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.

2

u/[deleted] 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

false

the 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

u/Vercyx Mar 27 '21

that would return true, not false, due to the use of < and not <=

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

u/Kyyken Mar 26 '21

good human

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) //false

checks out

8

u/[deleted] Mar 27 '21

This makes me made because it does it floating point math. !($value & 0x1), duh! :P

8

u/datguygomez Mar 27 '21

Uhhh points for creativity?

2

u/[deleted] Mar 27 '21

This is Js right?

1

u/CarterNotSteve Mar 27 '21

What language

1

u/Vercyx Mar 27 '21

Javascript

3

u/Sane-Pai Mar 27 '21

This is genius in its own way LMAO.

2

u/oakskog Mar 27 '21

Beautiful

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 's with possesion

3

u/jarfil Mar 27 '21 edited May 12 '21

CENSORED

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

u/[deleted] Mar 27 '21

This would never return true...

-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;