-2
//updates display       
 function updateDisplay() {
    displayMessage(locations [loclocal].description);
}       

I have no idea what happened but i opened up my code and i started getting this error Uncaught TypeError: Cannot read property 'description' of undefined. PLease help... I was checking to see if my game worked one last time before its due and now im nervous cause its due in 12 hours for class. the rest of the code is on my git https://github.com/rileyjgr/Games the two things to look at are gameworking.html and game.js

PLease any help is appreciated idk what i did and im freaking out i cant find the error

Rilwy G
  • 1
  • 1
  • 2

2 Answers2

-1

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.

Hart Simha
  • 862
  • 8
  • 14
  • Just tried this. no matter what i do it still says description is undefined . – Rilwy G Dec 11 '14 at 08:16
  • hey thanks. i went through my whole code and re edited it. took around an hour major pain in the ass but hey it worked. thanks – Rilwy G Dec 11 '14 at 10:13
  • No problem, and welcome to Stack Overflow @RilwyG! If this or any answers in the future on this site resolve a question you have or help you understand the solution, you may also want to consider upvoting them or marking them as 'accepted' by clicking the green checkmark icon next to the answer. This indicates to the wider community that an answer has been helpful to you and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Hart Simha Dec 11 '14 at 10:29
-1

In case anyone had the same problem. I fixed mine. I still dont know what the problem meant. but as Hart said there was some spelling mistakes (dyslexia op Am i right?) anyways i basically went through line by line and copied and pasted into a new doc and re read everything dozens of times till i found the errors. Even tho the part where i had the errror: function updateDisplay() { displayMessage(locations [loclocal].description); } is exactly the same.. i still had to go through and edit everything.

Rilwy G
  • 1
  • 1
  • 2
  • The error you received ("unable to read property 'description' of undefined") refers to the fact that locations[loclocal] is undefined at the time of access. More specifically locations[0]-locations[10] were explicitly set to `undefined` in the lines identified in my answer: `locations[0] = loclocal_0 ...` Because loclocal_0 is undefined at that point in the code, locations[0] is now `undefined`. When you try to access the 'description' property of `undefined` (or any property of `undefined`) you receive a TypeError with this feedback. – Hart Simha Dec 11 '14 at 10:49