Javascript help

From: 99% of gargoyles look like (MR_BASTARD)25 Jun 2010 14:06
To: Drew (X3N0PH0N) 5 of 47

FORUM HELPFUL AWARD!

 

(worship)

From: Drew (X3N0PH0N)25 Jun 2010 14:06
To: 99% of gargoyles look like (MR_BASTARD) 6 of 47
^____^

<treasures>
From: koswix25 Jun 2010 14:10
To: Drew (X3N0PH0N) 7 of 47
<burries>
From: 99% of gargoyles look like (MR_BASTARD)25 Jun 2010 14:12
To: koswix 8 of 47
<berries>
From: Drew (X3N0PH0N)25 Jun 2010 14:15
To: koswix 9 of 47
<berets>
From: koswix25 Jun 2010 14:22
To: Drew (X3N0PH0N) 10 of 47
<berates>
From: Drew (X3N0PH0N)25 Jun 2010 14:27
To: koswix 11 of 47
From: Peter (BOUGHTONP)25 Jun 2010 14:43
To: Wattsy (SLAYERPUNX) 12 of 47
Whilst what Xen says will work, it's possibly worth pointing out there is also a "===" operator.

The difference being that "==" will do type conversion to encourage a match, whilst "===" is stricter and requires same type too.

For example:
code:
var a = 0;
var b = '0';
 
document.write(a==b);
document.write('\n');
document.write(a===b);


Some people argue you should always do === (unless you explicitly need == instead), though I'm not sure how much it matters (possibly a bit slower for non-matches, but most likely not worth worrying about).
From: Peter (BOUGHTONP)25 Jun 2010 14:52
To: Wattsy (SLAYERPUNX) 13 of 47
Oh, and another thing:

code:
currentPlayer == 1 ? currentPlayer = 2: currentPlayer = 1;


I would write that like this:

code:
currentPlayer = (currentPlayer == 1) ? 2 : 1;


Because it's less repetitive, and thus less error-prone.

In this situation, you could also do "currentPlayer = 3-currentPlayer" which is even simpler (though more cryptic to understand).
From: Peter (BOUGHTONP)25 Jun 2010 15:00
To: Wattsy (SLAYERPUNX) 14 of 47
And one more post, in a last ditch attempt to not have to go write a really boring report thing - this is all based on there only ever being two players.

It's good practise to write more modular code that can be used for other purposes - for this example, multi-player turn-based switching can be done with very little extra work:

code:
function switchCurrentPlayer()
{
	currentPlayer++
 
	if ( currentPlayer > PlayerCount )
	{
		currentPlayer=1
	}
 
}


Set PlayerCount to any number, and this will cycle through them in order, whether there is 2 or 20 players.

(The "++" just means "add one")


Anyway, I better get on with wasting time doing what I'm supposed to be doing right now. :(
From: koswix25 Jun 2010 15:05
To: Drew (X3N0PH0N) 15 of 47
Don't show me these things when I am busy k thx :(
From: Drew (X3N0PH0N)25 Jun 2010 15:07
To: koswix 16 of 47
(giggle)
From: Wattsy (SLAYERPUNX)25 Jun 2010 15:16
To: Peter (BOUGHTONP) 17 of 47

Wow, just wow.

 

Peter, why are you not ruling the world, or even just Britain by now?

 

The last email made a lot more sense to me and is in the style of scripting that the OU is teaching.

 

Come one peter, become that evil genius we all know and love.

From: 99% of gargoyles look like (MR_BASTARD)25 Jun 2010 15:50
To: Peter (BOUGHTONP) 18 of 47
Huh? I don't get that. Surely to cycle through you'll need a while or for loop?
From: Peter (BOUGHTONP)25 Jun 2010 16:04
To: 99% of gargoyles look like (MR_BASTARD) 19 of 47
Possibly badly worded - by "cycle through" I just meant each time called you'd get the appropriate next player - you'd call the function at the end of each player's turn (which in most games is a manually triggered event).
From: Peter (BOUGHTONP)25 Jun 2010 16:07
To: Wattsy (SLAYERPUNX) 20 of 47
I don't know why. :(

Am I not allowed to be a non-evil one?
From: koswix25 Jun 2010 16:09
To: Peter (BOUGHTONP) 21 of 47
No, Google called dibs on that one.
From: Wattsy (SLAYERPUNX)25 Jun 2010 16:11
To: koswix 22 of 47
Google is teh evil, you just wait! They will have chickens with lasers on their heads before long.
From: koswix25 Jun 2010 16:13
To: Wattsy (SLAYERPUNX) 23 of 47
As long as they still do regular firmware updates for Android, who cares?
From: Wattsy (SLAYERPUNX)25 Jun 2010 16:15
To: koswix 24 of 47
My desire is lacking the 2.2 goodness, therefore they are evil.