Search code examples
javascriptfunctionswitch-statementinvokeconsole.log

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


Happy holidays first of all. I am unable to find the reason why I am unsuccessful at invoking the 'commands' function with its relative orders on this Javascript exercise. Have I messed up with the {}?

//// Rover object goes here ////

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}`);
} 
*/
}

Any input/suggestion will be greatly appreciated. I can't seem to find the mistake and it is quite infuriating. However, thank you again and have a lovely and festive time!


Solution

  • You need one more closing brace } at the end of the moveBackward function. You closed the switch statement but not the function.

    Happy Holidays!