Veronica Donoso
Veronica Donoso

Reputation: 13

Why the value of my function does not update with the value of my object

I'm doing an exercise about rover in Mars I have the object rover that has direction as property and default value 'N' (north), he needs first turn before to go forward, it means that if the rover wants to move to the left, it’s first move must be a turn, next move will then be a step forward. when I run the function to moveLeft(rover) The results are as expected. He turns perfect!! BUT the value of property direction is never updated, and I need this value update to the next step... I'm going to paste a part of my code. Thank you!

  direction: 'E',
  x: 0,
  y: 0,
  path: { x: 0, y: 0 },
  travelLog: []
}
var move = rover.direction

function moveLeft (who) {
  console.log(`Rover is facing ${move}`)
  switch (move) {
    case 'N':
      move = 'W'
      console.log(`Rover is facing now ${move}`)
      break;
    case 'W':
      move = 'S'
      console.log(`Rover is facing now ${move}`)
      break;
    case 'S':
      move = 'E'
      console.log(`Rover is facing now ${move}`)
      break;
    case 'E':
      move = 'N'
      console.log(`Rover is facing now ${move}`)
      break;
    default:
      console.log(`Please insert the correct directions`)
  }
  console.log('turnLeft was called!')
}

This is my output

Rover is facing E
Rover is facing now N
turnLeft was called!

console.log(rover)
{direction: "E", x: 0, y: 0, path: {…}, travelLog: Array(0)}

console.log(move)
W

console.log(rover.direction)
E

I expect the output of rover.direction to be the same of move

Upvotes: 1

Views: 59

Answers (2)

Jonas Wilms
Jonas Wilms

Reputation: 138247

(the other answer already covered why your current work doesn't work as expected)

I'd write it as:

const turns = ["N", "W", "S", "E"]; // We can easily look up the direction to the right or left here

function turnLeft(rover) {
  console.log(`Rover is facing ${rover.direction}`);
  
  const pos = turns.indexOf(rover.direction); // get the current position, e.g. E => 3
  rover.direction = turns[(pos + 1) % turns.length]; // Increase the position by 1 (=> 0), make sure it's inside the turns arrays indices by starting again from 0 (=> 0), then look that up => "N"
  
  console.log(`Rover is facing now ${rover.direction}`)
}


const rover = { direction: "E" };
turnLeft(rover);
turnLeft(rover);

Upvotes: 0

Jack A.
Jack A.

Reputation: 4443

This line:

var move = rover.direction

is copying the value of rover.direction into the variable named move, it does not create a reference to rover.direction. When you do something like this:

move = 'W'

it only updates the move variable, it does not update rover.direction.

To fix this, you can either add a line like:

rover.direction = move;

after your switch statement, or you can get rid of the move variable and work directly on rover.direction.

Also, a couple notes/suggestions:

  1. You are defining move outside of the moveLeft function. That creates another global variable in addition to the global rover variable. In general, you want to limit the number of global variables to be as few as possible.

  2. Your moveLeft function takes a who parameter that you are not currently using within the function. From your description, it looks like you intend to pass the rover variable in that parameter. In that case, you probably want to remove all references to move and just work with who.direction within your function.

Upvotes: 2

Related Questions