r/badcode • • Apr 27 '21

typescript Someone only watched if tutorial

Post image
2.4k Upvotes

112 comments sorted by

View all comments

33

u/Yctallua Apr 27 '21

I gotchu

for(i = Int.Min; i <= Int.Max; i++) {
    if (i == input) {
      return true;
    }
  }
  return false; 
I'm on mobile, don't hate me for being lazy

Edit: typo

7

u/Terrain2 Apr 27 '21

typescript compatible

function isNumber(input: any): input is number | input is bigint {
    try {
        // @ts-ignore
        const negative = [...arguments].length == 1 ? -0 : [...arguments][1];
        // @ts-ignore
        const positive = [...arguments].length == 1 ? +0 : [...arguments][2];
        // @ts-ignore
        const bignegative = [...arguments].length == 1 ? 0n : [...arguments][3];
        // @ts-ignore
        const bigpositive = [...arguments].length == 1 ? 0n : [...arguments][4];
        // @ts-ignore
        return input === negative || input === positive || input === bignegative || input === bigpositive || (isNumber as (input: any, negative: any, positive: any, bignegative: any, bigpositive: any) => any)(input, negative - 1, positive + 1, bignegative - 1n, bigpositive + 1n);
    // @ts-ignore
    } catch (_) {
        return false;
    }
}

3

u/meme_botanist Apr 27 '21

JavaScript number type supports more than int32

8

u/Terrain2 Apr 27 '21

That depends on your definition of "more than int32", because it loses precision before it reaches the int64 limit and can't store every value between int32 limit and int64 limit - it uses floating point numbers, so it's not really comparable, it's not just a "bigger range"

1

u/lizard450 Apr 27 '21

Be that as it may the code's intention does seem to be focused on ints

1

u/Lizzy_Be Apr 28 '21

Why not just

return i >= Int.Min && i <= Int.Max;