Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?

Raskolnikov2089 posted:

So I'm not changing the case of finalArray[y] in:

code:
Stuff = Stuff.toUpperCase();
I'm actually just reassigning what Stuff is pointing to, in this case a new string of letters?

That's right, toUpperCase returns a new string and leaves the old one alone

You can read the MDN documentation here:

quote:

The toUpperCase() method returns the value of the string converted to uppercase. toUpperCase() does not affect the value of the string itself.

Adbot
ADBOT LOVES YOU

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Raskolnikov2089 posted:

I'm actually just reassigning what Stuff is pointing to, in this case a new string of letters?

Yes, exactly.

karms
Jan 22, 2006

by Nyc_Tattoo
Yam Slacker

HappyHippo posted:

That's right, toUpperCase returns a new string and leaves the old one alone

You can read the MDN documentation here:

Note: even if toUpperCase did change Stuff in-place, the array still wouldn't be updated because Stuff points to a copy.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

KARMA! posted:

Note: even if toUpperCase did change Stuff in-place, the array still wouldn't be updated because Stuff points to a copy.

Not true

JavaScript code:
var finalArray = [{butt: "butt"}];

console.log(finalArray[0].butt);

var Stuff = finalArray[0];
Stuff.butt = Stuff.butt.toUpperCase();

console.log(finalArray[0].butt);
edit: you might be thinking of PHP:

PHP code:
$finalArray = [['butt' => 'butt']];

echo $finalArray[0]['butt'];

$Stuff = $finalArray[0];
$Stuff['butt'] = strtoupper($Stuff['butt']);

echo $finalArray[0]['butt'];

Wheany fucked around with this message at 11:29 on Apr 1, 2015

piratepilates
Mar 28, 2004

So I will learn to live with it. Because I can live with it. I can live with it.



Wheany posted:

Not true

JavaScript code:
var finalArray = [{butt: "butt"}];

console.log(finalArray[0].butt);

var Stuff = finalArray[0];
Stuff.butt = Stuff.butt.toUpperCase();

console.log(finalArray[0].butt);
edit: you might be thinking of PHP:

PHP code:
$finalArray = [['butt' => 'butt']];

echo $finalArray[0]['butt'];

$Stuff = $finalArray[0];
$Stuff['butt'] = strtoupper($Stuff['butt']);

echo $finalArray[0]['butt'];

That's a different case though. Stuff in your example is still a copy, but it's a copy of a reference to an object and you're modifying a property of the referenced object. Setting Stuff to be equal to a new object won't affect the array it was originally in.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Raskolnikov2089 posted:

I'm actually just reassigning what Stuff is pointing to, in this case a new string of letters?

Strings are immutable so it has to work that way.

Raskolnikov2089
Nov 3, 2006

Schizzy to the matic
I really need to spend more time working on strings. Arrays I can zoom around in at this point, but string methods I've barely touched.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

piratepilates posted:

That's a different case though. Stuff in your example is still a copy, but it's a copy of a reference to an object and you're modifying a property of the referenced object. Setting Stuff to be equal to a new object won't affect the array it was originally in.

I don't really get your point and how it's relevant in JS context.

waffle enthusiast
Nov 16, 2007



Is there a good resource on designing/organizing APIs inside a MEAN stack? NodeJS in Action has been kind of dense and meandering. I'm hoping for something a little more succinct.

Opulent Ceremony
Feb 22, 2012

Wheany posted:

I don't really get your point and how it's relevant in JS context.

They were rightly pointing out that you brought up a completely different circumstance to KARMA!'s good point, which would have made more sense if you introduced it like "This can get tricky if your array contains objects because..." or some such thing

mpaarating
May 6, 2011

The Baddest Boi

Dangerllama posted:

Is there a good resource on designing/organizing APIs inside a MEAN stack? NodeJS in Action has been kind of dense and meandering. I'm hoping for something a little more succinct.

Not exactly sure what you mean but I've learned a bunch from this article https://scotch.io/tutorials/build-a-restful-api-using-node-and-express-4 and the MEAN book from their site is a nice and concise intro to MEAN dev. I've not found a lot about about API design specifically related to Node/Express but this is a pretty good resource for RESTful API design https://github.com/tlhunter/consumer-centric-api-design

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Opulent Ceremony posted:

They were rightly pointing out that you brought up a completely different circumstance to KARMA!'s good point, which would have made more sense if you introduced it like "This can get tricky if your array contains objects because..." or some such thing

