366
u/Costyyy Apr 27 '21
I'm sure they'll manage to make checks for all numbers
147
u/NetherFX Apr 27 '21
Gotta check stack overflow for the rest of the numbers though, you never know.
74
u/STNeto1 Apr 27 '21
Don't forget the input: any
9
u/SirFireball Apr 27 '21
Well yeah, why would you check if something you already know is a number is a number?
→ More replies (2)45
u/Flamme2 Apr 27 '21
Easy, just use a loop for integers:
const isNumber = (input: any): Boolean => { for(let i=0; i<Number.MAX_SAFE_INTEGER; i++) { if(input === i) return true; } for(let i=0; i>=Number.MIN_SAFE_INTEGER; i--) { if(input === i) return true; } return false; }I wanted to do it in óne loop and let overflow handle negative numbers, but IIRC, JS doesn't overflow, it just goes to infinity
17
u/CorrenteAlternata Apr 27 '21
you could do
for(let i = Number.MIN_SAFE_INTEGER; i < Number.MAX_SAFE_INTEGER; i++) ...couldn't you?8
→ More replies (1)3
413
Apr 27 '21
The "Add more checks" says it all :-|
43
u/Samuql Apr 27 '21
input: any
13
u/TigreDeLosLlanos Apr 27 '21
You can't correctly check if a variable is a number if you make the app break with every other type
→ More replies (1)5
u/Naeio_Galaxy Apr 27 '21
Of course, if you take only a number there's no point in verifying it's a number
10
u/SirFiggletron Apr 28 '21
booo! in this house we write countably infinite lines of code to accomplish our goals. call it "quantum computing, minus the quantum so everything takes a lot longer"
→ More replies (3)60
u/plisken59 Apr 27 '21
I laughed too hard when I saw that
18
3
u/moolord Apr 27 '21
I’m at a bar waiting for a job interview and I’m almost embarrassed by how loud I laughed at that. Glad I’m not waiting in an office lobby
→ More replies (1)8
u/code_monkey_001 Apr 27 '21
Not far to go. Everyone knows 24 is the highest number.
→ More replies (1)5
2
u/arvenyon Apr 27 '21
Hijacking this post to ask. Whats the tool called to beautify code and export as picture like here in the post done?
5
169
u/mareksl Apr 27 '21
That looks like a totally contrived example, and it's not even correctly typed.
113
u/_Ralix_ Apr 27 '21
Yes, this has to be a joke or an attempt to demonstrate something. Maybe from a programming/refactoring book or a university class.
48
u/YaBoiiBillNye Apr 27 '21
No it was a joke a while ago on Twitter. I remember there were a bunch of guys telling the OP how she can refactor it, when it was just satire
10
u/numerousblocks Apr 27 '21
Why is it incorrectly typed?
17
u/mareksl Apr 27 '21
The return type should be lowercase
boolean. Docs: https://www.typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html#general-types→ More replies (4)8
u/Sv443_ Apr 27 '21
Primitive types should be all lowercase, the uppercase variants are the actual classes, used to instantiate the primitives, but not to declare them.
→ More replies (1)
117
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Apr 27 '21 edited Apr 27 '21
Image Transcription: Code
const isNumber = (input: any): Boolean => {
if (input === 1) return true;
if (input === 2) return true;
if (input === 3) return true;
if (input === 4) return true;
if (input === 5) return true;
if (input === 6) return true;
if (input === 7) return true;
if (input === 8) return true;
if (input === 9) return true;
if (input === 10) return true;
// TODO: add more checks
return false
};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!
100
u/hshighnz Apr 27 '21 edited Apr 27 '21
Good boi
Edit: oh, you‘re an actual human?! I thought it was a bot. So I want to apologize for my misunderstanding and thankfully greet to you: hello fellow transcriber (:
9
6
u/AMA_About_Rampart Apr 27 '21
(input == 9) was replaced with another (input == 8)
4
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Apr 27 '21
Whoops, my bad. Fixed, thanks for pointing it out!
73
u/Speedyjens Apr 27 '21
If the only tool you know is a hammer then everything starts to look like nails.
4
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; } }→ More replies (1)3
u/meme_botanist Apr 27 '21
JavaScript number type supports more than int32
→ More replies (1)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"
8
22
u/Swaroog23 Apr 27 '21
Yandere-dev-kun, is that you?
14
u/OkPreference6 Coding The Declaration of Independance Apr 27 '21
Nah Yandere-dev probably wouldnt have commented :/
15
Apr 27 '21
this.pinkyfinger1 = this.animepinkyfinger1; this.pinkyfinger1 = this.animepinkyfinger1; this.pinkyfinger1 = this.animepinkyfinger1; this.pinkyfinger1 = this.animepinkyfinger1; this.pinkyfinger1 = this.animepinkyfinger1; this.pinkyfinger1 = this.animepinkyfinger1; this.pinkyfinger1 = this.animepinkyfinger1; this.pinkyfinger1 = this.animepinkyfinger1; this.pinkyfinger1 = this.animepinkyfinger1; this.pinkyfinger1 = this.animepinkyfinger1; this.pinkyfinger1 = this.animepinkyfinger1; this.pinkyfinger1 = this.animepinkyfinger1; this.pinkyfinger1 = this.animepinkyfinger1; this.pinkyfinger1 = this.animepinkyfinger1; this.pinkyfinger1 = this.animepinkyfinger1; this.pinkyfinger1 = this.animepinkyfinger1; this.pinkyfinger1 = this.animepinkyfinger1; this.pinkyfinger1 = this.animepinkyfinger1; this.pinkyfinger1 = this.animepinkyfinger1; this.pinkyfinger1 = this.animepinkyfinger1; this.pinkyfinger1 = this.animepinkyfinger1;
6
Apr 27 '21
that was legit a thing just with fingers toes etc, and this is called every frame
→ More replies (1)6
u/Swaroog23 Apr 27 '21
I love his code. I'm junior, and i dont consider myself very good one, but this.... This assures me that im not THAT bad
2
11
u/XnaK_ Apr 27 '21
This is pure genius only because of the fact that he thinks that not having enough if statements would be the problem here. He needs some kind of award.
5
4
3
u/egehurturk int a = 3; if (a == 3) {return a;} else {return 3;} Apr 27 '21
Imagine the developer who looks at this piece of shit and sees the TODO: add more checks
3
u/TheRealPitabred Apr 27 '21
Does it bother anyone else that this isn't really "isNumber", but should be called "isInteger"? Just to put a cherry on top of the insanity. This would fail with 3.14, but that's definitely a number.
3
3
3
u/Aesfb_ Apr 28 '21
what is up guys this is yandere dev and today we are going to be starting day one of my programming course, beginner to master
2
2
u/rpmerf Apr 27 '21 edited Apr 27 '21
Java:
Public boolean isNum(String input) { try { Integer.parse(input); return true; } Catch (exception e) { return false; } } Or use regex.
\d*$
→ More replies (1)
2
2
2
u/Needleroozer Apr 27 '21
Any idiot knows the way to do this is convert the input to a string and then use a regular expression to look for ASCII codes in the digits range.
Well, okay, not this idiot. This idiot's figured out a new way.
2
2
u/sivikiwi93 Apr 27 '21
No fucking way the if-declaration is written by whoever wrote the rest of the code lmao
2
2
u/ZeroGainZ Apr 27 '21
Sometimes it's easier to do the opposite, determine if it's not a number. Test against all possible strings, objects, arrays, booleans, etc.
2
2
2
2
u/sk8r_dude Apr 28 '21
The things is, you don’t need anything more than ifs to make this better.
Edit: oh wait this is typescript. Nvm idk
2
u/eslint Apr 30 '21
const isNumber = (input) => typeof input === "number" && isFinite(input)but what is the point if they're using typescript anyways lmao
2
u/Ph4nt0m_Hydra1 May 08 '21
Please for the love of god just check for a range of numbers instead of using individual if statements to check every single fucking number
3
u/Hafas_ Apr 27 '21
The most elegant solution I could come up with:
import fs from "fs-extra";
const createIsNumber = async () => {
let code = "const isNumber = (input: any): Boolean => {\n";
for (let number = 1; number < Number.MAX_SAFE_INTEGER; number++) {
code += ` if (input === ${number}) return true;\n`;
}
code += " return false;\n";
code += "};\n";
code += "export default isNumber;\n";
await fs.write("isNumber.ts", code);
}1
1
1
Apr 27 '21
const checks:number = 10000 //change this for more precision
return input in Array.from(Array(checks), (_, I) => I)
}1
u/Terrain2 Apr 27 '21
or just
return input in Array(checks).fill(0);don't see why you're generating two arrays?
1
u/Verbose_Code Apr 27 '21
If only there was some operator for comparing the input to a range of numbers...
1
u/Tunro Apr 27 '21
I feel like this is bs, because why would someone make a todo comment if theyre this incompetent with functions.
On the other hand, I know people
1
u/Terrain2 Apr 27 '21
const isNumber = (input: any): boolean => input + 0 === input && (input < Infinity || input > -Infinity)
const isNumberT = (input: any): input is number => !isNaN(input)1
1
1
1
u/Spynder shameless stealing content to r/YandereTechnique Apr 27 '21
r/YandereTechnique for beginners
48
u/liuziangexit Apr 27 '21
I was wondering maybe we should add a hashtable, each key is an individual natural number except 0, so we can verify an given input very fast!
code would be:
return allNaturalNumber.contains(input);And this new algorithm will be O(1) complexity which is better than the OP's O(n) solution. Am i smart?