This might be your problem:
function nextLoc(dir) {
var newLoc = nav[loclocal][dir];
if (newLoc >= 0) {
loclocal = newLoc;
} else {
displayMessage("You cannot go that way.");
}
disable_btns();
}
You check to make sure newLoc >= 0, but you don't check that it's < locations.length. Change it to this:
function nextLoc(dir) {
var newLoc = nav[loclocal][dir];
//changed following line:
if (newLoc >= 0 && newLoc < locations.length) {
loclocal = newLoc;
} else {
displayMessage("You cannot go that way.");
}
disable_btns();
}
edit: The problem (with this error) is that you are assigning the elements of the locations
array to variables that have not yet been defined.
var locations = new Array();
locations[0] = loclocal_0;
will assign undefined
to locations[0], because loclocal_0 is assigned later in the code.
As a quick fix, you could move
var locations = new Array();
...
locations[10] = loclocal_10;
to below:
var loclocal_10 = new rooms();
...
loclocal_10.hasItem = false
in the code. This will likely reveal other bugs, just a couple I noticed at a glance:
in function rooms()
: this.toString=this.discription;
contains a typo.
I recommend going through the code and adding semicolons at the end of every line where you are relying on automatic semicolon insertion currently, and indenting consistently.
Take a deep breath. This is your first year, so you're probably not expected to write good code at this point, just focus on stepping through the errors one at a time, and keep in mind that for future assignments, taking some time to learn best practices of the language you are using up front will save you from hard-to-track errors in the future.
Stackoverflow isn't really suited for debugging an entire project of this size, but I recommend that if you need personalized help (and don't have access to on-campus resources), you look into sites like instaedu, odesk, or freelancer to get someone to spend the time your program needs to get all the kinkds sorted out. One-on-one time with someone who will walk you through what you should be doing (like you might receive with instaedu) would be best for learning, whereas freelancer and odesk may be of limited usefulness if your goal is to learn how to write programs yourself.