如何在if / else或switch语句中返回不同的索引位置?

时间:2017-01-04 22:15:55

标签: javascript arrays if-statement indexing

我正在检查我在程序中面对的方向:'N','E','S',W'。我想创建一个switch语句来检查我当前的方向,并根据命令是向右('r')还是向左('l),改变方向并将新方向返回给用户。我试图通过调用数组,然后减去索引位置,使其根据最后的方向和命令落在正确的方向。我很确定我做得不对。这是我到目前为止的代码(包含上下文对象)。我是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;
    }

我也想到循环这可能会更有效率。但是,如果不理解索引定位,我很难对其进行概念化。

5 个答案:

答案 0 :(得分:2)

而不是从这个数组中获取新方向

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

为什么不这样做呢

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

错误提示较少,因为您没有按照该数组元素的顺序进行中继,而且代码更具描述性。

无论如何,你的函数没有返回任何值,最后添加return

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;
}

现在只需使用特定命令调用该函数

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

答案 1 :(得分:2)

如果数组已按顺时针"顺时针"排序,则您不需要switch语句。订购。向右转是指获取数组中的下一个元素,向左转是指获取数组中的前一个元素。



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'));




答案 2 :(得分:0)

为什么不直接使用索引并计算新方向?

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];
}

或使用对象

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

答案 3 :(得分:0)

您的变量myRover.direction是一个字符串。在函数转向中,您必须将字符串传递给变量myRover.direction。在这里你传递一个数字(即:(myRover.roverDirections.length - 1))

这是一个例子:

SELECT t.SKU_new, t.ID_Order
INTO #caseone
FROM dbo.Order_No AS n JOIN
     dbo.Orders AS o
     ON n.Order_No = o.Order_No JOIN
     #Temp AS t
     ON (t.ID_Order = o.ID or t.SKU_new = o.SKU);

答案 4 :(得分:0)

您可以使用此功能:

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