Ultimately I would still avoid for/in There's a few other options available if you really hate traditional for loops... https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach http://underscorejs.org/#each etc. But honestly, you should just get used to them because they really aren't that bad.
|
|
# ? May 25, 2014 15:35 |
|
|
# ? Jun 5, 2024 04:08 |
|
Dominoes posted:Looking for thoughts on 'for in' loops in javascript, and this Stack Overlow question on the topic. The accepted answer is that the order of a for...in loop is also undefined in the standard. it's implementation specific what order the loop executes.
|
# ? May 25, 2014 15:38 |
|
If you have an array and not an object, just use forEach.
|
# ? May 25, 2014 15:46 |
|
ES6 has a for (var i of myArray) that works as expected.
|
# ? May 25, 2014 15:49 |
|
The more expensive part is usually tracking enumeration state, including prototype traversal and duplicate suppression; there are tricks that make string-of-int cheap. It'll likely be slower in a microbenchmark, but I doubt it'll show up in anything real. The bigger issue is that for-in isn't guaranteed to be in index order (though practically anything later than IE8 will, I *think*) and as a lesser concern if the array has additional properties on it they'll show up as well.
|
# ? May 26, 2014 04:37 |
|
I'm currently reflecting on a solution to my first real challenge in a new project I'm working on. I have an API endpoint that is spitting out a JSON blob which changes every 5 minutes. I want to build a simple front-end that does the following:
My current plan is to use jQuery to poll the whole feed with .getJSON() and .setInterval(), then perform a whole bunch of chained jQuery methods on the various HTML elements. Can anyone recommend a better way to do this?
|
# ? May 26, 2014 07:03 |
|
Nope, that's pretty much it.
|
# ? May 26, 2014 11:33 |
|
If it's a simple page and unlikely to change, that could be good enough. Otherwise you might want to check out some framework that automates some of that.
|
# ? May 26, 2014 11:53 |
|
neurotech posted:I'm currently reflecting on a solution to my first real challenge in a new project I'm working on. Maybe use websockets for this bit? If this is a "for fun" project it might be a neat technology to try out. It might eliminate the getJSON and setInterval parts of your code. ninjaEdit: actually, mdn says mdn posted:WebSockets is an advanced technology that makes it possible to open an interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply. Which sounds like what you're trying to do?
|
# ? May 26, 2014 14:44 |
|
Server sent events would be nicer for unidirectional communication, but JSON over XHR is the most convenient.
|
# ? May 26, 2014 15:19 |
|
Anyone else headed to JSConf this week?
|
# ? May 27, 2014 01:04 |
|
I have a problem that I've been thinking of and I want to know the best way to solve it using good javascript. Let's say you have objects A, B, and C, and they have a hierarchichal relationship like so: A has A list of B's B has A list of C's code:
If creating B and C was sync then we just have to imperatively create each object and call the callback at the end. But what if creating B and C is async? Then how do we create an A and run operations when all the B's are done when the callback functions to do the asynchronous operations will be created and control flow will immediately return to the calling function? We can't just pass a callback to B since the callback would be called multiple times (unless we implement a control to only call the callback when the last B is called which is what I've done so far), unless we change up the entire construction of the flow of this process to have the callbacks able to be called multiple times. We could use something like the async or promises modules in Node but what is the best way of doing this without extra modules, unless we just create those modules ourselves and use them basically? A basic synchronous version would be like so: code:
|
# ? May 27, 2014 06:56 |
|
More of a meta-question here. I've made my first forays into Git, and I love it. However I don't see the need for using the Git shell when the windows GUI suits my purposes just fine (javascript/php). Am I missing out on something important ignoring the shell for now? I assume I'll need to use it when I get to Ruby, but for now it doesn't seem helpful.
|
# ? May 27, 2014 16:06 |
|
Raskolnikov2089 posted:More of a meta-question here. I've made my first forays into Git, and I love it. You'll probably be fine as long as you only need basic committing, pulling, pushing, diffing and merging. If you're using it in a one-man project, you won't necessarily Wheany fucked around with this message at 20:45 on May 27, 2014 |
# ? May 27, 2014 16:52 |
|
Re: git GUI clients for Windows or Mac, you can grab Sourcetree by Atlassian because it's awesome.
|
# ? May 27, 2014 17:14 |
|
piratepilates posted:But what if creating B and C is async? Then how do we create an A and run operations when all the B's are done when the callback functions to do the asynchronous operations will be created and control flow will immediately return to the calling function? That's basically what you do. You're basically going to do the loops recursively, instead of iteratively. A synchronous recursive version looks roughly like: JavaScript code:
code:
Subjunctive fucked around with this message at 17:54 on May 27, 2014 |
# ? May 27, 2014 17:51 |
|
Skiant posted:Re: git GUI clients for Windows or Mac, you can grab Sourcetree by Atlassian because it's awesome. Just wanted to re-iterate this - it's a great program and really should be all you need. Integrates well with already installed versions of Git, and has one-click buttons for initiating Git Workflow (and using its various functionality) all without needing to know the console commands. They do help though, especially for more involved operations like submodules.
|
# ? May 27, 2014 19:43 |
|
quote:A bunch of useful feedback Thanks for the responses everyone. I tooled around with socket.io a few months back and it seemed promising. I think I'll spend my saturday trying it out.
|
# ? May 28, 2014 06:50 |
|
neurotech posted:I'm currently reflecting on a solution to my first real challenge in a new project I'm working on.
|
# ? May 28, 2014 06:59 |
|
That's quite nice, a jQuery layer replicating web animations. Wonder what the performance is like at larger scale with the chaining mechanism. Using RAF tends to work better than CSS transitions though if you want 60fps.
|
# ? May 28, 2014 15:16 |
|
Hi. I'm an IT student working on his first programming project. It's all JQuery and JS. Unlike most students I don't have a partner, so I'm having a specially hard time working this out, specially now at the beginning. It's a simple game of Simon, where two players play against each other. They provide all of the graphics, we simply have to code in a game where two players play against each other, matching a random sequence of colors. My problem is getting the very first part down. This is mostly a skeleton for what's going to be a game. I'm trying to get the player names in the form to validate, but for some reason it doesn't even check whether the name is too long or has the special characters I've specified. Yes, the game is in spanish, and my code is mostly not. I'm just trying to get the names to validate correctly for now. How is my logic wrong? code:
|
# ? May 28, 2014 17:03 |
Could you add the rest of your code into a jsfiddle? https://www.jsfiddle.net
|
|
# ? May 28, 2014 17:06 |
|
jsfiddle is being a serious pain, needing me to completely break up my code in order to satisfy its need for me to only include what html, css, and js it wants in seperate windows.
|
# ? May 28, 2014 17:11 |
pepito sanchez posted:jsfiddle is being a serious pain, needing me to completely break up my code in order to satisfy its need for me to only include what html, css, and js it wants in seperate windows. For what it's worth, most pages are generally split up this way.
|
|
# ? May 28, 2014 17:12 |
|
I do use Jsfiddle for small pieces of test code, but for a project this large, it's a serious pain in the rear end. What do you need to know? The html, I assume? code:
|
# ? May 28, 2014 17:17 |
Uhh I don't know what I need to know, I just think it'd be way easier to diagnose if we could see it in action (and get the dev tools packaged with most browsers)
|
|
# ? May 28, 2014 17:21 |
|
I don't think jsfiddle would be much help anyway, since this project includes lots of source images. I only started using jsfiddle a couple of weeks ago, so I'm not sure how I would add images and stuff. edit: Okay seeing your post now, I will have to add the complete thing to some kind of website, or something. But I have to go to my currently lovely job. I thought someone could figure out what I was obviously doing wrong with the function without the entire thing. Thanks anyway for your time.
|
# ? May 28, 2014 17:22 |
No problem, I mean, I can see what you're trying to do in subCheck but yes, there are much better ways and beyond that, trying to filter input like you're doing is one of those "solved" problems, something like a regular expression... or you could include a validation library(unnecessary imo) like http://jqueryvalidation.org/ for the regex route I would start here: http://stackoverflow.com/questions/3532053/regular-expression-for-only-characters-a-z-a-z
|
|
# ? May 28, 2014 17:28 |
|
Well I can't use anything outside of the standard jquery library and my own custom js. So that's a no-no. This will be a bigger issue once I have to implement audio for the divs later on in my code, since I can't use the better implemented libraries out there. But that's for a later time. I've seen regex functions and simply don't fully understand them. That's an issue since I have to supply full documentation of my code. Anyways I should be able to understand my own code. Otherwise, what's the point? Thanks again.
|
# ? May 28, 2014 17:32 |
|
pepito sanchez posted:My problem is getting the very first part down. This is mostly a skeleton for what's going to be a game. It could be a dozen things, start using debugger; or console.log() or alert() to confirm you're even calling the submit() method on submit, or subcheck() on submit, and make sure that player1.name is set for instance (console.log() that). If something doesn't follow your conditionals as you'd expect, test each piece of them. In reality it's probably because you're creating player1 and player2 as arrays, when really you want to create objects to store properties about each of them. (replace lines 2 and 3 with this:) code:
|
# ? May 28, 2014 17:35 |
|
Well the biggest problem I see is the ids "nombre1" and "nombre2" refer to <input> tags. so you can't do $("#nombre1").html("words"), the correct function is $("#nombre1").val("words") The problem though is if you did that you would change the <input> tags' text, which contains the player's name. What you actually need to do is create a new <div> underneath the <input> tags and fill that with your "invalid username" html that you want to use and then clear the HTML when the user inputs a valid name.
|
# ? May 28, 2014 17:39 |
|
poo poo that's probably all it is. Yeah I'm trying to create them as associative arrays. In the future I have to keep track of points and modyfy a table to order players depending on points. Good tip! I will test it out when I get back from work.
|
# ? May 28, 2014 17:40 |
|
I wrote a jsfiddle that shows my solution... http://jsfiddle.net/Fbzgs/ hedgecore posted:It could be a dozen things, start using debugger; or console.log() or alert() to confirm you're even calling the submit() method on submit, or subcheck() on submit, and make sure that player1.name is set for instance (console.log() that). If something doesn't follow your conditionals as you'd expect, test each piece of them. This is bad, but not relevant to the problem since arrays are objects
|
# ? May 28, 2014 17:47 |
|
Thanks for the fiddle. I actually did something similar before with my code, but the main problem was the way it formats with the existing CSS on the page. For some reason the error messages show up split and BETWEEN the two input boxes. As in, half a line and half a line. Weird... I tried putting the div in different positions, but it just ends up weirder. I think I'll need to mess with the CSS they've given us. I just started learning associative arrays, and that's pretty much how our teacher's teaching us. I mean I could also make the whole object at the time. As in code:
pepito sanchez fucked around with this message at 02:54 on May 29, 2014 |
# ? May 29, 2014 02:51 |
|
pepito sanchez posted:But it seems kind of pointless establishing that global variable at the beginning, no? Isn't it better to keep things simple and just establish it as it builds up new parts? Doing it in one go helps make the code self-documenting. You can see the structure of the object right away. It gets especially bad when you have an object whose properties are defined inside different branches: JavaScript code:
Sometimes that happens, but I prefer initializing my objects all at once, if possible.
|
# ? May 29, 2014 06:36 |
|
Well I did as you said, because it makes sense to do it for readability, at least. My main problem now is having a sequence of pictures show based on a random number array. In fact, I can't even get a picture to change for a certain amount of time before changing back once a player clicks on it, and only while playing a game. I chopped off the code I made for on click events. Pictures weren't changing, so I know I was doing something wrong from the get-go. I think it's because I was making all of those click events inside of a function, and I simply called that function once, when the game initialized. I need to start fresh on that, specially since each click a player makes should add to its own seperate array, which later compares to the computer's random array to see if they match or not. I made a fiddle. Graphics for the game are missing, but the code is all intact. I made an alert to make sure the code enters the beginner() function once the form is properly completed. It enters, but doesn't perform the sequenced event. No sounds play, and no images change on the screen. edit: http://jsfiddle.net/WcX8S/2/ edit2: Okay I fixed the beginner() array for loop, but when it plays, it only shows one color. Use if instead of else if? And the first div id never changes color. Am I off by one somewhere? I don't see it. Also, no sounds play, so I'm completely off on that regard. pepito sanchez fucked around with this message at 20:17 on May 30, 2014 |
# ? May 30, 2014 20:11 |
|
pepito sanchez posted:Am I off by one somewhere? I don't see it. randFour function returns a random number from 1 to 4, not 0 to 3. pepito sanchez posted:no sounds play, so I'm completely off on that regard. You're trying to call play() on the jQuery object, not the DOM object: $("#snd3").play();. Try $("#snd3").get(0).play(); instead. Wheany fucked around with this message at 13:25 on May 31, 2014 |
# ? May 31, 2014 13:05 |
|
Got it. $("#snd3")[0].play(); And yeah I noticed I was off by one after staring at it long enough :/
|
# ? May 31, 2014 13:36 |
|
Javascript newbie here, playing with Emscripten. The C code I'm compiling to Javascript is pretty small, a few dozen lines, but it's a lot of integer math that depends on overflows and etc and I'm too lazy to port it myself. Emscripten, with all of the options for size optimization, still dumps ~180K of boilerplate stuff in the output, most of which appears to be unused. My translated C code ends up being about !K of this. Running the output through a minifier breaks it. Is there something I'm missing about Emscripten? Is there any way to tell it to size-optimize the stuff it's dumping into the output and not just my translated code?
|
# ? Jun 4, 2014 10:45 |
|
|
# ? Jun 5, 2024 04:08 |
|
What minifier are you using? If running it through Closure Compiler breaks it then that sounds like a bug in one of the two projects that you should report.
|
# ? Jun 4, 2014 11:05 |