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
Munkeymon
Aug 14, 2003

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



Knifegrab posted:

Would such a pause be indefinite though? The script doesn't just hiccup, it straight up fails to continue.

Oh, no - I didn't realize you meant it was hanging indefinitely.

Adbot
ADBOT LOVES YOU

Knifegrab
Jul 30, 2014

Gadzooks! I'm terrified of this little child who is going to stab me with a knife. I must wrest the knife away from his control and therefore gain the upperhand.

Munkeymon posted:

Oh, no - I didn't realize you meant it was hanging indefinitely.

Yeah its just failing to resume enitrely. Going to figure out where exactly its happening and then likely will add a promise timeout function.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

You might be running off the end of the event loop, I recall that being annoying to debug.

Knifegrab
Jul 30, 2014

Gadzooks! I'm terrified of this little child who is going to stab me with a knife. I must wrest the knife away from his control and therefore gain the upperhand.

Subjunctive posted:

You might be running off the end of the event loop, I recall that being annoying to debug.

What exactly do you mean here? If I were compeltely the loop (which shouldn't happen) I'd have seen it in the logs so I would assume you are referring to something else.

Munkeymon
Aug 14, 2003

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



https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop (I assume)

kloa
Feb 14, 2007


I must be missing something, because I can't the following JSON result to output:

code:

var state = ...workplace.workplaceAddresses.P,1;

I've tried putting the P,1 in brackets and quotes but I keep getting JSHint errors when trying to console log the variable.

It's weird because mustache.js has no problem rendering fields with commas, but the JS console isn't liking it.

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

kloa posted:

I must be missing something, because I can't the following JSON result to output:

code:
var state = ...workplace.workplaceAddresses.P,1;
I've tried putting the P,1 in brackets and quotes but I keep getting JSHint errors when trying to console log the variable.

It's weird because mustache.js has no problem rendering fields with commas, but the JS console isn't liking it.

Err, that doesn't look like valid JS or JSON. What's it supposed to do? Mustache probably does some magical handling of "fields" like that when rendering.

kloa
Feb 14, 2007


Well I'm phone posting, so I short handed the parsed JSON result that's going into the variable.

I can output the parsed JSON as an object to the console, but trying to display a single field isn't working.

Knifegrab
Jul 30, 2014

Gadzooks! I'm terrified of this little child who is going to stab me with a knife. I must wrest the knife away from his control and therefore gain the upperhand.

kloa posted:

Well I'm phone posting, so I short handed the parsed JSON result that's going into the variable.

I can output the parsed JSON as an object to the console, but trying to display a single field isn't working.

Could you try:
code:
var state = ...workplace.workplaceAddresses["P,1"];

kloa
Feb 14, 2007


That was it!

I'm a dummy and was trying to do ".["P,1"]" instead of without the period :downs:

Knifegrab
Jul 30, 2014

Gadzooks! I'm terrified of this little child who is going to stab me with a knife. I must wrest the knife away from his control and therefore gain the upperhand.

kloa posted:

That was it!

I'm a dummy and was trying to do ".["P,1"]" instead of without the period :downs:

My entire javascript career is a series of incredibly dumb mistakes, just glad I could actually help someone else for once!

my effigy burns
Aug 23, 2015

IF I'M NOT SHITPOSTING ABOUT HOW I, A JUNIOR DEVELOPER IN JAVASCRIPT KNOW EVERYTHING THERE IS TO KNOW, PLEASE CHECK TO BE SURE MY ACCOUNT WAS NOT COMPROMISED BY A CLIENT-SIDE BOTNET, TIA
What's a good response in a technical interview for a javascript position when they tell you to do a specific kind of search and modify operation with an unsorted array and then to "convert it into a hash map." I was under the impression that since javascript arrays are objects and their indices are actually keys, a hash map is not a meaningful concept.

Is it some kind on initiative test where you're supposed to call shenanigans, or is it pretty common to get interviewers that don't know anything about javascript since whiteboarding is more about general CS skills?

v1nce
Sep 19, 2004

Plant your brassicas in may and cover them in mulch.

my effigy burns posted:

[...] operation with an unsorted array and then to "convert it into a hash map." I was under the impression that since javascript arrays are objects and their indices are actually keys, a hash map is not a meaningful concept.
I think you're reading too deeply into the question. JavaScript Arrays, like everything in JavaScript, is an Object, but that's not really relevant. An Array in JavaScript-land isn't a hash map because the indexes aren't under your control.

wikipedia posted:

In computing, a hash table (hash map) is a data structure used to implement an associative array, a structure that can map keys to values. A hash table uses a hash function to compute an index into an array of buckets or slots, from which the desired value can be found.

Arrays in JavaScript are only numeric:
code:
var x = ['a', 'b', 'c', 'd', 'e'];
Object.keys(x); // ["0", "1", "2", "3", "4"]
x[10] = 'f';
x // ["a", "b", "c", "d", "e", undefined × 5, "f"]
x.length // 11
But Objects can be used to make associative arrays (kinda):
code:
var x = {a:1, b:2, c:3, d:4}
Object.keys(x); // ["a", "b", "c", "d"]
x.y = 5;
x.length // undefined
x // Object {a: 1, b: 2, c: 3, d: 4, y: 5}
And you can gently caress this up by treating an array like an object, but it'll still behave mostly like an array:
code:
var x = ['a', 'b', 'c', 'd', 'e'];
x.y = 'go gently caress yourself';
Object.keys(x); // ["0", "1", "2", "3", "4", "y"]
x.length // 5
x // ["a", "b", "c", "d", "e"]
for(var i in x) { console.log(i); } // 0, 1, 2, 3, 4, y
So I guess a good response is: Don't be smart. Handle them like a client. Get clarifications on what you think they're asking you to do. In all likelihood they just want an object with associative keys to values and you should probably forget about the weird nuances of the language.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

v1nce posted:

Arrays in JavaScript are only numeric:
code:

var x = ['a', 'b', 'c', 'd', 'e'];
Object.keys(x); // ["0", "1", "2", "3", "4"]
x[10] = 'f';
x // ["a", "b", "c", "d", "e", undefined × 5, "f"]
x.length // 11

Try setting x.wibble; arrays can have non-numeric properties just fine. (Array.prototype, which is an array, has a bunch of non-numeric properties like map and splice.)

v1nce
Sep 19, 2004

Plant your brassicas in may and cover them in mulch.
Or you could try reading all of my post.

v1nce posted:

And you can gently caress this up by treating an array like an object, but it'll still behave mostly like an array:
code:
var x = ['a', 'b', 'c', 'd', 'e'];
x.y = 'go gently caress yourself';
Object.keys(x); // ["0", "1", "2", "3", "4", "y"]
x.length // 5
x // ["a", "b", "c", "d", "e"]
for(var i in x) { console.log(i); } // 0, 1, 2, 3, 4, y

It shows up in the keys, it gets iterated, but it doesn't add to the length, and it doesn't appear in the array itself. You're meddling with the Object formulation of the Array, rather than the actual array data structure.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

v1nce posted:

Or you could try reading all of my post.

It shows up in the keys, it gets iterated, but it doesn't add to the length, and it doesn't appear in the array itself. You're meddling with the Object formulation of the Array, rather than the actual array data structure.

I read your whole post. You can have properties on Arrays that aren't non-negative integers: they appear in keys and importantly can be used to retrieve values. They don't show up in toString output, but they don't appear in Object's toString output either! Array is specified to be Object with a different prototype, plus a magical length property that reacts to some keys (those that look like non-negative integers). Until 2008, when I split the implementation in Firefox to specialize dense storage of array values, AFAIK all JS engines implemented arrays as the same underlying structure as Object, including creating string keys for accesses and doing a hash or similar lookup. I don't know what "the Object formulation of the Array, rather than the actual array data structure" means, tbh.

There is a good reason to not use Arrays as arbitrary dictionaries if you don't know the range of possible key names, but it's because of something Array does, not because of something it doesn't do.

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

my effigy burns posted:

What's a good response in a technical interview for a javascript position when they tell you to do a specific kind of search and modify operation with an unsorted array and then to "convert it into a hash map." I was under the impression that since javascript arrays are objects and their indices are actually keys, a hash map is not a meaningful concept.

Is it some kind on initiative test where you're supposed to call shenanigans, or is it pretty common to get interviewers that don't know anything about javascript since whiteboarding is more about general CS skills?

If I asked this in a javascript interview, this is how I'd expect it to go:
code:
var arr = [
    { key: 'foo', val: 'bar' },
    { key: 'hello', val: 'world' }
];

var result = arr.reduce(function(map, obj) {
    map[obj.key] = obj.val;
    return map;
}, {});
Because that's a useful technique. I wouldn't be looking for an education on javascript semantics.

Knifegrab
Jul 30, 2014

Gadzooks! I'm terrified of this little child who is going to stab me with a knife. I must wrest the knife away from his control and therefore gain the upperhand.
Promise question related to flow control:

Say I have a promise chain with a bunch of step functions, my code loooks likes this:

code:
promiseSomething(arg)
.then(function(res) {
   return promiseSomethingElse();
}).then(function(res) {
   return promiseOneMoreThing();
}).then(function(res) {
   console.log('success');
}).fail(function(err) {
   console.log('something happened');
});
Now say I want to insert a codnition into my first then clause, and only return a promise on success and get out of hte promise chain on exit, how would I do this?

code:
promiseSomething(arg)
.then(function(res) {
   if(res) return promiseSomethingElse();
   else //I don't know what to do here, but I don't want to enter into any of the other then statements, 
   //and doing nothing still drops me into the next 'then' in the promise chain.
}).then(function(res) {
   return promiseOneMoreThing();
}).then(function(res) {
   console.log('success');
}).fail(function(err) {
   console.log('something happened');
});
Is the only way around this to nest another promise chain inside the first conditional?

piratepilates
Mar 28, 2004

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



Knifegrab posted:

Promise question related to flow control:

Say I have a promise chain with a bunch of step functions, my code loooks likes this:

code:
promiseSomething(arg)
.then(function(res) {
   return promiseSomethingElse();
}).then(function(res) {
   return promiseOneMoreThing();
}).then(function(res) {
   console.log('success');
}).fail(function(err) {
   console.log('something happened');
});
Now say I want to insert a codnition into my first then clause, and only return a promise on success and get out of hte promise chain on exit, how would I do this?

code:
promiseSomething(arg)
.then(function(res) {
   if(res) return promiseSomethingElse();
   else //I don't know what to do here, but I don't want to enter into any of the other then statements, 
   //and doing nothing still drops me into the next 'then' in the promise chain.
}).then(function(res) {
   return promiseOneMoreThing();
}).then(function(res) {
   console.log('success');
}).fail(function(err) {
   console.log('something happened');
});
Is the only way around this to nest another promise chain inside the first conditional?

Just throw an error when you want to get off the ride.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Don't use errors for flow control, IMO. I would have the conditional either kick off a promise chain if it wants to do more work, or return otherwise. It also puts the definition of the subsequent promises subordinate to the condition on which they're predicated, which IMO is much clearer.

Knifegrab
Jul 30, 2014

Gadzooks! I'm terrified of this little child who is going to stab me with a knife. I must wrest the knife away from his control and therefore gain the upperhand.

Subjunctive posted:

Don't use errors for flow control, IMO. I would have the conditional either kick off a promise chain if it wants to do more work, or return otherwise. It also puts the definition of the subsequent promises subordinate to the condition on which they're predicated, which IMO is much clearer.

Yeah error for anything other than actual error reporting (and this is functional branching, it is allowed to be one or the other value) seems like a bad idea. I ended up just nesting a promise structure inside the promise chain, that worked best, though I am not in love with the solution it seems to be the best approach less I continuously pass the values to the next handlers and do endless checking therein.

Huzanko
Aug 4, 2015

by FactsAreUseless

Knifegrab posted:

Yeah error for anything other than actual error reporting (and this is functional branching, it is allowed to be one or the other value) seems like a bad idea. I ended up just nesting a promise structure inside the promise chain, that worked best, though I am not in love with the solution it seems to be the best approach less I continuously pass the values to the next handlers and do endless checking therein.

What do you nerds think about using PostgreSQL with Node and Express and Angular? I know Mongo is poo poo (or so I've heard) so I'm looking for something else.

:gizz: PEAN Stack?

Also, thoughts on http://docs.sequelizejs.com/en/latest/ ?

Huzanko fucked around with this message at 01:58 on Oct 22, 2015

Odette
Mar 19, 2011

Noam Chomsky posted:

What do you nerds think about using PostgreSQL with Node and Express and Angular? I know Mongo is poo poo (or so I've heard) so I'm looking for something else.

:gizz: PEAN Stack?

Also, thoughts on http://docs.sequelizejs.com/en/latest/ ?

A lot of people seem to use sequelizeJS, but there are others like bookshelfJS.

I'm also in the process of switching from Mongo to postgreSQL, but I haven't decided on a library yet.

Huzanko
Aug 4, 2015

by FactsAreUseless

Odette posted:

A lot of people seem to use sequelizeJS, but there are others like bookshelfJS.

I'm also in the process of switching from Mongo to postgreSQL, but I haven't decided on a library yet.

Thanks! Nice to know it's a thing that people do. I only ever hear about Express Node and such in the context of using Mongo, which I'm told is poo poo garbage, so that's kept me away from the stack. However, I'm a JS dev so I kinda want to build some Node apps, rather than go back to Rails again, or pick up something totally new, again, for my webapp needs.

Huzanko
Aug 4, 2015

by FactsAreUseless
What about RethinkDB?

Skandranon
Sep 6, 2008
fucking stupid, dont listen to me

Noam Chomsky posted:

What about RethinkDB?

As long as it fits your data model, you're fine.

And unless you are trying to plan the architecture for a potentially large system... you're over thinking it. Especially if you are just going to be working on some personal apps. Pick whatever looks fun and get to work, stop malingering.

Huzanko
Aug 4, 2015

by FactsAreUseless

Skandranon posted:

As long as it fits your data model, you're fine.

And unless you are trying to plan the architecture for a potentially large system... you're over thinking it. Especially if you are just going to be working on some personal apps. Pick whatever looks fun and get to work, stop malingering.

It's an app for a tabletop RPG I'm designing. I mean, I'd like to just go with Mongo since I've gotten reasonably familiar with it but I don't know if schemaless will hurt me in this kind of app.

Also, I don't know much SQL.

Skandranon
Sep 6, 2008
fucking stupid, dont listen to me

Noam Chomsky posted:

It's an app for a tabletop RPG I'm designing. I mean, I'd like to just go with Mongo since I've gotten reasonably familiar with it but I don't know if schemaless will hurt me in this kind of app.

Also, I don't know much SQL.

I doubt Mongo will seriously hurt you there, just go for it.

Huzanko
Aug 4, 2015

by FactsAreUseless

Skandranon posted:

I doubt Mongo will seriously hurt you there, just go for it.

Thanks! I've kinda been hand-wringing over it.

Knifegrab
Jul 30, 2014

Gadzooks! I'm terrified of this little child who is going to stab me with a knife. I must wrest the knife away from his control and therefore gain the upperhand.

Noam Chomsky posted:

What do you nerds think about using PostgreSQL with Node and Express and Angular? I know Mongo is poo poo (or so I've heard) so I'm looking for something else.

:gizz: PEAN Stack?

Also, thoughts on http://docs.sequelizejs.com/en/latest/ ?

I use this minus angular. But node+express+postgres works wonderfully. I am not really familiar with angular JS, I think its a promise library? If it is I use Q instead. If its like Jquery I use jquery instead.

IronDoge
Nov 6, 2008

Knifegrab posted:

I use this minus angular. But node+express+postgres works wonderfully. I am not really familiar with angular JS, I think its a promise library? If it is I use Q instead. If its like Jquery I use jquery instead.

Angular is a javascript framework for web apps. It's has a promise library built in and it is better than using pure jQuery for controlling the UI IMO. It's pretty nifty generating entire components of a page using a few lines of code.

Example: You have an array of objects containing images and text you need to output to a list
code:
<ul ng-repeat="item in itemArr">
	<li>
		<img ng-src="item.imgUrl">
		<span class="caption">{{item.caption}}</span>
	</li>
</ul>
There's alot more it can do. There's a bit of a learning curve at first, but once it clicks Angular can be pretty cool. Alternatively, from what I've read, React.js is a lighter version of Angular and could also be something else you would want to look at. It cuts out some of the fat you might not need from Angular.

Munkeymon
Aug 14, 2003

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



Angular is a kitchen sink MVC library. React is just a very efficient templating/DOM manipulation library that you could even use with Angular if you really wanted.

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

IronDoge posted:

Angular is a javascript framework for web apps. It's has a promise library built in and it is better than using pure jQuery for controlling the UI IMO. It's pretty nifty generating entire components of a page using a few lines of code.


jQuery and Angular aren't even comparable. One is a library, with the original goal of making cross-browser JS consistent; Angular is an entire MVC framework.

Knifegrab
Jul 30, 2014

Gadzooks! I'm terrified of this little child who is going to stab me with a knife. I must wrest the knife away from his control and therefore gain the upperhand.
I think I will definitely be pursuing React and react routing in my next step of development.

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

Knifegrab posted:

I think I will definitely be pursuing React and react routing in my next step of development.

Do it. React has been a pleasure to work with.

stoops
Jun 11, 2001
I'm getting the mouse coordinates of an image. 0,0 starts at top left corner.

Is it possible to get 0,0 to start at bottom left?

Skandranon
Sep 6, 2008
fucking stupid, dont listen to me

stoops posted:

I'm getting the mouse coordinates of an image. 0,0 starts at top left corner.

Is it possible to get 0,0 to start at bottom left?

No, that's not how it works. You can adjust your calculations based on the co-ords and the width/height, but you aren't going to get the browser to give them differently.

Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.
You should also get over the math-nerd obsession with the position of the origin and get used to 0,0 being the top left of the image, because this has been standard in computer graphics since the dawn of time.

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Vulture Culture posted:

You should also get over the math-nerd obsession with the position of the origin and get used to 0,0 being the top left of the image, because this has been standard in computer graphics since the dawn of time.

Isn't OpenGL bottom-left?

Adbot
ADBOT LOVES YOU

Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.

Subjunctive posted:

Isn't OpenGL bottom-left?
3D is Cartesian for sanity, 2D generally isn't, for terrible reasons.

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