PDA

View Full Version : Code Not Running as it should



seanw1up
Oct 14th, 2010, 06:40 AM
The first time I enter the command n it should run the first if statement, when I enter n it should then run the second one as its in zone two. However this is just not happening... What am I doing wrong. Its simply running the second one and skipping the first. Thanks in advance folks:)

// gameFunctions.js
// Javascript file for Game.html
// September 29th 2010 Edition


// The below represent our global variables for the game. The first variable
// is called playerScore, and represents the total earned points. The player
// earns or loses points based on progress in the game, this value can either
// increase or decrease.

var playerLocation = "zone";

function runProgram()
{
var command = "";
command = document.getElementById("txtCommand").value;
switch (command)
{
case "n": north();
break;
case "s": south();
break;
case "e": east();
break;
case "w": west();
break;
case "north": north();
break;
case "south": south();
break;
case "east": east();
break;
case "west": west();
break;
default: giveError();
}
}

// The following function when called will take the player north.

function north()
{
if(playerLocation = "zone")
{
var message = "You have come to what seems to be a beach";
post(message);
var playerLocation = "zoneTwo";
}
if(playerLocation = "zoneTwo")
{
var message = "You are unsure if you should set up camp. Set up camp?"
post(message);
var playerLocation = "zoneThree";
}
else
{
giveError();
}
}

// The following function when called will take the player east.

function east()
{
if(playerLocation = "zone")
{
var message = "You have come to a very large and steep mountain";
post(message);
var playerLocation = "zoneTwo";
}
if(playerLocation = "zoneTwo")
{
var message = "You are unsure if you should set up camp. Set up Camp?";
post(message);
var playerLocation = "zoneThree";
}
else
{
giveError();
}
}

// The following function when called will take the player south.

function south()
{
if(playerLocation = "zone")
{
var message = "You have come to the entrance of a mine";
post(message);
var playerLocation = "zoneTwo";
}
if(playerLocation = "zoneTwo")
{
var message = "You are unsure if you should set up camp. Set up Camp?";
post(message);
var playerLocation = "zoneThree";
}
else
{
giveError();
}
}

}

// The following function when called will take the player west.

function west()
{
if(playerLocation = "zone")
{
var message = "You have found the edge of the world";
post(message);
var playerLocation = "zoneTwo";
}
if(playerLocation = "zoneTwo")
{
var message = "You are unsure if you should set up camp. Set up Camp?";
post(message);
var playerLocation = "zoneThree";
}
else
{
giveError();
}
}

// The following function reports an error when the program deviates from standard functionality. It is used mainly for debugging.

function giveError()
{
alert("gameFunctions.js has encountered a critical error")
alert("Please check that you are not using Internet Explorer to run this page")
alert("The program will now close")
}

// The following function reports an error when the typed in command is not recognized by the program database.

function doNotUnderstand()
{
alert("I am sorry but I do not understand please try again")
}

function post(newText)
{
document.getElementById("taGameText").value = ""
var gameTextBox = document.getElementById("taGameText");
gameTextBox.value = newText + gameTextBox.value;
}

Philip M
Oct 14th, 2010, 09:23 AM
A good many errors, I am afraid. One error is an extra closing brace at the end of function south().

if(playerLocation = "zone") { // = means assignment, == means comparison.

var playerLocation = "zoneTwo"; // by using the var keyword more than once you in effect erase the existing playerLocation global variable and create a new (local) one with the same name which returns undefined. A local variable is available only to the function within which it was declared.

if(playerLocation = "zoneTwo") { // playerLocation will always be "zone Two" as you have just changed its value to that! And in any case you have assigned that value to the variable with =.

You could have found out many of these errors simply by introducing alerts at strategic points and inspecting the values of the variables.

Tip: always place an opening brace { on the same line as the statement, not on the following line.

When posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar which will insert the tags. You can (and should) edit your previous post.

BTW, the time to say "thanks" is afterwards, not beforehand which gives the - doubtless unintended - impression that you take other people's voluntary unpaid assistance and expertise for granted. Or as British politician Neil Kinnock put it, "Don't belch before you have had the meal." Prefer to use "please" beforehand and if you find a response helpful then you can use the "Thank User For This Post" button.


Quizmaster: The English name for each of the days in the week ends in which letter of the alphabet?
Contestant: D

Kor
Oct 14th, 2010, 12:45 PM
... and I think that the conditional process should be made with the exclusion of the precedents, at least to avoid an unnecessary running of the other conditions :


if(){}
else if(){}
else{}

seanw1up
Oct 14th, 2010, 03:11 PM
Thanks for the help, I think this fixed my issue, just now trying to figure out how to add another location based if statement and since I can't use and else else if thats not going to work for the moment.

Philip M
Oct 14th, 2010, 03:38 PM
It should be obvious that you are not going about this in the right way. You will soon be lost in a forest of multiple if..else statements. Have a look at arrays.