SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Oct 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript If Else statement not working with global variable

    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;
    }

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    The = sign is assigning the string to the playerLocation variable, and always reporting true because it successfully performed the assignment.

    You need instead to use === which performs a comparison between playerLocation and the string.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,276
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    I didn't finish reading the code because I did see a big error:

    Code:
    function north()
    {
    if(playerLocation = "zone") {
    No, see, you want to check to see IF the player is in "zone" (where they started), but that's not what your code is doing. Your code is saying "if true" because you are re-assigning "zone" to playerLocation.

    I'm not sure how far the program is going with that, so I'm not sure what zone the player is actually in, but they are likely not moving from zone to zone at all.

    Change your = to == or === in your if conditions and see where you get then.

    *edit or what Paul said


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •