Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Robot Maze Issue

Seem to be getting this error: "Uncaught TypeError: undefined is not a function"

for my code on robot.html with the line:

m.setStart(1,1,"north");

I believe it is because north hasn't been defined? I have deleted all my code, re-did it all again from the start of the video and still received the same error. I am also not seeing the drop list of values for m similar to what the video shows.

Here is my maze.js code.

"use strict";

function Maze(width,height) {
    this.width = width;
    this.height = height;
    this.startX             = null;
    this.startY             = null;
    this.startOrientation   = null;
    this.endX               = null;
    this.endY               = null;

}

Maze.prototype.setStart = function (x, y, orientation) {
    this.startX = x;
    this.startY = y;
    this.startOrientation = orientation;
}

Maze.prototype.setEnd = function (x, y) {
    this.endX = x;
    this.endY = y;


}

Here is the robot.html code:

<!DOCTYPE html>
<head>

<script type="text/javascript" src="js/robot.js"></script>
<script type="text/javascript" src="js/maze.js"></script>
<script type="text/javascript" src="js/mazespace.js"></script>

<script>
    m = new Maze(7,5);
    m.setStart(1,1,"north");
    m.setEnd(7,1);
    </script>

</head>

<body>

<div id="page">
    Loading....
    </div>
    </body>
    </html>

Hmm when I open in firefox it gives a different error in the console "TypeError: m.setStart is not a function"

But when I use firebug console, no error.

Maybe it just depends on what browser console I am using, and there may be no error at all?

OK nevermind, the problem is the way I had my files saved. It was calling on an old version of maze.js instead of the one I was actively writing to. Oops.

1 Answer

Aaron Graham
Aaron Graham
18,033 Points

This is purely a guess, but I wonder if it has to do with your use of "use strict" and your declaration of "m" without using var. Like this:

m = new Maze(7, 5);

Instead of:

var m = new Maze(7, 5);

My understanding of "use strict" is that it only applies to the file it is declared in, but I wonder if, since it is used with the declaration of Maze, instances of Maze also fall under the "use strict" declaration. Again, just a guess.

Edit: just saw your comment... Never mind :-)