universesurfer
universesurfer

Reputation: 357

How do you return a different index position in an if/else or switch statement?

I am checking to see what direction I am facing in my program: 'N', 'E', 'S', W'. I want to create a switch statement that checks my current direction, and depending on whether the command is to turn right ('r') or left ('l), change the direction and return the new direction to the user. I am trying to do that by calling the array and then subtracting the index position so that it lands on the right direction according to the last direction and command. I'm quite certain I'm not doing it right. Here's my code so far (with object included for context). I'm new to Javascript.

var myRover = {
  position: [0,0],
  direction: 'N',
  roverDirections = ['N', 'E', 'S', 'W'],
  marsGrid: [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
  obstacles: []
};

function turn(command){

  if (command === 'l') {

      switch (myRover.direction) {
        case 'N':
        myRover.direction = (myRover.roverDirections.length - 1)
        break;
        case 'E':
        myRover.direction = (myRover.roverDirections.length - 4)
        break;
        case 'S':
        myRover.direction = (myRover.roverDirections.length - 3)
        break;
        case 'W':
        myRover.direction = (myRover.roverDirections.length - 2)
        break;
      }

    if (command === "r") {
      switch (myRover.direction) {
        case 'N':
        myRover.direction = (myRover.roverDirections.length - 3)
        break;
        case 'E':
        myRover.direction = (myRover.roverDirections.length - 2)
        break;
        case 'S':
        myRover.direction = (myRover.roverDirections.length - 1)
        break;
        case 'W':
        myRover.direction = (myRover.roverDirections.length - 4)
        break;
    }

It's also occurred to me that looping this might be much more efficient. But it's hard for me to conceptualize it without understanding the index positioning.

Upvotes: 1

Views: 807

Answers (5)

Nina Scholz
Nina Scholz

Reputation: 386680

Why not use the index and calulate the new direction direct?

function turn(command) {
    var direction = myRover.roverDirections.indexOf(myRover.direction);
    if (command === 'l') {
        direction++;
    } else if (command === 'r') {
        direction += 3;
    }
    return myRover.direction = myRover.roverDirections[direction % 4];
}

Or with an object

function turn(command) {
    var direction = myRover.roverDirections.indexOf(myRover.direction);
    direction += { l: 1, r: 3 }[command] || 0;
    return myRover.direction = myRover.roverDirections[direction % 4];
}

Upvotes: 0

castletheperson
castletheperson

Reputation: 33496

You don't need a switch statement if the array is already sorted in "clockwise" order. Turning right would mean getting the next element in the array, and turning left would mean getting the previous element in the array.

var myRover = {
  direction: 'N',
  roverDirections: ['N', 'E', 'S', 'W'],
  turn: function(command) {
    var offset = command === 'r' ? 1 : 3;
    var oldDirectionIndex = this.roverDirections.indexOf(this.direction);
    var newDirectionIndex = (oldDirectionIndex + offset) % 4;
    this.direction = this.roverDirections[newDirectionIndex];
    return this.direction;
  }
};

console.log(myRover.turn('r'));
console.log(myRover.turn('r'));
console.log(myRover.turn('r'));
console.log(myRover.turn('r'));
console.log(myRover.turn('l'));
console.log(myRover.turn('l'));
console.log(myRover.turn('l'));
console.log(myRover.turn('l'));

Upvotes: 2

trincot
trincot

Reputation: 350760

You could use this function:

function turn(command){
    return myRover.direction = myRover.roverDirections[
            (myRover.roverDirections.indexOf(myRover.direction)
             + ' r l'.indexOf(command)) % 4];
}

Upvotes: 0

Sam Quinn
Sam Quinn

Reputation: 3871

Instead of get the new direction from this array

 roverDirections = ['N', 'E', 'S', 'W']

Why not just do this

 case 'N':
   myRover.direction = 'W'
 break

Is less error prompt because you don't relay in the order of the elements of that array, and also the code is more descriptive.

Anyway, your function is not returning any value, add a return at the end

function turn(command){

 if (command === 'l') {

  switch (myRover.direction) {
    case 'N':
    myRover.direction = (myRover.roverDirections.length - 1)
    break;
    case 'E':
    myRover.direction = (myRover.roverDirections.length - 4)
    break;
    case 'S':
    myRover.direction = (myRover.roverDirections.length - 3)
    break;
    case 'W':
    myRover.direction = (myRover.roverDirections.length - 2)
    break;
  }

if (command === "r") {
  switch (myRover.direction) {
    case 'N':
    myRover.direction = (myRover.roverDirections.length - 3)
    break;
    case 'E':
    myRover.direction = (myRover.roverDirections.length - 2)
    break;
    case 'S':
    myRover.direction = (myRover.roverDirections.length - 1)
    break;
    case 'W':
    myRover.direction = (myRover.roverDirections.length - 4)
    break;
}
  return myRover.direction;
}

And now just call that function with the certain command

  console.log(turn('l')) // prints 'W'

Upvotes: 2

Maxime Leprince
Maxime Leprince

Reputation: 156

Your variable myRover.direction is a string. In your function turn, you have to pass a string to your variable myRover.direction. Here you pass a number ( ie : (myRover.roverDirections.length - 1) )

Here an example :

switch (myRover.direction) {
   case 'N':
    myRover.direction = myRoverDirections[3]
    break;

Upvotes: 0

Related Questions