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 Object-Oriented JavaScript: Part 1

After I put m.setWall(1,3,"north"), why it return undefined? I did exactly as Randy did and still no good. Please help.

No clue

Would need to see your code and the error message to help.

I see the code you added. What is the error message in your console that you're getting? What is undefined? I've run all this locally and I am able to type 'm' in the console and see the correct output.

I've cleaned up your code a bit for any future readers:

robot.html

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="robot.js"></script>
    <script type="text/javascript" src="maze.js"></script>
    <script type="text/javascript" src="mazespace.js"></script>
    <script>
        m = new Maze(7,5);
        m.setStart(1,1,"north");
        m.setEnd(7,1);
        m.setWall(1,1,"east");
    </script>
</head>
<body>
    <div id="page">
        Loading...
    </div>
</body>
</html>

maze.js

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

    this.spaces = [];

    var x, y;
    for (x = 1; x <= width; x += 1) {
        this.spaces[x] = [];
        for (y = 1; y <= height; y += 1) {
            this.spaces[x][y] = new MazeSpace();
        }
    }
}

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

Maze.prototype.setWall = function (x, y, direction) {
    if (x > 0 && x <= this.width && y > 0 && y <= this.height && ["north", "east", "south", "west"].indexOf(direction) !== -1) {
        this.spaces[x][y].setWall(direction);
        return true;
    }
    return false;
};

You did not include robot.js or mazespace.js but I'll include them from what I saw in the video. I'm assuming you have them. If not, this is most likely your problem.

mazespace.js

"use strict";

function MazeSpace(directions) {
    this.north = false;
    this.east = false;
    this.south = false;
    this.west = false;
}

MazeSpace.prototype.setWall = function(direction) {
    this[direction] = true;
}

robot.js

"use strict";

function Robot() {
    this.x = null;
    this.y = null;
    this.orientation = null;
    this.maze = null;
}

Robot.prototype.setMaze = function(maze) {
    this.maze = maze;
    this.x = maze.startX;
    this.y = maze.startY;
    this.orientation = maze.startOrientation;
};

Robot.prototype.turnRight = function() {
    if (!this.maze || !this.maze.isValidDirection(this.orientation)) {
        return false;
    }

    var rights = {
        north: "east",
        east: "south",
        south: "west",
        west: "north"
    }
    this.orientation = rights[this.orientation];
    return true;
}

Robot.prototype.turnLeft = function() {
    if (!this.maze || !this.maze.isValidDirection(this.orientation)) {
        return false;
    }

    var lefts = {
        north: "west",
        east: "north",
        south: "east",
        west: "south"
    }
    this.orientation = lefts[this.orientation];
    return true;
}

3 Answers

Okie dokie

this is the maze.js file

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


this.spaces = [];

var x, y;
for (x = 1; x <= width; x += 1) {
    this.spaces[x] = [];
for (y = 1; y <= height; y += 1) {
    this.spaces[x][y] = new MazeSpace();
    }
}

}

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;

};

Maze.prototype.setWall = function (x, y, direction) { if (x > 0 && x <= this.width && y > 0 && y <= this.height && ["north", "east", "south", "west"].indexOf(direction) !== -1) { this.spaces[x][y].setWall(direction); return true; } return false; };

Robot.html file

<!DOCTYPE html> <html> <head>

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

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

</script>

</head> <body>

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

</body> </html>