Javascript help

From: Wattsy (SLAYERPUNX)25 Jun 2010 13:14
To: ALL1 of 47
I am learning java script for my OU course and have hit brickwall.

Can any kind fold help me please?

I am trying to write a function for this statement:
*switches current player
*
*function takes no argument
* if current player is 1, sets current player to 2
* otherwise sets current player to 1
*function returns no value

So far I have:

code:
function switchCurrentPlayer()
{
	if (currentPlayer = 1)
	   {
	       currentPlayer = 2;
        }
            else
                {
                    currentPlayer = 1;
                }
}


Using this code to test I keep getting player 2

code:
switchCurrentPlayer();
    document.write('player ' + currentPlayer + '<br>');
switchCurrentPlayer();
    document.write('player ' + currentPlayer + '<br>');


Anyone point me in the right direction? I am going a bit screen blind.
From: Drew (X3N0PH0N)25 Jun 2010 13:20
To: Wattsy (SLAYERPUNX) 2 of 47
You need to be using == not =

= sets a value
== compares values

So your:
if (currentPlayer = 1)

is always returning true, and is setting current player to 1
(and then because it tests true (i.e. the script is able to set current player to 1) it is then being set to 2, by the enclosed code.

code:
function switchCurrentPlayer()
{
	if (currentPlayer == 1)
	   {
	       currentPlayer = 2;
        }
            else
                {
                    currentPlayer = 1;
                }
}


Would work.

Neater code to do the same thing:

code:
function switchCurrentPlayer() {
    currentPlayer == 1 ? currentPlayer = 2: currentPlayer = 1;
}


syntax for that is like:

[if this bit is true] ? [do this] : [if not, do this];
From: Wattsy (SLAYERPUNX)25 Jun 2010 13:53
To: Drew (X3N0PH0N) 3 of 47

You sir, are a star! Thank you. This is all new to me and programming is not my strong point. Do you mind if I PM you for further help if needed?

 

I can offer biscuits with owl shapes on them as a reward!

From: Drew (X3N0PH0N)25 Jun 2010 13:55
To: Wattsy (SLAYERPUNX) 4 of 47
Don't mind at all.

Though PB ad others might be more help if stuff gets advanced.
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?