sharlene sharlene - 9 days ago 5
AngularJS Question

Javascript: Updating direction of robot using string is not working

Javascript

Trying to work on a simple game of moving a robot using key L(to move left), R(to move right), F(to move forward) from a specific position on a board where it is created using 5x5 dimension and always facing North (shown as N). Clicking a Move button after entering any of above string/characters should display the current position.

Expected: For instance if I say board dimension (5x5), current position (3,3), enter 'R' and hit Move button it should show the resulting position as (3,3) E because the robot was facing north (N) first and now asked to move to RIGHT which would be east (E).

Problem: Can't seem to spot the issue in my code why the direction is not getting updated.

Here is the code that does all calculation and update.

var RobotManager = {
roomType: 'square',
roomParameters: [5, 5],
robotDirection: 'N',
robotPosition: [1, 2],
possibleDirections: ["N", "E", "S", "W"],
errorMessageNumber: -1,
errorMessage: [
"error0",
"error1",
"all other errors removed to keep code clean"
],
squares: [],
stringCommandThatExexuted: '',
init: function() {

var regexp_number = /^[1-9]$|([1][0-9])$/;

return true;
},

// This should move the robot to the right direction
turnRight: function() {
var movePosition = this.possibleDirections.indexOf(this.robotDirection);
if (movePosition == this.possibleDirections.length - 1) {
return this.possibleDirections[0];
}
return this.possibleDirections[movePosition + 1];
},

turnLeft: function() {
var movePosition = this.possibleDirections.indexOf(this.robotDirection);
if (movePosition == 0) {
return this.possibleDirections[this.possibleDirections.length - 1];
}
return this.possibleDirections[movePosition - 1];
},

moveForward: function() {
var nextPosition = this.getNextPosition();
var nextSquare = {
X: nextPosition[0],
Y: nextPosition[1]
};
if (this.isSquareAvailable(nextSquare)) {
this.robotPosition = nextPosition;
} else {
this.errorMessageNumber = 1;
return false;
}
return true;
},

//this is not getting executed to update the direction
getNextPosition: function() {
var x, y;
switch (this.robotDirection) {
case "N":
x = this.robotPosition[0];
y = this.robotPosition[1] - 1;
break;
case "E":
x = this.robotPosition[0] + 1;
y = this.robotPosition[1];
break;
case "W":
x = this.robotPosition[0] - 1;
y = this.robotPosition[1];
break;
case "S":
y = this.robotPosition[1] + 1;
x = this.robotPosition[0];
break;
}
return [x, y];
},

//First button clicks comes here and just renders default value of direction
getRobotsPositionAndDirection: function() {
if (this.errorMessageNumber <= 1) {
var message = "";
if (this.errorMessageNumber == 0) {
return this.errorMessage[0];
}
if (this.errorMessageNumber == 1) {
message = this.errorMessage[1];
}
return message + this.robotPosition[0] + " " + this.robotPosition[1] + " " + this.robotDirection;
}
return this.errorMessage[8];
},
checkCommandString: function(string) {
var english_command = /^[LRF]*$/;

if (english_command.test(string)) {
return true;
}
this.errorMessageNumber = 0;
return false;
},
getCommandStringThatExecuted: function() {
return this.stringCommandThatExexuted;
},

//This is where index is passed as 0 and doesn't execute
moveRobotToOnePosition: function(letter) {
switch (letter) {
case 'L':
this.robotDirection = this.turnLeft();
this.stringCommandThatExexuted += 'L';
break;
case 'R':
this.robotDirection = this.turnRight();
this.stringCommandThatExexuted += 'R';
break;
case 'F':
if (this.moveForward()) {
this.stringCommandThatExexuted += 'F';
return true;
} else {
return false;
}
break;
}
},
moveRobot: function(string) {
string = string.toUpperCase();
if (this.checkCommandString(string)) {
var array = string.split('');
for (var i = 0; i < array.length; i++) {
return this.moveRobotToOnePosition(i);
}
return true;
}
this.errorMessageNumber = 0;
}
}



2ps 2ps
Answer

So your problem is actually pretty simple, and my apologies for taking so long to figure this out. The issue is that you were passing the array index and not the array element.

  moveRobot: function(string) {
    string = string.toUpperCase();
    if (this.checkCommandString(string)) {
      var array = string.split('');
      for (var i = 0; i < array.length; i++) {
        // return this.moveRobotToOnePosition(i);
        this.moveRobotToOnePosition(array[i]); // don't use return here unless you want to exit the function.
      }
      return true;
    }
    this.errorMessageNumber = 0;
  }
Comments