r/badcode • u/bbb651 • Oct 19 '19
js Getting the ASCII code of a character doesn't get easier than this.
17
u/nobody912 Oct 19 '19
Can I get a Unicode version?
11
u/bbb651 Oct 20 '19
2
u/nobody912 Oct 20 '19
I don't believe it. Absolute madlad.
3
u/Octribin Oct 20 '19
Probably (hopefully!) generated rather than written by hand. But still 😂
6
u/bbb651 Oct 22 '19 edited Oct 22 '19
Yep, I made this script (for Node.js):
const fs = require("fs"); const highNumber = 1000000; let out = ""; for (i = 0; i < highNumber; i++) { out += ` if (char === ${String.fromCharCode(96)}${String.fromCharCode(i)}${String.fromCharCode(96)}) { return ${i}; } `; } fs.writeFile("C:/Documents/output.js", out, function(err) { if (err) { return console.log(err); } console.log("The file was saved!"); });And edited the ` character to have normal quotes. Now that I think about it I could have used [\`] insted of [${String.fromCharCode(96)}].
I actually learned a lot from it about the file module in Node.js and working with tremendously big files.
Edit: Reddit messed up the formatting because of the `, switched to square brackets instead.
2
u/wibblewafs Oct 20 '19
I'll keep you posted when I've got the BMP covered. I'm still a quarter of the way through implementing support for the non-latin european scripts right now.
32
u/somehowyellow Oct 19 '19
Hey OP can I get a copy of the function? I need that in a project and I don't want to type everything out
10
u/bbb651 Oct 19 '19 edited Oct 20 '19
JavaScript has a
functionmethod exactly for that1
u/warmike_1 Oct 20 '19
VB has a function too. Switched to C++ recently and miss string functions so much.
2
Nov 15 '19
C++ has a function for that.
char to_ascii(char c) { return c; }1
u/warmike_1 Nov 15 '19
I tried outputing with an int variable a few days ago and it worked as well. Thanks anyway.
10
36
u/lucidspoon Oct 19 '19
The fuck is up with the indentation?!?
21
u/thechopperhopper Oct 19 '19
I think it's two screenshots glued together.
21
3
u/rift95 Oct 19 '19
I'm assuming the dev generated this file. So the indentations are probably just a small indentation error in the first "if", that then got exaggerated over time.
18
u/Risc12 Oct 19 '19
For those who don’t know why the shown code is a bad idea:
return char.charCodeAt()
8
Oct 20 '19
Can't you just convert to int?
4
u/Risc12 Oct 20 '19
Nope, parseInt will try and get the numerical value of the string:
parseInt(“1”) // -> 1
Otherwise it will return NaN:
parseInt(“a”) // -> NaN
I believe in Java you are right about the behaviour of parseInt.
1
Oct 20 '19
Oh, right. I program mostly in C and Python. In Python it's a simple conversion, and in C if you add 2 chars they actually add the ascii values and become a new char.
2
u/Risc12 Oct 20 '19
Ah I see! Javascript has no char-type, no integers either actually. Just strings and numbers.
1
u/rhen_var Oct 25 '19
In C chars are just 1 byte integers so you can cast them to an int/add them together/whatever
2
1
u/jlamothe Oct 20 '19
Indentation aside, why are they using backticks instead of apostrophes? That code shouldn't even parse.
3
4
u/Ronaldarndt Oct 20 '19
Its javascript. Backticks are valid
1
u/jlamothe Oct 20 '19
TIL
1
u/sreyemnayr Nov 01 '19
Backticks are the shit. Allows for newlines within strings and no need to escape quotes or apostrophes.
3
u/t4ct1cx Oct 20 '19
They're missing about 128 characters. I see why it's bad. 😤 SMH how am I suppose to get the code for those letters with this.
1
1
1
1
0
Oct 19 '19
[deleted]
81
u/regibalbo Oct 19 '19
Maybe having a list with the chars and using indexOf, that's way shorter
28
u/ruzi00 Oct 19 '19
"List with the chars" can be just a regular string.
19
u/gmtime Oct 19 '19
Actually no, the first character is '\0', which terminates the string.
30
9
u/lorlen47 Oct 20 '19
This is JS, and JS (and almost all modern programming languages) don't use null terminator to indicate end of the string, they save the length alongside string data. So using \0 as part of the string is fine.
1
0
5
u/EkskiuTwentyTwo i -= (i - (-1)) - i Oct 24 '19
Or an object-based selection:
const getASCII = char => {"NUL":0,"SOH":1,"STX":2,...,"A":65,"B":66,..." ":127}[char];Now it's only one line. Much more efficient.
89
u/bbb651 Oct 19 '19
Found in the wild on a coding challenges website.
For context, this is probably satire (and generated using a script) as it was written it was written by a experienced developer, but it's the most horrendous code I've ever seen.