Yeah, you're correct and it was me, I was the butt.

Newf
Feb 14, 2006
I appreciate hacky sack on a much deeper level than you.
Does there exist any standalone js library for creating dom elements via a zen coding syntax?

JavaScript code:
var newBootstrapButton = zen.("button.btn.btn-primary#newButton");
var newList = zen.("ul#newList>li*5");

waffle enthusiast
Nov 16, 2007



mpaarating posted:

Not exactly sure what you mean but I've learned a bunch from this article https://scotch.io/tutorials/build-a-restful-api-using-node-and-express-4 and the MEAN book from their site is a nice and concise intro to MEAN dev. I've not found a lot about about API design specifically related to Node/Express but this is a pretty good resource for RESTful API design https://github.com/tlhunter/consumer-centric-api-design

Thanks this is good info. I'm basically having trouble figuring out what data to expose to Angular, and how to logically structure my endpoints. Right now the Express server is basically just a web scraper that grabs third party data and presents it back out as JSON (I was running into CORS issues trying to do this all in Angular). I know structuring an API is a bit philosophical, and the answer is usually "it depends on what you're trying to do with the data." But basically I just don't want to have to read ten chapters on middleware components to solve my problem, which is what "NodeJS in Action" felt like. I just kind of want to finish the project.

I think scotch.io might be the right direction for me there. Thanks again.

22 Eargesplitten
Oct 10, 2010



I'm trying to set up a page to automatically refresh after a set period of inactivity. I've used some code off StackExchange, but I'm running into an issue. I can't change the source, but I can use the console. Whenever the page refreshes, the console doesn't bring the script over to the new page to keep it running. Is there any way I can make it come over?

Newf
Feb 14, 2006
I appreciate hacky sack on a much deeper level than you.

22 Eargesplitten posted:

I'm trying to set up a page to automatically refresh after a set period of inactivity. I've used some code off StackExchange, but I'm running into an issue. I can't change the source, but I can use the console. Whenever the page refreshes, the console doesn't bring the script over to the new page to keep it running. Is there any way I can make it come over?

You mean that you, as a client, want this page to refresh itself every so often in your browser? Have you tried the drinking bird?

This is a sort of thing that greasemonkey (firefox) and tampermonkey (chrome) are made to deal with. You can write a script and install it into your browser so that it runs whenever you're at page X.

Newf fucked around with this message at 21:39 on Jul 2, 2017

22 Eargesplitten
Oct 10, 2010



Yeah, at work I have a webpage with a frame I need to refresh every couple of minutes. I want to get it to refresh that page on one monitor while I do other work on the other monitor. I probably shouldn't install tampermonkey, as tempting as it is. I can edit the source code, so I'll look into either triggering the refresh button or refreshing the frame. I just don't know much JavaScript, so I'll have to learn it in my downtime. I can read it just fine, but I don't know how to write it.

Newf
Feb 14, 2006
I appreciate hacky sack on a much deeper level than you.
Tampermonkey is just a browser plugin. It's not exactly a security concern, since the JS that you run inside of it is sandboxed inside the browser in the same way that all of the js that you run into in the wild is sandboxed inside the browser. It's about tampering with the display of web content, not tampering with the computer or even the browser, really. If you're not sure about it for work, try playing around with it at home first. A script to reload a page every couple of minutes would go something like

JavaScript code:
window.setTimeout(window.location.reload, 60 * 1000 * 2);
If half of your company has this same need, you've got an opportunity to make yourself the 'technical solutions person'. Go for it.

piratepilates
Mar 28, 2004

So I will learn to live with it. Because I can live with it. I can live with it.



If you don't want to install Tampermonkey you can create a Chrome extension which injects the js on any URL you want and just load the extension as an unpacked extension in developer mode. Should take 5 minutes to set it up.

EAT THE EGGS RICOLA
May 29, 2008

