0

Description of code The purpose of this code is to check if the position variable is on a tile listed in the map array. If you are on the map it displays the map info console.log and updates a variable called lastPosition to be your new current position, if you are however not on the map it should set your position to lastPosition.

What seems to be wrong The lastPosition variable seems to be getting changed before it is even accessed in the code, it is being changed before entering into the for loop that checks position (which is the only place in the code where the variable is assigned a new value). What seems to be happening to me is that the variable is somehow being altered by the interpreter while it is being moved from function to function. I have spent too much time trying to figure this out on my own and I really need help (thanks in advance).

Here is the entirety of my code:

//map variable
const map = [
  [
    [0, 0],
    ['this is room 1']
  ],
  [
    [0, 1],
    ['this is room 2']
  ]
];
var position = [0, 0];
var lastPosition = [0, 0];

//run the script when enter is pressed
document.onkeydown = function(e) {
  e = e || window.event;
  var key = e.which || e.keyCode;
  if (key === 13) {
    run();
  }
}

//main run function
function run() {
  console.log(map);
  //get input from text box
  input = document.getElementById('input').value;
  if (input == '') {
    return;
  }
  console.log(input);

  interpreter();
}

//interprets the input in the box
function interpreter() {
  if (input == 'up') {
    position[1] += 1;
    accessMap()
  } else if (input == 'down') {
    position[1] -= 1;
    accessMap()
  } else if (input == 'right') {
    position[0] += 1;
    accessMap()
  } else if (input == 'left') {
    position[0] -= 1;
    accessMap()
  }
}

function accessMap() {
  console.log(position);
  console.log(lastPosition);
  var positionInfo = position.toString();
  var positionTest = 0;
  console.log('before: ' + positionTest);
  //checks if your position is in the map by turning arrays into strings and comparing them
  for (i = 0; i < map.length; i++) {
    mapInfo = map[i][0].toString();
    console.log('Map info: ' + mapInfo);
    console.log('Position info: ' + positionInfo);
    //if inside map
    if (positionInfo == mapInfo) {
      console.log('in map');
      positionTest = 1;
      lastPosition = position;
      console.log('in map test: ' + positionTest);
    }
  }
  console.log('after: ' + positionTest);
  //if not inside map
  if (positionTest != 1) {
    console.log('not in map');
    position = lastPosition;
    console.log('out map test: ' + positionTest);
  }
  console.log(position);
  console.log(lastPosition);
}
<input id="input" type="text">

Final thoughts There are no errors that come up in the console, and I have tried moving around console.log() in every imaginable place and even reworked and shortened the original code (shortened version is here), but I can't seem to figure out what is wrong, let alone fix it. As previously mentioned the lastPosition value is somehow altered before it is accessed in code allowing the user to exit the map. (idk where to put this but expanding the map and making it bigger shows no difference)

(I know it is wacky to have JS inside a HTML document but it is necessary for what I am working on, and I did try it with it in a separate document and there was no difference in outcome)

DarkBee
  • 16,592
  • 6
  • 46
  • 58

0 Answers0