|
Kraus posted:I'm still messing with code golf* and was wondering if anyone had any advice for shrinking this implementation(JavaScript) of FizzBuzz down. Apparently it can be done in 36 fewer characters? I reduced it by 19 chars by moving the loop increment into conditional check, and using an array lookup to select from one of the 4 combos. code:
quote:*I'm not doing it for points on any site, just to become a better programmer.
|
# ? Apr 4, 2019 01:16 |
|
|
# ? Jun 6, 2024 06:02 |
|
Generally codegolf sites give you something shorter than document.write to work with, so there's probably like 10 characters saved there in the number you're citing unless it was explicitly specified this many characters with document.write.
|
# ? Apr 4, 2019 03:00 |
|
roomforthetuna posted:Generally codegolf sites give you something shorter than document.write to work with, so there's probably like 10 characters saved there in the number you're citing unless it was explicitly specified this many characters with document.write. Yeah, the site in question would let you use "print()", and it does the same as "document.write(+"<br>"); Saves 16 characters, but I'm taking that into account. And to everyone dunking on code golf: I would never write actual software this way. I know the difference between writing proper code and having fun. But, as it was pointed out, code golf will absolutely help you learn some esoteric stuff about your language(s), which is largely what I'm after, because yes, more knowledge does make you better. EDIT: minato posted:I reduced it by 19 chars by moving the loop increment into conditional check, and using an array lookup to select from one of the 4 combos. You can knock off another four characters by using ! instead of ==0 for the division! code:
Kraus fucked around with this message at 06:14 on Apr 4, 2019 |
# ? Apr 4, 2019 05:48 |
|
I just noticed you can knock off another 3:code:
|
# ? Apr 4, 2019 07:44 |
|
Kraus posted:Yeah, the site in question would let you use "print()", and it does the same as "document.write(+"<br>"); Saves 16 characters, but I'm taking that into account. Ah rats I was going to point out that document.writeln exists
|
# ? Apr 4, 2019 13:54 |
|
i would be happy if people never saw the !~arr.indexOf(n) trick again
|
# ? Apr 4, 2019 14:27 |
|
I have been spending a couple weeks on a new project and have come to a conclusion: Typescript is really good. That is all.
|
# ? Apr 4, 2019 15:57 |
|
Suspicious Dish posted:i would be happy if people never saw the !~arr.indexOf(n) trick again amen
|
# ? Apr 4, 2019 17:01 |
The Fool posted:I have been spending a couple weeks on a new project and have come to a conclusion: It is! I wish I could convince more people at work to switch to Typescript outside of Angular. Too many Ruby programmers who don't see why types are useful
|
|
# ? Apr 4, 2019 17:30 |
Suspicious Dish posted:i would be happy if people never saw the !~arr.indexOf(n) trick again I'm gonna try to sneak this through a code review at some point, for shits and giggles.
|
|
# ? Apr 4, 2019 17:51 |
|
I wish typescript would allow me to compile to ES5 IIFE. Instead of forcing me to use typescript -> rollup -> buble.
|
# ? Apr 4, 2019 21:48 |
|
There is an npm package that I want to use on my project. However, I want to have it downloaded and added to my own repo so I don't have to worry about it going away or other changes being made. What's the best way to do this? I was going to try doing it by hand, but npm dependency chains are ridiculous.
|
# ? Apr 4, 2019 22:23 |
|
Nolgthorn posted:I wish typescript would allow me to compile to ES5 IIFE. Instead of forcing me to use typescript -> rollup -> buble. The SystemJS exporter is close. I used that for a while.
|
# ? Apr 4, 2019 23:09 |
|
The Fool posted:There is an npm package that I want to use on my project. However, I want to have it downloaded and added to my own repo so I don't have to worry about it going away or other changes being made. Couldn't you just lock the version in your package.json? Afaik you can't remove a package from the npm registry after its been up for 24 hours.
|
# ? Apr 4, 2019 23:24 |
|
FWIW I solved this by finding the github repo of the package in question, forked it, then did npm install --save thefool/forkedrepo
|
# ? Apr 5, 2019 00:46 |
|
Hi, I'm learning react. Whats the correct way to have a form appear when you press a button? My code is really bad right now, but here it is in codesandbox https://codesandbox.io/s/oq4682rym9 Basically the idea is that you have a menu bar up the top and everything you work with underneath. Its using an api supplied by my uni that has a tonne of crime stats for my state. Eventually they are going to be mapped and graphed. Currently I'm trying to make a register form appear when you hit the register button. It appears but when you submit it, chrome says the submission is cancelled because its not connected. Whats the correct way of doing this? E: the uni is pretty unhelpful right now, this stuff isnt in the lecture notes, they are still writing this assignment. This is the first semester they've done react underage at the vape shop fucked around with this message at 03:54 on Apr 5, 2019 |
# ? Apr 5, 2019 03:37 |
|
You need another piece of boolean state, say showMenu, and then render the form conditionally: {showMenu && <regButton/>}. The onClick handler for the button should be () => setShowMenu(!showMenu). You should not be putting elements into state like you are with the offences button. The state should be data, and then the data is used when rendering elements.
|
# ? Apr 5, 2019 03:57 |
|
necrotic posted:You need another piece of boolean state, say showMenu, and then render the form conditionally: {showMenu && <regButton/>}. The onClick handler for the button should be () => setShowMenu(!showMenu). How should I handle when I want to render different thngs? A seperate hook to tell it what to render? Ie render the register form, render the login form (probably reuse the form and set the button/submit handling dependent on state?), and in future the graphing stuff and mapping stuff?
|
# ? Apr 5, 2019 07:30 |
|
underage at the vape shop posted:How should I handle when I want to render different thngs? A seperate hook to tell it what to render? Ie render the register form, render the login form (probably reuse the form and set the button/submit handling dependent on state?), and in future the graphing stuff and mapping stuff? That sounds dangerously like routing to me.
|
# ? Apr 5, 2019 12:48 |
|
underage at the vape shop posted:How should I handle when I want to render different thngs? A seperate hook to tell it what to render? Ie render the register form, render the login form (probably reuse the form and set the button/submit handling dependent on state?), and in future the graphing stuff and mapping stuff? Again, just think about it at the data level, and as quite basic operations on that. You could have flags for each one (though that will get unwieldy), or you can delegate out to another function that decides on what to render based on what the button clicks do, for example: code:
Lumpy posted:That sounds dangerously like routing to me. RobertKerans fucked around with this message at 13:05 on Apr 5, 2019 |
# ? Apr 5, 2019 13:00 |
|
Under what circumstances will importing a JavaScript file as a module trip a CORS error? I converted my class's codebase to use module import statements to bring in all the JavaScript. Suddenly, many of my students are having problems, including with the following error: quote:Access to script at 'file:///Users/Name/Documents/GitHub/a1-githubName/main-scene.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https. We're opening this by visiting localhost, while the .html and .js files are hosted locally using python. One of my TAs added a little bit of python code to patch in corrected MIME types for some people who were having trouble with that on modules, but that hasn't been enough to fix the above error some students are now getting. Help!
|
# ? Apr 6, 2019 07:10 |
|
Dumb Lowtax posted:Under what circumstances will importing a JavaScript file as a module trip a CORS error? You can't load JavaScript from file urls in 2019 (without a dangerous chrome command line option).
|
# ? Apr 6, 2019 08:33 |
|
Good eye, so the two students who have this problem are simply the two who didn't follow directions. They double-clicked index.html after hosting, instead of navigating to localhost:8000. Thank you!
|
# ? Apr 6, 2019 08:37 |
|
Dumb Lowtax posted:Good eye, so the two students who have this problem are simply the two who didn't follow directions. They double-clicked index.html after hosting, instead of navigating to localhost:8000. Thank you! If your students were using vs code they could just hit launch from their IDE and it could launch the python process and chrome automatically.
|
# ? Apr 6, 2019 16:23 |
|
The Fool posted:If your students were using vs code they could just hit launch from their IDE and it could launch the python process and chrome automatically. Yeah at this point it's obvious that I should have gone with that, I even tell them that it's probably better for all purposes. Next time. For now I ran out of preparation time before school started and didn't get to try it myself.
|
# ? Apr 6, 2019 19:46 |
Dumb Lowtax posted:Yeah at this point it's obvious that I should have gone with that, I even tell them that it's probably better for all purposes. Next time. For now I ran out of preparation time before school started and didn't get to try it myself. This is great to read, I was worried you would keep going down this rabbit hole. Kudos, hope the class goes well.
|
|
# ? Apr 8, 2019 08:04 |
|
Someone code golfed a chess ai. I've tried to write a chess ai in javascript and it isn't easy so this is quite an accomplishment while also being an example of why you should never code golf. http://www.nanochess.org/chess4.html code:
|
# ? Apr 8, 2019 10:00 |
|
Dumb Lowtax posted:Yeah at this point it's obvious that I should have gone with that, I even tell them that it's probably better for all purposes. Next time. For now I ran out of preparation time before school started and didn't get to try it myself. Osmosisch posted:This is great to read, I was worried you would keep going down this rabbit hole. Kudos, hope the class goes well. I agree with this person. So often people just stick to their guns or at least don't say they've changed their minds, and it restores my faith in humanity to hear someone at least go "well, you know, you guys have got a point". When someone does this, it always makes me nervous that I'm not saying it out loud when I've changed my mind or learned something enough.
|
# ? Apr 8, 2019 17:41 |
|
I spent the weekend setting up webpack for a react/typescript app by hand, with webpack-dev-server, html-webpack-plugin, and source maps/chrome debugger. It's one of those things that I'd recommend everyone do at least once, and then just continue using CRA then ejecting when ready. Thermopyle posted:I agree with this person.
|
# ? Apr 8, 2019 18:08 |
|
The Fool posted:I spent the weekend setting up webpack for a react/typescript app by hand, with webpack-dev-server, html-webpack-plugin, and source maps/chrome debugger. You know, I spent a weekend doing that I came out with a different lesson: once I knew how it all worked, I just stopped using CRA. In large part because hardly anything changes between projects so I can just copy/paste my base config.
|
# ? Apr 8, 2019 22:52 |
|
Parcel is the poo poo and you should believe the hype and use it. I'm pretty comfortable with Webpack and I pretty much only use it at work because I'm forced to. That said CRA -> Webpack -> Parcel is a nice progression because you learn how poo poo actually works (well, as much as anyone can when it comes to bundlers anyway) and can make an informed decision about what tools to use instead of throwing the same boilerplate at every project.
Anony Mouse fucked around with this message at 03:55 on Apr 9, 2019 |
# ? Apr 9, 2019 03:52 |
|
I will second the Parcel recommendation, because it just automatically does the right thing for most files without having to screw around with loaders, plus hacky but consistent syntax for getting the raw string contents of any file embedded without passing through loader/plugin stuff. The one problem is their incredibly stupid thing where it auto-installs dependencies by default.
|
# ? Apr 10, 2019 03:27 |
|
I don't use Parcel, but I back the recommendation anyway, because unless you want to do some wacky combination like typescript with dynamic module loading it's way less of a headache than the alternatives.
|
# ? Apr 10, 2019 03:35 |
|
I have an html table with a set of key:value pairs for tags. I want to reference certain values in another function. The problem is the order and amount of tags in the table is going to vary so I can't do a document.getElementById("myvalue").value = this.cells[3].innerHTML; as I was before since I don't know when that value is going to show up in the row. I'm thinking I put in an if statement something inside of the loop. Something to the effect of: code:
|
# ? Apr 10, 2019 19:33 |
|
Why are you getting data out of your HTML? Can you store your data in an object? Then both generate your table from that and reference it in your other function easier.
|
# ? Apr 10, 2019 19:37 |
|
The Fool posted:Why are you getting data out of your HTML? I suppose I could. The idea is that the table has a list of objects data and the user and perform actions based on the selection they make on the table.
|
# ? Apr 10, 2019 23:17 |
|
I know they've got special attributes you can add to an HTML tag that begin with "data-", which automatically add that string as a data member to the DOM element (HTML tag) so that you can see it from JavaScript -- think of it as a way HTML structures can communicate information to JavaScript. However, communication to go that direction at all might be kind of a code smell, or an indicator that your approach is backwards. For example, I had one of these "data-" attributes in my infamous "code navigator" upthread, to help it remember which source code it had populated a text box with. One day I refactored my code to be more encapsulated and suddenly this awkward thing I thought I had no other choice to use was no longer needed.
|
# ? Apr 10, 2019 23:31 |
|
If you've got some digital clocks on your page. You've got javascript that loops though all the ".clock" elements and starts updating them every second that passes, but you want them in different timezones you might set "data-timezone" attributes so that in your javascript you just have to look at the element's "data.timezone". It's super useful if your entire page isn't js.
|
# ? Apr 10, 2019 23:58 |
|
Nolgthorn posted:If you've got some digital clocks on your page. You've got javascript that loops though all the ".clock" elements and starts updating them every second that passes, but you want them in different timezones you might set "data-timezone" attributes so that in your javascript you just have to look at the element's "data.timezone".
|
# ? Apr 11, 2019 00:31 |
|
|
# ? Jun 6, 2024 06:02 |
|
SnatchRabbit posted:I suppose I could. The idea is that the table has a list of objects data and the user and perform actions based on the selection they make on the table. You should be (re)generating the table from the authoritative data set, not trying to reverse-engineer stuff out of the page HTML..
|
# ? Apr 11, 2019 03:36 |