CodingJavascript help

 

Press Ctrl+Enter to quickly submit your post
Quick Reply  
 
 
  
 From:  Wattsy (SLAYERPUNX)  
 To:  ALL
37533.1 
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.

Web Shite Mail Me

0/0
 Reply   Quote More 

 From:  Drew (X3N0PH0N)  
 To:  Wattsy (SLAYERPUNX)     
37533.2 In reply to 37533.1 
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];

0/0
 Reply   Quote More 

 From:  Wattsy (SLAYERPUNX)  
 To:  Drew (X3N0PH0N)     
37533.3 In reply to 37533.2 

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!

Web Shite Mail Me

0/0
 Reply   Quote More 

 From:  Drew (X3N0PH0N)  
 To:  Wattsy (SLAYERPUNX)     
37533.4 In reply to 37533.3 
Don't mind at all.

Though PB ad others might be more help if stuff gets advanced.

0/0
 Reply   Quote More 

 From:  99% of gargoyles look like (MR_BASTARD)  
 To:  Drew (X3N0PH0N)     
37533.5 In reply to 37533.2 

FORUM HELPFUL AWARD!

 

(worship)

bastard by name, bastard by nature

0/0
 Reply   Quote More 

 From:  Drew (X3N0PH0N)  
 To:  99% of gargoyles look like (MR_BASTARD)     
37533.6 In reply to 37533.5 
^____^

<treasures>

0/0
 Reply   Quote More 

 From:  koswix  
 To:  Drew (X3N0PH0N)     
37533.7 In reply to 37533.6 
<burries>


GIVE ME MEAT! :@ msg:37418.1
0/0
 Reply   Quote More 

 From:  99% of gargoyles look like (MR_BASTARD)  
 To:  koswix     
37533.8 In reply to 37533.7 
<berries>

bastard by name, bastard by nature

0/0
 Reply   Quote More 

 From:  Drew (X3N0PH0N)  
 To:  koswix     
37533.9 In reply to 37533.7 
<berets>

0/0
 Reply   Quote More 

 From:  koswix  
 To:  Drew (X3N0PH0N)     
37533.10 In reply to 37533.9 
<berates>


GIVE ME MEAT! :@ msg:37418.1
0/0
 Reply   Quote More 

 From:  Drew (X3N0PH0N)  
 To:  koswix     
37533.11 In reply to 37533.10 
<beartato>

0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  Wattsy (SLAYERPUNX)     
37533.12 In reply to 37533.3 
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).
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  Wattsy (SLAYERPUNX)     
37533.13 In reply to 37533.2 
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).
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  Wattsy (SLAYERPUNX)     
37533.14 
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. :(
0/0
 Reply   Quote More 

 From:  koswix  
 To:  Drew (X3N0PH0N)     
37533.15 In reply to 37533.11 
Don't show me these things when I am busy k thx :(


GIVE ME MEAT! :@ msg:37418.1
0/0
 Reply   Quote More 

 From:  Drew (X3N0PH0N)  
 To:  koswix     
37533.16 In reply to 37533.15 
(giggle)

0/0
 Reply   Quote More 

 From:  Wattsy (SLAYERPUNX)  
 To:  Peter (BOUGHTONP)     
37533.17 In reply to 37533.14 

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.

Web Shite Mail Me

0/0
 Reply   Quote More 

 From:  99% of gargoyles look like (MR_BASTARD)  
 To:  Peter (BOUGHTONP)     
37533.18 In reply to 37533.14 
Huh? I don't get that. Surely to cycle through you'll need a while or for loop?

bastard by name, bastard by nature

0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  99% of gargoyles look like (MR_BASTARD)     
37533.19 In reply to 37533.18 
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).
0/0
 Reply   Quote More 

 From:  Peter (BOUGHTONP)  
 To:  Wattsy (SLAYERPUNX)     
37533.20 In reply to 37533.17 
I don't know why. :(

Am I not allowed to be a non-evil one?
0/0
 Reply   Quote More 

Reply to All  
 

1–20  21–40  41–47

Rate my interest:

Adjust text size : Smaller 10 Larger

Beehive Forum 1.5.2 |  FAQ |  Docs |  Support |  Donate! ©2002 - 2024 Project Beehive Forum

Forum Stats