SirenKing
SirenKing

Reputation: 155

Javascript Global Variables Are Resetting

SUMMARY OF WHAT I"M TRYING TO DO: I've put together a very simple game that uses a do/while function and switch to check what room you are in. If you are in room = 1, the switch selects room1 and runs the room1() function. Inside the room, you can choose which direction to go. If you choose north or east, a door1() or door4() function will run to say "Would you like to open this door?" You can say "Open" and walk into the next room, and your room = 1 will update to a new value.

WHAT IS BROKEN: This is all working beautifully and the functions (while they are somewhat bloated) appear to be working as they should. The one major issue is that my variables are resetting whenever I walk through a door, so I am always in room 1, my compass always equals 0 and none of the doors or rooms equal "visited" (example: room1V = 0).

Basically, I walk through a door and I end up in room 1 again, instead of room 2 or 4.

This is the code, and a walkthrough:

    var room1V = 0; //room1  // these variables tell the computer whether I have 'visited' a room before.
    var room2V = 0; //room2
    var room3V = 0; //room3
    var room4V = 0; //room4

    var door1V = 0; //room1 - room2 // these variables tell the computer whether I have used a door before.
    var door2V = 0; //room2 - room3
    var door3V = 0; //room3 - room4
    var door4V = 0; //room4 - room1

    var compass = 0; // which side of the room am I on?
    var room = 1; // what room am I currently in?
    var reply = 1; // this is re-declared as a local variable in each function, and it works fine
    win = 0; // this will eventually tell the room-check do/while to stop

    // This do/while checks what room I am in:
    do {
        quit = 0;
        switch(room) {
            case '1':
                room1(compass,room1V);
                break;
            case '2':
                room2(compass,room2V);
                break;
            case '3':
                room3(compass,room3V);
                break;
            case '4':
                room4(compass,room4V);
                break;
        }
    } while (win != 1);

Since the default says room = 1, the room1(compass,room1V) function will start.

    function room1(compass,room1V) {
        if (room1V === 1) {
            console.log("You are in room 1 again.");
            document.write("You are in room 1 again." + "<br>");
            var reply = prompt("Where would you like to go? EAST, NORTH?");
            switch(reply.toLowerCase()) {
                case 'east':
                    compass = "east"; //because you are trying to open the east door, you will now see room1 from the east. If you make it through, your compass will update to 'west' because you will be on the west side of room2.
                    door1(compass,door1V);
                    break;
                case 'north':
                    compass = "north";
                    door4(compass,door4V);
                    break;
                default:
                    console.log("Something went wrong.");
                    document.write("Something went wrong." + "<br>");
            }
        } else {
            console.log("You are in room 1.");
            document.write("You are in room 1." + "<br>");
            room1V = 1;
            reply = prompt("Where would you like to go? EAST, NORTH?");
            switch(reply.toLowerCase()) {
                case 'east':
                    compass = "east";
                    door1(compass,door1V);
                    break;
                case 'north':
                    compass = "north";
                    door4(compass,door4V);
                    break;
                default:
                    console.log("Something went wrong.");
                    document.write("Something went wrong." + "<br>");
            }
        }
    }   // Working

If I go 'north,' the compass will update to say what direction I am viewing the room from, and door4(room,door4V) will run.

    function door4(room,door4V) {
        if (door1V === 1) {
            console.log("You approach door 4 again.");
            document.write("You approach door 4 again." + "<br>");
            var reply = prompt("What would you like to do? OPEN, QUIT?");
            switch(reply.toLowerCase()) {
                case 'open':
                    if (room === 4) {
                       room = 1;
                       compass = "north";
                       console.log("You walk through door 4 into room 1...");
                       document.write("You walk through door 4 into room 1..." + "<br>");
                    } else {
                       room = 4;
                       compass = "south";
                       console.log("You walk through door 4 into room 4...");
                       document.write("You walk through door 4 into room 4..." + "<br>");
                       }
                    quit = 1;
                    break;
                case 'quit':
                    quit = 1;
                    break;
                default:
                    console.log("Something went wrong.");
                    document.write("Something went wrong." + "<br>");
            }
        } else {
            console.log("You approach door 4.");
            document.write("You approach door 4." + "<br>");
            var reply = prompt("What would you like to do? OPEN, QUIT?");
            switch(reply.toLowerCase()) {
                case 'open':
                    if (room === 4) {
                       room = 1;
                       compass = "north";
                       console.log("You walk through door 4 into room 1...");
                       document.write("You walk through door 4 into room 1..." + "<br>");
                    } else {
                       room = 4;
                       compass = "south";
                       console.log("You walk through door 4 into room 4...");
                       document.write("You walk through door 4 into room 4..." + "<br>");
                    }
                    quit = 1;
                    break;
                case 'quit':
                    quit = 1;
                    break;
                default:
                      console.log("Something went wrong.");
                      document.write("Something went wrong." + "<br>");
            }
        }
    }       // Working

At this point, the do/while function is supposed to say, "Oh! Since room = 4, you are now in room 4." But that isn't what happens. Room does = 4, but when the do/while re-runs, I am back in room1, and all the variables appear to have reset.

Upvotes: 3

Views: 4831

Answers (3)

andre mcgruder
andre mcgruder

Reputation: 1520

You never change the value of the room variable to match the new room. It gets set in the global vars and does not change.

Update your room in your do while loop like this:

// This do/while checks what room I am in:
    do {
        quit = 0;
        switch(room) {
            case '1':
                room1(compass,room1V);
                room = 1;
                break;
            case '2':
                room2(compass,room2V);
                room = 2;
                break;
            case '3':
                room3(compass,room3V);
                room = 3;
                break;
            case '4':
                room4(compass,room4V);
                room = 4;
                break;
        }
    } while (win != 1);

Upvotes: 0

taveras
taveras

Reputation: 495

You are running into an issue with "variable shadowing" (Variable shadowing in JavaScript)

var fancy = 0;

function room1(fancy) {
  /* here, there are two variables with the name 'fancy' in scope */
  fancy = 1;
}

console.log(fancy); // 0
room1();
console.log(fancy); // 0

you can solve this by changing the room1 argument to have a different name:

function room1(otherFancy) { fancy = 1; } and now:

console.log(fancy); // 0
room1();
console.log(fancy); // 1

Upvotes: 2

deborah-digges
deborah-digges

Reputation: 1173

The reason your variables are "resetting" is because primitives in JavaScript are passed by value. In your do-while loop, you pass variables room1V .. room4V to functions room1 .. room4. The variables are primitives in JavaScript and are passed by value. This means any changes made to them in the called function will not reflect in the original argument. The same is true when you are calling the door4 function with compass and door4v: these variables are passed by value. To understand this better, try reading this chapter.

Upvotes: 4

Related Questions