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
Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Gordon Cole posted:

Cool, so I guess all static analysis tools are completely useless. And what's the deal with compiler warnings?! It's not like a computer is going to find any problems with our code, so why even bother!

The most important thing he is actually saying is "Global variables are totally cool and the more you use them, the better"

Gordon Cole posted:

The "only real problem" with leaving out semicolons that he mentions is actually a pretty big problem:

This is my favorite part. I'd say that "always use semicolons" is a bit easier to remember and less error prone than "never use semicolons, except..."

Adbot
ADBOT LOVES YOU

Optimus Prime Ribs
Jul 25, 2007

akadajet posted:

I was looking at GitHub's Javascript Styleguide and noticed this...

GitHub posted:

Do your best to never use a semicolon. This means avoiding them at line breaks and avoiding multi-statement lines.

Holy gently caress. :stare:
I really don't know how else to respond to that. I've always though of JavaScript's optional semicolons as a poorly thought out feature that is best left ignored/pretending it doesn't exist.

Suspicious Dish posted:

JSLint is a piece of opinionated garbage. I mean, it has a goatse as its logo.

Hahahah. I don't know how I've never noticed that before (not that I actually use JSLint as I find it to be pretty superfluous).

e:

http://mislav.uniqpath.com/2010/05/semicolons/ posted:

Look at some obfuscated code; there are semicolons in there. Does it help readability? No, but what would really help is a lot of whitespace and original variable names.

I get the point he's trying to make, but still... :psyduck:

Optimus Prime Ribs fucked around with this message at 10:41 on Dec 13, 2012

Mogomra
Nov 5, 2005

simply having a wonderful time

Optimus Prime Ribs posted:

Holy gently caress. :stare:
I really don't know how else to respond to that. I've always though of JavaScript's optional semicolons as a poorly thought out feature that is best left ignored/pretending it doesn't exist.
Is it even really a "feature" as much as it was one browser assuming that people writing JavaScript are mouth-breathing sub-humans, and then every other browser thinking they needed to do that too or else they'd break the internet?

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

Mogomra posted:

Is it even really a "feature" as much as it was one browser assuming that people writing JavaScript are mouth-breathing sub-humans, and then every other browser thinking they needed to do that too or else they'd break the internet?
ASI was an intentional part of the original design of Javascript. It just wasn't quite done correctly since Eich only had a few days to hack together Javascript.

Mogomra
Nov 5, 2005

simply having a wonderful time

Plorkyeran posted:

ASI was an intentional part of the original design of Javascript. It just wasn't quite done correctly since Eich only had a few days to hack together Javascript.
I really like JavaScript, but knowing that makes me feel bad. :cripes:

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Plorkyeran posted:

ASI was an intentional part of the original design of Javascript. It just wasn't quite done correctly since Eich only had a few days to hack together Javascript.

Leaving off semicolons was intended. ASI was a hack thrown in at the last minute.

I like the idea of leaving off semicolons, but ASI is just too risky.

Wozbo
Jul 5, 2010

Karthe posted:

I'm in the middle of making a web-based "connect the colored, numbered dots to make a picture" game - think PathPix or Link-A-Pix. It's a simple thing at the moment, with all of the JS, CSS, and HTML contained within one HTML document. Things have been going well - I've got path validation figured out, as well as the method by which I can generate a playing field of variable size and then lay out a sequence of predetermined colored number points for the user to connect.

Unfortunately I've hit a wall - I can't figure out how best to keep track of the path of each line. I need to be able to store every cell used in a line so that if a user retraces their steps to redraw a line, I can sanity-check their movements to make sure they're moving properly according to the game rules. Also, if a user completes a line but needs to redraw, I want to enable them to double-click on the beginning or end of a completed line to reset every cell used in that line as though they were never traced on.

I'm thinking of using an array for each line unique - when a user draws a line segment, provided the movement passes validation, that cell's ID will be put into an array after its preceding cell's ID. If a user retraces their steps, each cell they retrace their steps from is removed sequentially from the array. Here's a pseudocode example of an array containing a list of cells in a completed line:

code:
array{'c1-1', 'c1-2', 'c1-3', 'c1-4'} // c1-1 and c-14 are the start and end points of the line respectively.
If a user wants to retrace the above line to go down from c1-2 instead of right, they'd need to be able to either A) tap on c1-1 or c1-4 and backtrace through c1-3 and then c1-2, or B) double-tap on either c1-1 or c1-4 to simultaneously clear out cells c1-1, c1-2, c1-3, and c1-4 as though the line were never drawn.

It sounds simple enough, but I'm having the most trouble deciding on how to organize a multi-level array to store all of this information - I need to be able to store the length and color of each line as well so I can reference it as part of the move validation function. Once arrays get two or three levels deep my head starts to spin :sweatdrop:

What's the best way of going about this? Is an multi-level array overkill, or even too limiting?

I'd make an array of Objects referencing an array + meaningful info. When someone makes a new path from a previous point, push a new Object into the paths superarray with the elements you wish to keep etc. Have each object also store its index.

Note: If you ever shift() the array you will need to update that reference.

IAmKale
Jun 7, 2007

やらないか

Fun Shoe

Wozbo posted:

I'd make an array of Objects referencing an array + meaningful info. When someone makes a new path from a previous point, push a new Object into the paths superarray with the elements you wish to keep etc. Have each object also store its index.

Note: If you ever shift() the array you will need to update that reference.
Are you talking about something like this?

code:
SuperArray['c1-2'] = cell={preceeding:"c1-1",color:"red",length:4};
SuperArray['c1-3'] = cell={preceeding:"c1-2",color:"red",length:4};
The way I see it, every occupied cell in the grid would have an entry in SuperArray[] (referenced by the cell's ID) that I could then trace back through using a cell object's cell.preceeding property to iterate through SuperArray[] until I get to the start or end of a line.

IAmKale fucked around with this message at 19:25 on Dec 13, 2012

Wozbo
Jul 5, 2010
No, more like:

code:
ArrayPaths = 
[
	{Name: 'pathfoo', color: '#FFFFFF', refindex: 0, path: ['1', '2', '3', ...], etc data}, 
	{Name: 'pathbar', color: '#F0F0F0', refindex: 1, path: ['1', '2', '4', ...], etc data}
];
ArrayPaths[0] is the first path and all the data surrounding it
ArrayPaths[1] is the next etc etc.

E: I'm not gonna quote it all, but quote the Keys and you also have valid, serializable JSON object if you want to be able to send a saved state to someone else.

Wozbo fucked around with this message at 19:46 on Dec 13, 2012

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?
I'm writing a javascript game and I want to use right click as a control. I can prevent the context menu from appearing by doing

JavaScript code:
canvas.addEventListener("contextmenu", 
function (e) {
     e.preventDefault();
     return false;
});
But the "click" event doesn't register when I right click.

IAmKale
Jun 7, 2007

やらないか

Fun Shoe

HappyHippo posted:

I'm writing a javascript game and I want to use right click as a control. I can prevent the context menu from appearing by doing

JavaScript code:
canvas.addEventListener("contextmenu", 
function (e) {
     e.preventDefault();
     return false;
});
But the "click" event doesn't register when I right click.
return false() is cancelling the click entirely. Get rid of it and then do whatever you want after e.preventDefault().

IAmKale fucked around with this message at 21:30 on Dec 13, 2012

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?
So basically "click" won't register on right clicks, and I have to rely on "contextmenu" for right click events?

IAmKale
Jun 7, 2007

やらないか

Fun Shoe

HappyHippo posted:

So basically "click" won't register on right clicks, and I have to rely on "contextmenu" for right click events?
You can determine what mouse button has been clicked on a mouse event, just check e.which == 3 for right-click (1 is left, 2 is middle).

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

Karthe posted:

You can determine what mouse button has been clicked on a mouse event, just check e.which == 3 for right-click (1 is left, 2 is middle).

Yeah that's what I attempted to do but no luck, the event doesn't even fire so I don't get to that point. It seems to work if I just handle the contextmenu event and pass the event argument to the click event handler like this:
JavaScript code:
canvas.addEventListener("click", onClick, false);
canvas.addEventListener("contextmenu", onContextmenu, false);

...

function onContextmenu(e) {
	e.preventDefault();
	onClick(e);
}
function onClick(e) {
	if(e.which == 1) {
		//left click
	} else if(e.which == 3) {
		//right click
	}
}

Destroyenator
Dec 27, 2004

Don't ask me lady, I live in beer

Gordon Cole posted:

This is my favorite part of the linked article:
I was sure it was a poorly executed satire until I read his other posts. :sigh:

pksage
Jul 2, 2009

You are an experience!
Make sure you're a good experience.

Gazpacho posted:

If Ext.util.CSS does not allow you to modify your static rules, it should allow you to copy them into runtime rules that you can modify (and would override the static ones through cascading).

Ext.util.CSS worked perfectly. Thank you so much!

IAmKale
Jun 7, 2007

やらないか

Fun Shoe
JavaScript code:
function getLastSegment(cellID, goBack)
{
	[snip]
	if(goBack === false)
	{
		//alert(goBack + ", " + cellID);
		if(objCellArray[cellID].next)
		{
			alert("next detected, recursing on " + cellID);
			getLastSegment(objCellArray[cellID].next, false);
		}
		else
		{
			alert("next is null, alerting " + cellID);
			return cellID; // <--- This fucker right here keeps returning "undefined"
		}					
	}
	[snip]
}
Why the hell does my function return undefined for cellID on Line 14? The expected value is visible when I alert() cellID on the line above it, but no matter what I do I always get undefined when I try to return the variable for use elsewhere.

IAmKale fucked around with this message at 21:35 on Dec 14, 2012

karms
Jan 22, 2006

by Nyc_Tattoo
Yam Slacker
edit: nvm.

Wozbo
Jul 5, 2010
Post the rest of your function and some sample structure, recursion can be a bitch sometimes and I bet you its a subtle problem on the unwind.

Easy way to do it: give me a JSON.stringify(foo) of what you do on the first call.

IAmKale
Jun 7, 2007

やらないか

Fun Shoe

Wozbo posted:

Post the rest of your function and some sample structure, recursion can be a bitch sometimes and I bet you its a subtle problem on the unwind.

Easy way to do it: give me a JSON.stringify(foo) of what you do on the first call.

Here's the entire function, as well as the object that I want to parse through with the recursive function:
JavaScript code:
objCellArray["c1-1"] = {last:null, next:"c1-2", lineLength:4, lineColor:"#FF0000", endPoint:true};
objCellArray["c1-2"] = {last:"c1-1", next:"c1-3", lineLength:4, lineColor:"#FF0000", endPoint:false};
objCellArray["c1-3"] = {last:"c1-2", next:"c1-4", lineLength:4, lineColor:"#FF0000", endPoint:false};
objCellArray["c1-4"] = {last:"c1-3", next:null, lineLength:4, lineColor:"#FF0000", endPoint:true};

function getLastSegment(cellID, goBack)
{
	if(goBack === true)
	{
		if(objCellArray[cellID].last)
		{
			getLastSegment(objCellArray[cellID].last, true);
		}
		else
		{
			return cellID;
		}
	}
	else if(goBack === false)
	{
		//alert(goBack + ", " + cellID);
		if(objCellArray[cellID].next)
		{
			//alert("next detected, recursing on " + cellID);
			getLastSegment(objCellArray[cellID].next, false);
		}
		else
		{
			//alert("next is null, alerting " + cellID);
			return cellID;
		}					
	}
	else
	{
		alert("error occurred: " + goBack);
	}
}

alert(getLastSegment("c1-3", false));
I'd give you the other thing you requested but I've never done anything with JSON before. The best I could come up with from what you said was to do
JavaScript code:
JSON.stringify(objCellArray["c1-3"])
but that just outputs the parameters. :sweatdrop:

Wozbo
Jul 5, 2010
Looking it over. First thoughts:

Null and undefined aren't the same thing. I'd do this:

if(objCellArray[cellID].next != null && objCellArray[cellID].next != undefined)

for your checks.

As for the second part, giving the parameters is what json.stringify would have done. You just included it at the top so I didn't need it again.


EDIT AHHAHAH, heres your problem. put this: return getLastSegment(objCellArray[cellID].next, false);

Totally overlooked it. When you unwind you aren't returning the value.

E: Working code

code:
objCellArray["c1-1"] = {last:null, next:"c1-2", lineLength:4, lineColor:"#FF0000", endPoint:true};
objCellArray["c1-2"] = {last:"c1-1", next:"c1-3", lineLength:4, lineColor:"#FF0000", endPoint:false};
objCellArray["c1-3"] = {last:"c1-2", next:"c1-4", lineLength:4, lineColor:"#FF0000", endPoint:false};
objCellArray["c1-4"] = {last:"c1-3", next:null, lineLength:4, lineColor:"#FF0000", endPoint:true};

function getLastSegment(cellID, goBack)
{
    if(goBack === true)
    {
        if(objCellArray[cellID].last)
        {
            return getLastSegment(objCellArray[cellID].last, true);
        }
        else
        {
            return cellID;
        }
    }
    else if(goBack === false)
    {
        //console.log(goBack + ", " + cellID);
        if(objCellArray[cellID].next != null && objCellArray[cellID].next != undefined)
        {
            //console.log("next detected, recursing on " + cellID);
            return getLastSegment(objCellArray[cellID].next, false);
        }
        else
        {
            //console.log("next is null, //console.loging " + cellID);
            return cellID;
        }					
    }
    else
    {
        //console.log("error occurred: " + goBack);
    }
}

console.log(getLastSegment("c1-3", false));
E3: I still don't see why you are structuring this way. I'd honestly do something like this:

code:
objActivePath = new Object() //holds ref to current working path
objPathArray = new Array();
objPathArray.push({lineLength:4, lineColor:"#FF0000",path: ["c1-1", "c1-2", "c1-3", "c1-4"]});
objActivePath = objPathArray[0];

Wozbo fucked around with this message at 22:26 on Dec 14, 2012

IAmKale
Jun 7, 2007

やらないか

Fun Shoe
Oh man, that was my problem? :doh: But what does adding return to the recursive function call do? I thought I only needed to add return to the information I wanted to get back from the function.

Wozbo
Jul 5, 2010
Think about this, when you return, what are you doing with the returned value? The second you go one level up, you have the result and promptly do nothing with it. You should return back up now that you have something meaningful.

E: Code example
code:
Function foo(){
	bar();
}

Function bar(){
	return 3;
}
If you call "foo();" you will have no result because you aren't doing anything with it. you need to "return bar();" Now that the function is recursive, all you are doing is returning on yourself.

Simple example:
code:
function recurse(i)
{
	if (i >= 10)
		return i;
	else
		return recurse(i + 1); 
//without the return, 
//you are unwinding by not returning anything, aka undefined.  
//The second you return, you have a result, 
//but the your function continues until the end of the code block and does nothing with it.
}

recurse(5); // should return 10

Wozbo fucked around with this message at 22:36 on Dec 14, 2012

IAmKale
Jun 7, 2007

やらないか

Fun Shoe

Wozbo posted:

Think about this, when you return, what are you doing with the returned value? The second you go one level up, you have the result and promptly do nothing with it. You should return back up now that you have something meaningful.
Ah, that makes sense. If I understand correctly, I'm not so much returning a value as I am an entire function call. If I don't return the function, then the remaining recursions occur inside the first function and the value does nothing because its never passed off as the result.

But why did cellID in the alert() return the expected value?

IAmKale fucked around with this message at 22:44 on Dec 14, 2012

Wozbo
Jul 5, 2010
No, you are returning the result of that function call up a level. The call is evaluated, then raturned up a level, which is the evaluation, so returned up a level, etc. etc.

You could do:

var temp = getLastSegment(objCellArray[cellID].next, false);
return temp;

And it would work too. Its just a waste of resources/ I just don't think that way.

You aren't doing anything with the value that is returned and then the function completes without returning anything in your original code.

IAmKale
Jun 7, 2007

やらないか

Fun Shoe

Wozbo posted:

You aren't doing anything with the value that is returned and then the function completes without returning anything in your original code.
Ah, I get it now. Thanks for the help!

And sorry if I sound obtuse, I just want to make sure I understand the why moreso than just "remember to add return". :downs:

Wozbo
Jul 5, 2010
No problemo. Recursion can be mighty confusing at times.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Karthe posted:

JavaScript code:
alert(getLastSegment("c1-3", false));

Are you using your browser's Javascript debugger? Because if you are not, you should(*.

Using alert like that is pretty 2002.

* Depending on browser:
In Chrome and Opera, right click somewhere on the page and select "inspect element"
in Internet Explorer, press F12
in Firefox, you need to install Firebug, then right click anywhere and select Inspect element with Firebug

You can set breakpoints and watches and such in the debugger and see what the values of any variables are without having to "alert" them.

IAmKale
Jun 7, 2007

やらないか

Fun Shoe

Wheany posted:

Are you using your browser's Javascript debugger? Because if you are not, you should(*.

Using alert like that is pretty 2002.

* Depending on browser:
In Chrome and Opera, right click somewhere on the page and select "inspect element"
in Internet Explorer, press F12
in Firefox, you need to install Firebug, then right click anywhere and select Inspect element with Firebug

You can set breakpoints and watches and such in the debugger and see what the values of any variables are without having to "alert" them.
Yeah, you're right...those were just easier for me to work with. I'd never tried until now to use Chrome's advanced debugging tools under the Source tab, but now that I've discovered it and figured out how to load up a script for analysis, I'll end up using it more often.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

Karthe posted:

Yeah, you're right...those were just easier for me to work with. I'd never tried until now to use Chrome's advanced debugging tools under the Source tab, but now that I've discovered it and figured out how to load up a script for analysis, I'll end up using it more often.

alert() is not easier to work with than console functions, though. Like if you want to output the value of x and y in this loop:
JavaScript code:
var x, y;
for(y = 0; y < 10; y++) {
	for(x = 0; x < 10; x++) {
	console.log('x:' + x + ' y:' + y);
		if (x === 10 && y === 10) {
			console.info("lol, off by one");
		}
		if (isNaN(x) || isNaN(y)) {
			console.error('the laws of mathematics have changed suddenly and x or y has become NaN');
		}
		if (x === 6 && y === 6) {
			console.warn('look out, only one more 6 before the dark lord rises');
		}
	}
}
Imagine having to click ok on that alert window for 100 times only to realize that x and y are never 10, instead of looking at your console and seeing 100 lines of output, which are output instantly without you having to keep on clicking.

And besides log(), console has info(), warn() and error(), which you can use to print out different kinds of messages. They show up in different color, so as soon as you see red in your console, you know there was some error somewhere.

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.
Yeah, just DON'T use alert. It is always a slower debug tool than the variety of modern options you now have. If you need to be able to stop and analyze you can use breakpoints in your browser or drop:

code:
debugger;
into your code.

pksage
Jul 2, 2009

You are an experience!
Make sure you're a good experience.
I am a huge sucker for debugger;. Being able to check out the contents of your maps and stuff during runtime is priceless.

Blinkz0rz
May 27, 2001

MY CONTEMPT FOR MY OWN EMPLOYEES IS ONLY MATCHED BY MY LOVE FOR TOM BRADY'S SWEATY MAGA BALLS

pksage posted:

I am a huge sucker for debugger;. Being able to check out the contents of your maps and stuff during runtime is priceless.

Or you could just set a breakpoint in whatever debugging software you use. I don't quite see the point of adding an extra line of code, especially when it could cause a lot of issues if it ends up being left in.

corgski
Feb 6, 2007

Silly goose, you're here forever.

Blinkz0rz posted:

Or you could just set a breakpoint in whatever debugging software you use. I don't quite see the point of adding an extra line of code, especially when it could cause a lot of issues if it ends up being left in.

Or you could block any commits that contain "debugger;" and that way you still get the benefit.

You are using version control, right?

pksage
Jul 2, 2009

You are an experience!
Make sure you're a good experience.

thelightguy posted:

Or you could block any commits that contain "debugger;" and that way you still get the benefit.

You are using version control, right?

Yup, I have the ExtJS compiler leave out all debugger lines. For some reason, one of my source files keeps getting compressed down to one line in Chrome's debugger (before running any compressors on it), so I can't set a breakpoint without using debugger. I should probably figure out what's doing that instead of using debugger, but :effort:

Edly
Jun 1, 2007

pksage posted:

Yup, I have the ExtJS compiler leave out all debugger lines. For some reason, one of my source files keeps getting compressed down to one line in Chrome's debugger (before running any compressors on it), so I can't set a breakpoint without using debugger. I should probably figure out what's doing that instead of using debugger, but :effort:

If you click the "pretty print" (curly braces) icon at the bottom of the Chrome debugger, it'll auto-format the code, which breaks it onto separate lines.

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?
Is is possible to add a clip region mid path, so that it only affects everything after the clip region is defined? And how does this interact with the nonzero winding rule? I'm getting some strange results and I'm wondering if there's any info on this. Google isn't helping me.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
I thought the clip region only applied at fill/stroke time. It's possible it's undefined. Report an issue to the WHATWG about it, and they'll clear up the specs.

LordAndrew
Jul 13, 2009

Walking death machine equipped with the knowledge of Nuclear.
JSLint updated a few days ago to always give you poo poo if you use tabs instead of spaces. The commit message on GitHub just says "use_spaces". I like JSLint because I write all of my JavaScript in Notepad++ and then run it through to see how badly I messed up, but it keeps getting stricter and more opinionated. :eng99:

Adbot
ADBOT LOVES YOU

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
I think my favorite is still "stupid". It used to complain to you if you had a function that contained "Sync" anywhere in its name (indexOf("Sync") > -1), but it was changed so that it only matches names ending in "Sync".

But not before a coworker decided that instead of using the "stupid" option, he renamed a function named something like "checkSynchronizationComplete" to something else.

Of course the option's documentation is "tolerate stupidity" instead of, let's say, "tolerate synchronous functions." Of course if the documentation said that, he might have to change the option to "synchronous" or something.

e: Wait, so using tabs is not even an option? It just straight-up does not allow the tab character anywhere? :laffo:

Wheany fucked around with this message at 08:19 on Dec 17, 2012

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