I need to implement relatively complex dynamic autocomplete for a form using a Solr instance as the data source. Is there a straightforward way to implement this (maybe I should just rely on jquery's autocomplete stuff)?

necrotic
Aug 2, 2005
I owe my brother big time for this!

EAT THE EGGS RICOLA posted:

I need to implement relatively complex dynamic autocomplete for a form using a Solr instance as the data source. Is there a straightforward way to implement this (maybe I should just rely on jquery's autocomplete stuff)?

I'd just use one of the autocomplete libs. Bootstrap has a relatively decent one, I haven't used jQuery's though I'm sure it works mostly fine.

smithandweb
Feb 28, 2013

EAT THE EGGS RICOLA posted:

I need to implement relatively complex dynamic autocomplete for a form using a Solr instance as the data source. Is there a straightforward way to implement this (maybe I should just rely on jquery's autocomplete stuff)?

How complex does it need to be? jQuery's autocomplete is decent but I personally hate using jQuery UI because it's so big and not too mobile friendly, especially if you're already using other frameworks like Bootstrap, Handlebars, etc.

You can also build your own jQuery UI with only the dependencies you need. Check out their NPM page for more info on how to do that. https://www.npmjs.com/package/jquery-ui

Raskolnikov2089
Nov 3, 2006

Schizzy to the matic
So I was screwing around with some old stuff I did in coderbyte, and ran across a simple factorial recursion problem I did awhile ago

code:
function factorial(n){
	if(n < 0){
		return;
	}
	if(n ===1){
		return 1;
	}
	var k =  n * (factorial(n-1));   //formerly no var k, added for visibility
	console.log(k);
	return k
}

factorial(7);
I realized I didn't full understand what the gently caress was happening (what value is n being multiplied by) so I added a variable and a console.log so I could watch the process in inspector.

Lo and behold, the opposite of what I thought was happening, was happening. Instead of 7 * 6 * 5 * 4 *3 * 2 *1 = 362880, it was working it's way up the other way around. i.e. 1 * 2 * 3 * 4 * 5 * 6 * 7 = 362880. Okay, weird. So I did some reading and learned more about stack order, so it started to make some sense. Added to the stack from bottom to top, then the stack returns from top to bottom.

code:
 return n * (factorial(n-1)0
So if I understand what's happening, my initial call of factorial(7) put local n =7 allll the way at the bottom of my eventual stack. Next up was n =6, n=5 etc etc until my base case was met and what was returned basically was interpreted like this:

1
*
2
*
3
*
4
*
5
*
6
*
7 = 5040


Is that how it worked?

Raskolnikov2089 fucked around with this message at 18:37 on Apr 10, 2015

Anony Mouse
Jan 30, 2005

A name means nothing on the battlefield. After a week, no one has a name.
Lipstick Apathy
Yes. It's called "recursive programming" for a reason - it involves a function calling itself. Every time it does, it has to wait for the new function call to resolve, but that function call might involve calling itself again, and again, ad nauseum. Eventually there are no more recursive calls to make and you reach the base case (unless you're stuck in an infinite loop) and the very latest function call finally gets to return a value to the function that called it, instead of recursing further. The returned value finally allows the previous function to proceed executing and return another value to the function that called it, and so on back up the chain, until the original function call you made finally gets to return a value.

There's probably more proper technical jargon that someone could chime in with, but that's the gist of it. I'm curious how you know terms like recursion, base case, and stack without being 100% clear on even a basic example?

Edit: Actually your code must be flawed from the start somehow, because 7 factorial is 5,040, not 362,880. http://jsfiddle.net/299ufL5y/1/

Anony Mouse fucked around with this message at 07:03 on Apr 10, 2015

Rahul
Dec 10, 2004

I've got a quick question about a custom function I'm trying to write for a Google Drive spreadsheet with Google Apps Script.
I need to modify it so a single function call will work over an array of values instead of single cells, so that google drive won't flip out when it gets called a couple of hundred times.
Here's the function as it is now:
code:
/**
 * Searches the string specified by 'data' and counts the number of occurences of 'criterion'
 */
function VOTECOUNT(data, criterion) {
  var count = 0;
  
  if(criterion == 0){
    return;
  }
  for(i = 0; i < data.length; i++){
    if(data.substring(i,i + criterion.length) === criterion){
       count ++;
       }
  }
  
  if(count === 0){
    return;
  }
  else{
  return count;
  }
};
Basically it takes two strings, and counts the number of times one string occurs as a substring within the other string.
Now is there an easy way to modify this so that if 'criterion' is an array with M elements, and 'data' is an array with N elements, the function will return a M*N 2d array, where the function has been called on every single combination?

Newf
Feb 14, 2006
I appreciate hacky sack on a much deeper level than you.

Rahul posted:

I've got a quick question about a custom function I'm trying to write for a Google Drive spreadsheet with Google Apps Script.
I need to modify it so a single function call will work over an array of values instead of single cells, so that google drive won't flip out when it gets called a couple of hundred times.
Here's the function as it is now:


Basically it takes two strings, and counts the number of times one string occurs as a substring within the other string.
Now is there an easy way to modify this so that if 'criterion' is an array with M elements, and 'data' is an array with N elements, the function will return a M*N 2d array, where the function has been called on every single combination?

You just want to recurse the call here if your inputs are arrays. Something like this might work: http://jsfiddle.net/wdwvdzzm/

JavaScript code:
/**
 * Searches the string specified by 'data' and counts the number of occurences of 'criterion'
 */
function VOTECOUNT(data, criterion) {
    if (Array.isArray(data) && Array.isArray(criterion)){
        var retArray = [];
        for (var i=0; i<data.length; i++){
            retArray[i] = [];
            for (var j=0; j<criterion.length; j++){
                retArray[i].push(VOTECOUNT(data[i], criterion[j]));
            }
        }
        return retArray;
    } else {
      var count = 0;
      
      if(criterion == 0){
        return;
      }
      for(i = 0; i < data.length; i++){
        if(data.substring(i,i + criterion.length) === criterion){
           count ++;
           }
      }
      
      if(count === 0){
        return;
      }
      else{
      return count;
      }
    }
};

alert(VOTECOUNT(['ayeayeaye', 'ayenayaye'], ['aye','nay']));
e: Is there a reason you want 'undefined' returned here instead of zero?

Newf fucked around with this message at 13:36 on Apr 10, 2015

Raskolnikov2089
Nov 3, 2006

Schizzy to the matic

Anony Mouse posted:

Yes. It's called "recursive programming" for a reason - it involves a function calling itself. Every time it does, it has to wait for the new function call to resolve, but that function call might involve calling itself again, and again, ad nauseum. Eventually there are no more recursive calls to make and you reach the base case (unless you're stuck in an infinite loop) and the very latest function call finally gets to return a value to the function that called it, instead of recursing further. The returned value finally allows the previous function to proceed executing and return another value to the function that called it, and so on back up the chain, until the original function call you made finally gets to return a value.

There's probably more proper technical jargon that someone could chime in with, but that's the gist of it. I'm curious how you know terms like recursion, base case, and stack without being 100% clear on even a basic example?

Edit: Actually your code must be flawed from the start somehow, because 7 factorial is 5,040, not 362,880. http://jsfiddle.net/299ufL5y/1/

I've used recursion a fair amount and understand how to set it up. Stuff like the base case, termination case, etc I fully grasp. I just don't fully understand the underlying mechanism in the stack. It took me a bit last night to see that what firstFactorial(n-1) was doing was re-calling the function (got that part) so that the new n value could be added to the stack (didn't have this part).

So now that I know for sure that's how the stack works for recursive addition or multiplication, what about an operation where the order actually matters?

If I was to take the same code, but swap the * out with a / or - operand things seem to go to hell if that is how the stack functions:

code:
function factorial(n){
	if(n < 0){
		return;
	}
	if(n ===1){
		return 1;
	}
	var k =  n - (firstFactorial(n-1));   //formerly no var k, added for visibility
	console.log(k);

	factorial(7);
}
Which makes sense, given that 7/6/5/4/3/2/1 or 7-6-5-4-3-2-1 is different from 1/2/3/4/5/6/7 or 1-2-3-4-5-6-7.

http://jsfiddle.net/299ufL5y/2/

Raskolnikov2089 fucked around with this message at 15:26 on Apr 10, 2015

MasterSlowPoke
Oct 9, 2005

Our courage will pull us through
7 -
6 -
5 -
4 -
3 -
2 -
1

Working bottom to top:

7 -
6 -
5 -
4 -
3 -
1
=>

7 -
6 -
5 -
4 -
2
=>

7 -
6 -
5 -
2
=>

7 -
6 -
3
=>

7 -
3
=>

4

You're not doing 7-6-5-4-3-2-1, you're doing 7-(6-(5-(4-(3-(2-(1)))))).

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?

Raskolnikov2089 posted:

I've used recursion a fair amount and understand how to set it up. Stuff like the base case, termination case, etc I fully grasp. I just don't fully understand the underlying mechanism in the stack. It took me a bit last night to see that what firstFactorial(n-1) was doing was re-calling the function (got that part) so that the new n value could be added to the stack (didn't have this part).

So now that I know for sure that's how the stack works for recursive addition or multiplication, what about an operation where the order actually matters?

If I was to take the same code, but swap the * out with a / or - operand things seem to go to hell if that is how the stack functions:

code:
function factorial(n){
	if(n < 0){
		return;
	}
	if(n ===1){
		return 1;
	}
	var k =  n - (firstFactorial(n-1));   //formerly no var k, added for visibility
	console.log(k);

	factorial(7);
}
Which makes sense, given that 7/6/5/4/3/2/1 or 7-6-5-4-3-2-1 is different from 1/2/3/4/5/6/7 or 1-2-3-4-5-6-7.

http://jsfiddle.net/299ufL5y/2/

This code you've posted twice now makes no sense. firstFactorial isn't defined at all, you call factorial(7) in the factorial function itself for some reason, and you're only returning any value in the base case (you find k and log it, but you never return it). I'm guessing you must be writing this from memory or something because as written it's completely broken and would result in an infinite loop of factorial(7) assuming that firstFactorial is defined anywhere at all.

Edit: For reference, here's the "correct" version (you should never do this in a language without tail call optimization):

code:
function factorial(n) {
	if(n == 0)
		return 1;
	else
		return n * factorial(n - 1);
}

HappyHippo fucked around with this message at 16:34 on Apr 10, 2015

EAT THE EGGS RICOLA
May 29, 2008

smithandweb posted:

How complex does it need to be? jQuery's autocomplete is decent but I personally hate using jQuery UI because it's so big and not too mobile friendly, especially if you're already using other frameworks like Bootstrap, Handlebars, etc.

You can also build your own jQuery UI with only the dependencies you need. Check out their NPM page for more info on how to do that. https://www.npmjs.com/package/jquery-ui

It's not that bad, but it needs to query across multiple Solr fields and populate multiple form fields once one of the autocomplete options is selected.

There's no requirement that this be mobile-accessible (it's an internal application on an airgapped network with no access to the internet) so I guess I'l just go with jQuery.

Raskolnikov2089
Nov 3, 2006

Schizzy to the matic

HappyHippo posted:

This code you've posted twice now makes no sense. firstFactorial isn't defined at all, you call factorial(7) in the factorial function itself for some reason, and you're only returning any value in the base case (you find k and log it, but you never return it). I'm guessing you must be writing this from memory or something because as written it's completely broken and would result in an infinite loop of factorial(7) assuming that firstFactorial is defined anywhere at all.

Edit: For reference, here's the "correct" version (you should never do this in a language without tail call optimization):

code:
function factorial(n) {
	if(n == 0)
		return 1;
	else
		return n * factorial(n - 1);
}

Eugh. Yeah, sorry, I've created several different versions of factorial() by copying and pasting so that I could look at all the different ways the stack is working. One multiplies, another divides, etc etc. I should have double checked that what I copied here was working code. Right now I have 8 versions going (+ - * / with the position of factorial(n-1) moved around). I fixed what I originally posted.

At least for multiplication and addition it all makes sense now. I'm going to have to table n - factorial(n-1) === 4 for now. I get why that's happening but it's still a little crazy to me.

Raskolnikov2089 fucked around with this message at 18:45 on Apr 10, 2015

MasterSlowPoke
Oct 9, 2005

Our courage will pull us through
It's just the distributive property.

http://www.freemathhelp.com/distributive-property.html

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?

Raskolnikov2089 posted:

Eugh. Yeah, sorry, I've created several different versions of factorial() by copying and pasting so that I could look at all the different ways the stack is working. One multiplies, another divides, etc etc. I should have double checked that what I copied here was working code. Right now I have 8 versions going (+ - * / with the position of factorial(n-1) moved around). I fixed what I originally posted.

At least for multiplication and addition it all makes sense now. I'm going to have to table n - factorial(n-1) === 4 for now. I get why that's happening but it's still a little crazy to me.

The stack isn't really relevant to this. Just expand out the definition, putting the result of a function call in parenthesis:
code:
f(n) = n * f(n-1)
f(0) = 1

f(4) = 4 * f(3) 
     = 4 * (3 * f(2))
     = 4 * (3 * (2 * f(1)))
     = 4 * (3 * (2 * (1 * f(0))))
     = 4 * (3 * (2 * (1 * (1))))
Now put whatever operator you want in there. If it's a "-" then
code:
f(4) = 4 - (3 - (2 - (1 - (1))))
And so on

karms
Jan 22, 2006

by Nyc_Tattoo
Yam Slacker
edit: nm

Newf
Feb 14, 2006
I appreciate hacky sack on a much deeper level than you.

HappyHippo posted:

The stack isn't really relevant to this. Just expand out the definition, putting the result of a function call in parenthesis:

Knowing how the recursive definition works and knowing how the computer actually physically does the thing are very different. Recursive functions confused the hell out of me when I learned about them (I already had a math degree) because I had no conception of a stack and I didn't know where the 'temp' values were being declared, computed, destroyed etc.

Stack frames pile up here until they're actually able to work out the values they've been asked for, and then they pass the value back to the frame below it and 'pop' out of existence. Here's what happens to the stack when you call f(4):

code:
1:

f(4) -> 4 + f(3)  -> put a new frame on the stack in order to compute f(3)

2:

f(3) -> 3 + f(2)  -> put a new frame on the stack in order to compute f(2)
f(4)    4 + f(3)

3:

f(2) -> 2 + f(1) ...
f(3)	3 + f(2)
f(4)	4 + f(3)

4:

f(1) -> 1 + f(0)
f(2)	2 + f(1)
f(3)	3 + f(2)
f(4)	4 + f(3)

5:

f(0) -> 1        -> f(0) is 1, so pass that back to the caller from the previous frame
f(1)	1 + f(0)
f(2)	2 + f(1)
f(3)	3 + f(2)
f(4)	4 + f(3)

6:

f(1) -> 1 + 1
f(2)	2 + f(1)
f(3)	3 + f(2)
f(4)	4 + f(3)

7:

f(1) -> 2        -> f(1) is 2, so pass that back to the caller from the previous frame
f(2)	2 + f(1)
f(3)	3 + f(2)
f(4)	4 + f(3)

8:

f(2) -> 2 + 2
f(3)	3 + f(2)
f(4)	4 + f(3)

9:

f(2) -> 4         -> ...
f(3)    3 + f(2)
f(4)	4 + f(3);

10:

f(3) -> 3 + 4
		3 + f(3);

11:

f(3) -> 7
f(4)    3 + f(3)

12:

f(4) -> 3 + 7

13:

f(4) -> 10

14:

return 10 to the caller of f(4) (eg, you)

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Be careful to not take the "physical" (?) nature of the stack frame too literally. In JS as in other languages they may disappear where you don't expect due to optimizations.

Thinking about scopes is more helpful, IME, and can be reused when reasoning about closures and such.

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?

Newf posted:

Knowing how the recursive definition works and knowing how the computer actually physically does the thing are very different. Recursive functions confused the hell out of me when I learned about them (I already had a math degree) because I had no conception of a stack and I didn't know where the 'temp' values were being declared, computed, destroyed etc.

Yeah, it just seemed to me that that Raskolnikov2089 was looking in the wrong place for the answer to their question. I was pointing out that you don't need to bring in the details of the stack in order to see that its 4-(3-(2-(1))) and not (((4)-3)-2)-1; that's implied by how the function is defined.

ArcticZombie
Sep 15, 2010
I'm having a funny issue with a javascript dictionary/hash/object/whatever you call a set of key-value pairs.

Here's the JS:

code:
var test = {"standard": {"foo":[1,2,3,4,5], "bar":[1,2,3,4,5]},
            "wordfeud": {"foo":[1,2,3,4,5], "bar":[1,2,3,4,5]}};

console.log(layouts);
console.log(layouts.standard);
console.log(layouts["standard"]);

console.log(test);
console.log(test.standard);
console.log(test["standard"]);
And the resulting output in the console:

code:
Object { standard: Object, wordfeud: Object, wwf: Object }
undefined
undefined

Object { standard: Object, wordfeud: Object }
Object { foo: Array[5], bar: Array[5] }
Object { foo: Array[5], bar: Array[5] }
Why does trying to access the layouts object result in undefined, when it clearly shows that those keys exist?

Here's a screenshot of the console, with the properties window on the right clearly showing that all the data I want is in there.

Not shown in the code is how the layouts object is defined. I've not shown this but basically, it makes an AJAX request to the web server (Flask), which responds with JSON.

Edit: I've just realised this is probably something to do with JavaScript being asynchronous, but I've no idea how to solve the problem.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

ArcticZombie posted:

Edit: I've just realised this is probably something to do with JavaScript being asynchronous, but I've no idea how to solve the problem.

Well, if something is asynchronous, give it a callback function to call when the asynchronous process completes.

JavaScript code:
var callback = function (results) {
	console.log(results.whatever);
}

someAsynchronousFunction(callback);

Adbot
ADBOT LOVES YOU

ArcticZombie
Sep 15, 2010
Thanks, I solved the problem, but I can't understand why the first log has access to the object but the ones after it don't.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply