简体   繁体   中英

How can I solve this pushing of an array into an object?

I am working on a text-based rover simulator and the goal is having 2 different rovers that get commands (turnLeft, turnRight, moveForward, moveBackwards) and move in a 10x10 grid. Besides, I want to log each rover's path and here is where I'm having some trouble. I created an object travelLog with two keys, rover1 and rover2. The value of each key is an initialized array []. However, when I try to moveForward, I get the error 'TypeError: Cannot read property 'push' of undefined'.

Each command accepts one argument (in this case, the rover's name: rover1 or rover2). Since the loggin takes place in both the moveForward and moveBackwards function, I wrote this:

 const rover1 = { direction: 'N', x : 0, y : 0, }; const rover2 = { direction: 'N', x : 5, y : 7, }; let travelLog = { rover1: [], rover2: [], } const obstacles = { x: [0,1,2,2,4,6,7], y: [6,0,2,7,4,3,8], }; // TURNLEFT FUNCTION function turnLeft(rover){ console.log("turnLeft was called!"); switch(rover.direction) { case 'N' : rover.direction = 'W'; break; case 'W' : rover.direction = 'S'; break; case 'S' : rover.direction = 'E'; break; case 'E' : rover.direction = 'N'; break; } }; // TURN RIGHT FUNCTION function turnRight(rover){ console.log("turnRight was called!"); switch (rover.direction) { case 'N' : rover.direction = 'E'; break; case 'E' : rover.direction = 'S'; break; case 'S' : rover.direction = 'W'; break; case 'W' : rover.direction = 'N'; break; } }; // FUNCTION TO CHECK IF THERE IS ANY OBSTACLE AHEAD. function checkObstacle (rover,otherRover){ for (let i = 0; i < obstacles.x.length; i++){ if(obstacles.x[i] === rover.x && obstacles.y[i] === rover.y){ console.log('An obstacle was found! Order aborted!'); return true; } else if(otherRover.x=== rover.x && otherRover.y === rover.y){ console.log('Another rover found in your path! Order aborted!'); return true; } else { return false; } } }; // MOVE FORWARD FUNCTION function moveForward (rover) { console.log("moveForward was called"); travelLog[rover].push(['[x' + rover.x +',' + 'y' + rover.y + ']' ]); if(rover.direction === 'N' && rover.y > 0 ){ // NORTHBOUND MOVEMENT rover.y--; if(checkObstacle()){ rover.y++; }; } else if(rover.direction === 'W' && rover.x > 0){ // WESTBOUND MOVEMENT rover.x--; if(checkObstacle()){ rover.x++; }; } else if(rover.direction === 'S' && rover.y < 9 ){ // SOUTHBOUND MOVEMENT rover.y++; if(checkObstacle()){ rover.y--; }; } else if(rover.direction ==='E' && rover.x <9){ //EASTBOUND MOVEMENT rover.x++; if(checkObstacle()){ rover.x--; }; } else { console.log('Your order tried to put the rover off the grid! Order cancelled!'); }; console.log(`The new position of the rover is heading ${rover.direction} row ${rover.x} column ${rover.y}`); } 

I expect the new position of the rover to be logged into the object travelLog, in its corresponding key (either rover1 or rover2).

When you call travelLog[rover] , rover is not a string which can be used as a key for the object. Instead it is object itself.
So, you need to have a name of that rover like rover.name (which will be rover1 ) and then use it: travelLog[rover.name]... .


Example

const rover1 = {
  name: 'Bob',
  direction: 'N',
  x: 0,
  y: 0,
};

const rover2 = {
  name: 'Mike',
  direction: 'N',
  x: 5,
  y: 7,
};

let travelLog = {};

// ...

function moveForward(rover) {
  console.log("moveForward was called");
  if (!travelLog[rover.name]) {
    travelLog[rover.name] = [];
  }
  travelLog[rover.name].push(`Rover ${rover.name} moved to [x: ${rover.x},y ${rover.y}]`);

  // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM