|
Try wrapping the last bit in parentheses. Ternary and binary or have different presedence rules.
|
# ? Jan 30, 2019 02:07 |
|
|
# ? Jun 1, 2024 05:04 |
|
Kraus posted:
Because it's a syntax error. Put the document.getElementById("success").style.display = "block" inside parenthesis and it should work. e:;b
|
# ? Jan 30, 2019 02:08 |
|
quote:For some reason, this simply doesn't work. If you replace the "success" part with something like alert("success"), this works just fine. I, for the life of me, have no idea why Javascript doesn't like the version with the getElementById. Anyone know why? Debugger says: Invalid left hand sign in assignment. I think it's doing: code:
|
# ? Jan 30, 2019 02:12 |
|
I'm not someone who regularly writes inexplicit javascript but I imagine `&&` has as much to do with it. Javascript is trying to form a boolean and is likely optimising your assignment out of the operation.
|
# ? Jan 30, 2019 02:18 |
|
necrotic posted:Try wrapping the last bit in parentheses. Ternary and binary or have different presedence rules. Ah, yeah, that totally did it! Thanks! What's funny, for golf purposes, these solutions are only better than the other depending on if you're counting spaces or not. Ternary: 142 counting spaces, 132 not. Or: 141 counting spaces, 133, not.
|
# ? Jan 30, 2019 02:41 |
|
Nolgthorn posted:I'm not someone who regularly writes inexplicit javascript but I imagine `&&` has as much to do with it. Javascript is trying to form a boolean and is likely optimising your assignment out of the operation. given "a ? b : c" code:
b is 0 c is document.getElementById("success").style.display = "block"; given "a = b" code:
b is 'block' you cannot assign a value to a statement. AND and OR have highest precedent, conditional statement has a higher precedence than assignment operator
|
# ? Jan 30, 2019 02:43 |
|
Strong Sauce posted:AND and OR have highest precedent, conditional statement has a higher precedence than assignment operator This makes total sense for why the parentheses are required! Thanks for the theoretical reason!
|
# ? Jan 30, 2019 02:50 |
|
Kraus posted:Ah, yeah, that totally did it! Thanks! For real golf improvement you need to move some of the longer stuff out into variables and use a efficiently-named function to find elements, e.g.: code:
|
# ? Jan 30, 2019 03:16 |
|
biznatchio posted:For real golf improvement you need to move some of the longer stuff out into variables and use a efficiently-named function to find elements, e.g.: Wow. I need to up my game, severely.
|
# ? Jan 30, 2019 04:09 |
|
You don't even have to set style.display to anything, save 14 chars withcode:
|
# ? Jan 30, 2019 04:14 |
|
minato posted:You don't even have to set style.display to anything, save 14 chars with So, you're basically deleting all its style information and it defaults to displaying.
|
# ? Jan 30, 2019 04:21 |
|
You guys please consider using uglify-js and writing normal code for us garbage humans.
|
# ? Jan 30, 2019 11:29 |
|
Oh god, this thread has gotten me more into code golfing in JS, and I found a site (https://code-golf.io/) that offered some challenges. I decided to tackle the Fibonacci challenge, where you have to get the Fibonacci sequence to display from 0 to 832,040, each on their own line. I managed to come up with this 71-character solution: code:
|
# ? Jan 30, 2019 15:53 |
|
There's a way of generating fib(n) without generating fib(n-1), i.e. in O(1) time. It's a very good tool for looking in interviews. Here's some snackoverflow code I copied, but it could easily be code-golfed to something much smaller.code:
|
# ? Jan 30, 2019 17:03 |
|
Kraus posted:But, the leaderboards on that site claim that it can be done in as little as 33 characters. Does anyone have any idea how?
|
# ? Jan 30, 2019 17:44 |
|
Kraus posted:Oh god, this thread has gotten me more into code golfing in JS, and I found a site (https://code-golf.io/) that offered some challenges. I decided to tackle the Fibonacci challenge, where you have to get the Fibonacci sequence to display from 0 to 832,040, each on their own line. You can save a lot of characters by using print(...) to output with a newline instead of document.write(... + '<br>'). This isn't standard JavaScript but it's in the instructions so it works for me. Declaring and indexing that array is costly since we only need the two most recent entries (and I think a third variable to use as a temporary variable while we step forward...) Also, cunningly the last entry requested is the last entry before Fibonacci numbers pass a million, which has a very small representation in JavaScript. So, here's what I have currently: JavaScript code:
|
# ? Jan 30, 2019 18:37 |
|
Doom Mathematic posted:
And then save some chars with the recursion effect: JavaScript code:
|
# ? Jan 30, 2019 18:57 |
|
Uh, all print did was repeatedly call a dialogue box for physically printing things.
|
# ? Jan 30, 2019 19:11 |
|
Kraus posted:Uh, all print did was repeatedly call a dialogue box for physically printing things. I think you're running your code in your browser console. Try running on https://code-golf.io/fizz-buzz#javascript itself like you mentioned.
|
# ? Jan 30, 2019 19:16 |
|
Doom Mathematic posted:I think you're running your code in your browser console. Try running on https://code-golf.io/fizz-buzz#javascript itself like you mentioned. Okay. Hmm. That seems a little janky if it doesn't always work, and is just environment-specific. Oh well. Edit: What I'm saying is that if you wanted to run this code outside of that environment, you'd end up having to write 16 characters more, since "print()" does something else outside of that environment. That means the record is a stealth 49, unless y'all know another way to get stuff to appear on screen, formatted that way. Kraus fucked around with this message at 19:39 on Jan 30, 2019 |
# ? Jan 30, 2019 19:21 |
|
Kraus posted:Okay. Hmm. That seems a little janky if it doesn't always work, and is just environment-specific. Oh well. Yeah, JavaScript environments differ from one another. Not all of them are web browsers. The challenge as stated doesn't require the code to run outside of the environment it provides, which, by the way, isn't a web browser, document.write doesn't work there.
|
# ? Jan 30, 2019 21:22 |
|
What I prefer is that the code doesn't work as long as I can reserve 16 characters. In fact typing this sentence is making my hands sweat.
|
# ? Jan 31, 2019 02:47 |
|
Thanks for sucking up my entire afternoon and evening with code golf.
|
# ? Jan 31, 2019 05:54 |
|
A friend of mine managed to bring it down to 36 characters:code:
|
# ? Feb 1, 2019 01:41 |
|
Kraus posted:A friend of mine managed to bring it down to 36 characters: You should be able to avoid that “b=0” setup.
|
# ? Feb 2, 2019 04:39 |
|
MrMoo posted:You should be able to avoid that “b=0” setup. you cannot compare a variable to something before it has been defined. you will get a reference error when the engine gets to b<9e5.
|
# ? Feb 2, 2019 04:53 |
|
that's the challenge, yes.
|
# ? Feb 2, 2019 04:58 |
|
minato posted:You don't even have to set style.display to anything, save 14 chars with Huh, I didn't know that worked. Turns out you can do code:
|
# ? Feb 2, 2019 06:30 |
|
You can also save a single char with:code:
|
# ? Feb 2, 2019 07:23 |
|
minato posted:You can also save a single char with: In that case why not just code:
|
# ? Feb 2, 2019 07:33 |
|
Oh nice. Didn't realize that was the only element with an id in the page. So that saves 4 chars.
|
# ? Feb 2, 2019 07:41 |
|
My take:code:
Anony Mouse fucked around with this message at 07:49 on Feb 2, 2019 |
# ? Feb 2, 2019 07:45 |
|
Roadie posted:Don't forget the true galaxy brain approach: I actually saw some similar code yesterday in a package we are using. code:
What the heck?
|
# ? Feb 2, 2019 16:54 |
|
smackfu posted:I actually saw some similar code yesterday in a package we are using. That's an extremely common way to clone a JavaScript object. It can be very lossy but it's also surprisingly fast. It also avoids pulling in a third-party deep cloning library whose behaviour with respect to e.g. circular objects is less well-understood than JSON.parse and JSON.stringify, which are universally known quantities. Of course, in most cases it should not be necessary to completely clone an object in this way... for example, in this case const improvedSpec = { ...inputSpec } would do the same job much faster still...
|
# ? Feb 2, 2019 17:56 |
|
Interesting, I figured they were using stringify to validate the user-provided JSON but couldn’t figure out why they were then parsing and using the result.
|
# ? Feb 2, 2019 18:04 |
|
Anony Mouse posted:My take: Whoa. So combining that with biznatchio's realization you can just do .style++, and cutting all the class, ID, and variable names down to one character each, that gives us a 55 character solution: code:
|
# ? Feb 2, 2019 18:07 |
|
smackfu posted:Interesting, I figured they were using stringify to validate the user-provided JSON but couldn’t figure out why they were then parsing and using the result. i've definitely made a utility method "copy" and stuck that JSON stringify/JSON parse in there. javascript is always pass by value, but if a variable return to an object, the "value" is a reference to the object - this means if you do something like slicing an array, and modify a property of the element in the sliced array, the element in the original array will also be modified, and you can stop that from happening by doing this.
|
# ? Feb 2, 2019 18:20 |
|
Bruegels Fuckbooks posted:i've definitely made a utility method "copy" and stuck that JSON stringify/JSON parse in there. javascript is always pass by value, but if a variable return to an object, the "value" is a reference to the object - this means if you do something like slicing an array, and modify a property of the element in the sliced array, the element in the original array will also be modified, and you can stop that from happening by doing this. no slice makes a copy of the array code:
code:
|
# ? Feb 2, 2019 21:42 |
|
code:
necrotic fucked around with this message at 22:37 on Feb 2, 2019 |
# ? Feb 2, 2019 22:35 |
|
|
# ? Jun 1, 2024 05:04 |
|
I feel like if you've got a good background on pointers and references from a C++ background or a quality CS intro class you just don't ever have that misunderstanding
|
# ? Feb 2, 2019 22:56 |