|
Dangerllama posted:This code always returns the empty JSON via the API: [{}] Because you are returning the initial value ({}) from getDuData. To make it work asynchronously you need to do something like: code:
|
# ? Dec 14, 2014 02:33 |
|
|
# ? Jun 5, 2024 06:09 |
|
Given a stupid structure like:JavaScript code:
The question is how to get a list of all parent objects info.id given a particular id. It easy to write a function to recurse in and find any given id, but for some reason I'm having difficulty getting the parents ids which actually seems like it should be easy on first blush. So...
For reference, my depth-first recursion function works just like you'd think: JavaScript code:
|
# ? Dec 14, 2014 03:55 |
|
necrotic posted:Because you are returning the initial value ({}) from getDuData. To make it work asynchronously you need to do something like: Ahh…This makes a lot more sense. Thanks to you both.
|
# ? Dec 14, 2014 04:32 |
|
Thermopyle posted:For reference, my depth-first recursion function works just like you'd think: A good first step would be to modify this so you can actually make use of it in your program instead of just printing something out. A typical example for a "find" function would be returning true if the value is found, or false if it is not.
|
# ? Dec 14, 2014 04:53 |
|
Jabor posted:A good first step would be to modify this so you can actually make use of it in your program instead of just printing something out. A typical example for a "find" function would be returning true if the value is found, or false if it is not. Sorry, that's not my actual function. I do lots of other things to the data structure so I just whipped up a quick example of how I recurse through it. I do actually do useful things with my actual code.
|
# ? Dec 14, 2014 05:14 |
|
Thermopyle posted:Sorry, that's not my actual function. I do lots of other things to the data structure so I just whipped up a quick example of how I recurse through it. I do actually do useful things with my actual code. Okay then, so let's set about solving your problem. We solve recursive problems in two steps - our first step is to solve the base case(s). Our first base case is when we've found the node we're after. code:
code:
|
# ? Dec 14, 2014 05:58 |
|
Heh, I guess I wasn't clear enough. I know the principles of writing recursive functions. I kept screwing myself because I forgot about the difference between passing primitive values and objects in js as I don't write in js enough. After taking a break I realized that I was passing a reference to the same array around to accumulate parents. All sorted now. Sorry to waste your time.
|
# ? Dec 14, 2014 08:54 |
|
Okay, you know how XORing a byte by 0b0000 0000 (0x00) will flip the bits, right? e.g. 0b1010 1010 (0xAA) ^ 0b0000 0000 (0x00) -> 0b0101 0101 (0x55). You'd expect the same behavior in Javascript, since it's basic computer science and logic, right? WRONG code:
" MDN posted:Bitwise XORing any number x with 0 yields x. Bitwise XORing any number x with -1 yields ~x. You have got to be kidding me.
|
# ? Dec 15, 2014 18:15 |
|
That's a great find, you should post in the coding horrors thread
|
# ? Dec 15, 2014 18:29 |
|
Sedro posted:That's a great find, you should post in the coding horrors thread That wasn't very nice
|
# ? Dec 15, 2014 18:33 |
|
Pollyanna posted:Okay, you know how XORing a byte by 0b0000 0000 (0x00) will flip the bits, right? What's 1 XOR 0? You're not gonna have enough faces or palms when you get this all sorted out.
|
# ? Dec 15, 2014 18:55 |
|
I've let this bang around in my head for a few days but I'm still confused. One of the early listings from Secrets of the JS Ninja has a line of code that doesn't seem to me to do anything (it's an assignment) but the code fails when it's omitted. Here's a fiddle.JavaScript code:
Please explain.
|
# ? Dec 16, 2014 11:10 |
|
Newf posted:
The page breaks because the assert function tries to use the results variable, which will be undefined if you comment out the line you specified. EDIT If you take a look at the actual tests being run, you can see that the calling order will be something like test -> assert -> assert -> test -> assert -> ... This in essence means that calling test will create a list under the #results DOM element, and subsequent calls to assert will append stuff to the created list. Calling test again will create another list under the #results DOM element, and so on. ostills fucked around with this message at 12:02 on Dec 16, 2014 |
# ? Dec 16, 2014 11:53 |
|
ostills posted:The page breaks because the assert function tries to use the results variable, which will be undefined if you comment out the line you specified. This is pretty terrible code.
|
# ? Dec 16, 2014 12:06 |
|
Wheany posted:This is pretty terrible code. Yeah. I feel like there is a lesson about functions having side effects here.
|
# ? Dec 16, 2014 12:15 |
|
ostills posted:Yeah. I feel like there is a lesson about functions having side effects here. To me, it's more a lesson in naming your variables. I would have probably used var resultsElem = document.getElementById("results"); and then testResult = assert()
|
# ? Dec 16, 2014 12:45 |
|
Christ, this is obvious now, thanks. This sort of shared-scoping/variable re-use bites me constantly with js. Will probably keep getting me until my brain changes shape entirely.
Newf fucked around with this message at 13:01 on Dec 16, 2014 |
# ? Dec 16, 2014 12:58 |
|
It seems like results was never declared with a var (unless I missed it) so automatic globals strikes again. If you use "use strict" then you'd be forced to declare "results" in the relevant scope which would hopefully have made this more clear. Edit: nevermind, it's declared at the top. Anyway, did you attempt to use the the browser's debug feature? It will point you to the line the error occurred on which should've helped you track this down. Trying to figure out what's going on when all you have is the page working or breaking is just going to lead to frustration. HappyHippo fucked around with this message at 16:27 on Dec 16, 2014 |
# ? Dec 16, 2014 16:22 |
|
I'm working on a JS app that basically sits in other peoples' websites where we're compiling/minifying a local jQuery and various plugins into one file. This has been working fine but we've been trying to reduce the footprint of our app. The idea is to load jQuery only if the website we're in doesn't already have it. So basically I ended up with something like this when jQuery doesn't exist:code:
Tomed2000 fucked around with this message at 23:14 on Dec 22, 2014 |
# ? Dec 22, 2014 23:12 |
|
Tomed2000 posted:I'm working on a JS app that basically sits in other peoples' websites where we're compiling/minifying a local jQuery and various plugins into one file. This has been working fine but we've been trying to reduce the footprint of our app. The idea is to load jQuery only if the website we're in doesn't already have it. So basically I ended up with something like this when jQuery doesn't exist: You should start looking into a script loader - something like require.js (although there are many others.) A lot of work has been done on automating script loading and making it so pages load faster already, you probably shouldn't roll your own.
|
# ? Dec 22, 2014 23:49 |
|
Hey guys, uh someone in another thread told me to use Bootstrap or Foundation to handle my html/css templating and stuff. It looks like they both required jquery and use some javascript for certain features. Do any of you know how they behave with javascript disabled?
|
# ? Dec 29, 2014 21:11 |
|
I have a simple, single webpage that has a button called "Start Over" with coding of:code:
|
# ? Dec 29, 2014 21:14 |
|
Storgar posted:Hey guys, uh someone in another thread told me to use Bootstrap or Foundation to handle my html/css templating and stuff. It looks like they both required jquery and use some javascript for certain features. Do any of you know how they behave with javascript disabled? For bootstrap, just stay away from the list of features under the "javascript" tab of the website. http://getbootstrap.com/javascript/ Everything else will work fine. But very few people even worry about browsers with no JS these days. The internet is just broken without it.
|
# ? Dec 29, 2014 21:33 |
|
Hughmoris posted:I have a simple, single webpage that has a button called "Start Over" with coding of: Add a click event listener and check for the shiftKey modifier: http://jsfiddle.net/m9zy63v9/1/
|
# ? Dec 29, 2014 22:53 |
|
necrotic posted:Add a click event listener and check for the shiftKey modifier: http://jsfiddle.net/m9zy63v9/1/ Perfect, thank you.
|
# ? Dec 29, 2014 23:51 |
|
Please use the "input" event, not "click", just in case someone uses the Enter or Space keys to activate your button. Unless those activate "click" now. They didn't used to.
|
# ? Dec 30, 2014 05:01 |
|
Storgar posted:Hey guys, uh someone in another thread told me to use Bootstrap or Foundation to handle my html/css templating and stuff. It looks like they both required jquery and use some javascript for certain features. Do any of you know how they behave with javascript disabled? Disable javascript in your browser and check out their demo pages. Should give you a decent idea.
|
# ? Dec 30, 2014 08:59 |
|
Storgar posted:Hey guys, uh someone in another thread told me to use Bootstrap or Foundation to handle my html/css templating and stuff. It looks like they both required jquery and use some javascript for certain features. Do any of you know how they behave with javascript disabled? Also I will never actually actively tell someone to use jQuery over vanillaJS, however if it gets the job done and you know why and how you are using it then that's fine. It's all about getting the job done right and in a timely manner.
|
# ? Dec 30, 2014 10:26 |
|
Suspicious Dish posted:Please use the "input" event, not "click", just in case someone uses the Enter or Space keys to activate your button. Unless those activate "click" now. They didn't used to. If he wants shift-enter to accomplish the same thing then yes. To mean that seems like an odd input combo as enter usually submits a form and its not that hard to accidentally hold shift in the process.
|
# ? Dec 30, 2014 13:00 |
|
5pitf1re posted:Also I will never actually actively tell someone to use jQuery over vanillaJS, however if it gets the job done and you know why and how you are using it then that's fine. It's all about getting the job done right and in a timely manner. I'm all like this except I always end up using jQuery for something and then I might as well use it for everything.
|
# ? Dec 30, 2014 18:30 |
|
Thermopyle posted:I'm all like this except I always end up using jQuery for something and then I might as well use it for everything. I end up wanting to re-create chunks of jQuery in shim methods that just let me avoid all the tedium involved in dealing with the DOM. Also, just using an example on the joke page JavaScript code:
JavaScript code:
The usefulness of jQ as a band aid over all the incompatible mess in older browsers is fading with their market share, but I'll just replace it with something that lets me avoid dealing directly with the DOM because
|
# ? Dec 30, 2014 22:01 |
|
Absolutely. VanillaJS can be tedious as hell, I agree. With promises I tend to care less about those ugly parts as you can simply shim them away and have the bonus of promises so that you don't have to deal with spaghetti code and ugly callback handlers. e: Just wondering, who in here ever used the Stack Overflow JavaScript chat? 5pitf1re fucked around with this message at 13:28 on Dec 31, 2014 |
# ? Dec 31, 2014 12:03 |
|
Trying to teach myself Javascript using the book "A Smarter Way To Learn Javascript" and I'm already stumped. I'm trying to call a function, and I've copied/pasted it from the book but it is not working for me. When I click on "Click", nothing happens. Where am I going wrong? http://jsfiddle.net/m9zy63v9/2/
|
# ? Dec 31, 2014 17:58 |
|
Two problems: there's line numbers in your JS and the JS was set to run onLoad, but your binding of the behavior is right in the HTML. Binding a function in an attribute only works for functions that are already declared, not tied to the load event, so switching it to <head> solves the problem. You might not understand that second part at this point in your learning, which is perfectly normal.
|
# ? Dec 31, 2014 18:11 |
|
rt4 posted:Two problems: there's line numbers in your JS and the JS was set to run onLoad, but your binding of the behavior is right in the HTML. Binding a function in an attribute only works for functions that are already declared, not tied to the load event, so switching it to <head> solves the problem. Can't believe I didn't see the numbers in the JS. Thanks, I pasted everything into notepad and got it working.
|
# ? Dec 31, 2014 18:15 |
|
An editor with better syntax highlighting would've put some sort of error notification there
|
# ? Dec 31, 2014 18:39 |
|
rt4 posted:An editor with better syntax highlighting would've put some sort of error notification there You just have to click the JSHint button and it will tell you that your JS was all messed up.
|
# ? Dec 31, 2014 18:40 |
|
5pitf1re posted:You just have to click the JSHint button and it will tell you that your JS was all messed up. Just started using the JSFiddle site, that is pretty neat!
|
# ? Dec 31, 2014 18:52 |
I've read a bunch of different answers but for some reason I can't wrap my head around the best way to access/iterate/loop through values that are nested but not in arrays. As an example: code:
What I'm trying to understand is a real way to loop through to these from the top down, preferably with something that would work recursively or would be able to keep drilling down through an unknown number of layers/splits. Say there were two classrooms and I wanted to give "flem" extra credit, but I don't know what classroom they're in and I'd like the function to figure it out. I've been googling it for a few hours and I'm still not sure how to use a for in loop to solve this, or if that's even the right tool. Smerdyakov fucked around with this message at 11:30 on Jan 1, 2015 |
|
# ? Jan 1, 2015 10:38 |
|
|
# ? Jun 5, 2024 06:09 |
|
Smerdyakov posted:I've read a bunch of different answers but for some reason I can't wrap my head around the best way to access/iterate/loop through values that are nested but not in arrays. Phone posting so not much detail for you but you want to use a "for in" loop, coupled with hasOWnProperty. code:
Lumpy fucked around with this message at 18:31 on Jan 1, 2015 |
# ? Jan 1, 2015 18:27 |