无法调用 JS 函数:语法错误或眼睛太交叉?

Unable to invoke JS function: Syntax mistake or eyes too crossed?

首先祝大家节日快乐。 我无法找到在 Javascript 练习中使用其相关命令调用 'commands' 函数失败的原因。我弄错了 {} 吗?

//// 漫游者对象在此处 ////

const rover = {
  direction: "N",
  x: 5,
  y: 5,
  travelLog: []
};

// ========================

//// Rover turns left switch case ////

function turnLeft(rover) {
  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;
  }
  console.log("turnLeft was called!");
  console.log(`Rover has now direction:"${rover.direction}"`);
}

//// Rover turns right switch case ////

function turnRight(rover) {
  switch (rover.direction) {
    case "N":
      rover.direction = "E";
      break;
    case "W":
      rover.direction = "N";
      break;
    case "S":
      rover.direction = "W";
      break;
    case "E":
      rover.direction = "S";
      break;
  }
  console.log("turnRight was called!");
  console.log(`Rover has now direction:"${rover.direction}"`);
}

//// Moving the rover ////

function moveForward(rover) {
  console.log("moveForward was called!");
  let newPosition = { x: rover.x, y: rover.y };
  rover.travelLog.push(newPosition);

  switch (rover.direction) {
    case "N":
      if (rover.y <= 0) {
        console.log("you can't travel out there, bud");
      } else {
        rover.y--;
        console.log(
          `Rover moved North: position is now: ${rover.x} , ${rover.y}`
        );
      }
      break;

    case "E":
      if (rover.x >= 9) {
        console.log("you can't travel out there, bud");
      } else {
        rover.x++;
        console.log(
          `Rover moved East: position is now: ${rover.x} , ${rover.y}`
        );
      }
      break;

    case "S":
      if (rover.y >= 9) {
        console.log("you can't travel out there, bud");
      } else {
        rover.y++;
        console.log(
          `Rover moved South: position is now: ${rover.x} , ${rover.y}`
        );
      }
      break;

    case "W":
      if (rover.x <= 0) {
        console.log("you can't travel out there, bud");
      } else {
        rover.x--;
        console.log(
          `Rover moved West: position is now: ${rover.x} , ${rover.y}`
        );
      }
      break;
  }
}

//move backward//

function moveBackward(rover) {
  console.log("moveBackward was called!");
  let newPosition = { x: rover.x, y: rover.y };
  rover.travelLog.push(newPosition);

  switch (rover.direction) {
    case "N": // Rover is facing North, but it will take one step backwards, hence direction South //
      if (rover.y >= 9) {
        console.log("watch your steps, don't trip");
      } else {
        rover.y++;
        console.log(
          `Rover moonwalked South: position is now: ${rover.x} , ${rover.y}`
        );
      }
      break;

    case "E": //moving direction South
      if (rover.x <= 0) {
        console.log("watch your steps, don't trip");
      } else {
        rover.x--;
        console.log(
          `Rover moonwalked West: position is now: ${rover.x} , ${rover.y}`
        );
      }
      break;

    case "S": // direction North
      if (rover.y <= 0) {
        console.log("watch your steps,don't trip");
      } else {
        rover.y--;
        console.log(
          `Rover moonwalked North: position is now: ${rover.x} , ${rover.y}`
        );
      }
      break;

    case "W": //direction East
      if (rover.x >= 9) {
        console.log("watch your steps, don't trip");
      } else {
        rover.x++;
        console.log(
          `Rover moonwalked East: position is now: ${rover.x} , ${rover.y}`
        );
      }
      break;
  }

  //// Assigning commands ////

  function commands(rover, orders) {
    for (let i = 0; i < orders.length; i++) {
      let order = orders[i];
      switch (order) {
        case "f":
          moveForward(rover, order);
          break;
        case "b":
          moveBackward(rover, order);
          break;
        case "r":
          turnRight(rover, order);
          break;
        case "l":
          turnLeft(rover, order);
          break;
        default:
          console.log("Please use only 'r', 'f', 'b' or 'l'");
      }
    } 
  }

commands(rover,"ffbrl");

  
////printout all the rover's movements////

/*
for (let i = 0; i < rover.travelLog.length; i++) {
console.log(`Path ${i} ==> x=${rover.travelLog[i].x}, y=${rover.travelLog[i].y}`);
} 
*/
}

任何 input/suggestion 将不胜感激。我似乎找不到错误,这真是令人气愤。不过,再次感谢您,祝您节日愉快!

moveBackward 函数末尾还需要一个右大括号 }。您关闭了 switch 语句但没有关闭函数。

节日快乐!