我正在做这个kata,其中我有一辆漫游车,需要能够向10x10的木板内发出指令。流动站一次只能移动1次。要么转弯要么前进。
当我console.log
使用高级功能时,我没有得到流动站在板上的最新位置。我不知道。我写了所有内容并找到了解决方案,但是后来我破坏了它,将某些内容覆盖了,无法弄清楚我擦除或更改的内容。
// Rover Object Goes Here
// ======================
let grid;
let rover = {
direction: "N",
location: {x:0,y:0},
// path: [{x:0,y:0}]
}
// ======================
//Turn left function
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!. Rover is now facing ${rover.direction}` );
}
//Turn right function
function turnRight(rover){
switch (rover.direction) {
case "N":
rover.direction = "E";
break;
case "E":
rover.direction = "S";
break;
case "S":
rover.direction = "W";
break;
case "W":
rover.direction = "N";
break;
}
console.log(`turnRight was called!. Rover is now facing ${rover.direction}` );
}
// Function for moving rover
function moveForward(theRover) {
//Variables
let locationX = theRover.location.x;
let locationY = theRover.location.y;
let roverDir = theRover.direction;
let roverInfoConsole = `Current rover position is x:${locationX} y:${locationY}
Current rover direction is ${roverDir} `;
// Restricted Moves
if ( (roverDir === "N" && locationY <= 0) ||
(roverDir === "E" && locationX >= 9) ||
(roverDir === "S" && locationY >= 9) ||
(roverDir === "W" && locationX <= 0) ) {
console.log(`Cannot move in that direction. The rover would move to a restricted area.
${roverInfoConsole}`);
//allowed moves
//North
} else if (roverDir === "N" && locationY <= 9) {
locationY = locationY - 1;
console.log(`moveForward was called.
Current rover position is x:${locationX} y:${locationY}
Current rover direction is ${roverDir} `);
//East
} else if (roverDir === "E" && locationX < 9) {
locationX = locationX + 1;
console.log(`Current rover position is x:${locationX} y:${locationY}
Current rover direction is ${roverDir} `);
//South
} else if (roverDir === "S" && locationY < 9) {
locationY = locationY + 1;
console.log(`moveForward was called.
Current rover position is x:${locationX} y:${locationY}
Current rover direction is ${roverDir} `);
//West
} else if (roverDir === "W" && locationX < 9) {
locationX = locationX - 1;
console.log(`moveForward was called.
Current rover position is x:${locationX} y:${locationY}
Current rover direction is ${roverDir} `);
}
};
console.log(turnRight(rover));
console.log(moveForward(rover));
console.log(moveForward(rover));
console.log(turnRight(rover))
console.log(moveForward(rover));
console.log(moveForward(rover));
我希望最终的输出是:
"Current rover position is x:2 y:2
Current rover direction is S "
但是它没有更新位置。这是我实际上得到的整个控制台日志:
"turnRight was called!. Rover is now facing E"
undefined
"Current rover position is x:1 y:0
Current rover direction is E "
undefined
"Current rover position is x:1 y:0
Current rover direction is E "
undefined
"turnRight was called!. Rover is now facing S"
undefined
"moveForward was called.
Current rover position is x:0 y:1
Current rover direction is S "
undefined
"moveForward was called.
Current rover position is x:0 y:1
Current rover direction is S "
undefined
答案 0 :(得分:2)
我认为您必须递增/递减theRover.location.x
和theRover.location.y
,而不要使用所使用的临时变量。
答案 1 :(得分:1)
有两点改变您的结果:
1)您的函数均未返回任何内容,因此记录其输出毫无意义。删除将删除日志中的所有undefined
条目。
2)更重要的是,在moveForward函数中,您使用LocationX
和LocationY
变量计算了新位置,但是从未更新流动站对象的位置属性。因此,下次运行该函数时,位置值将再次从流动站对象获取,并且由于从未更新过,因此它们仍与上一次相同。
我还删除了您的日志记录中的某些重复/冗余。
工作演示:
// Rover Object Goes Here
// ======================
let grid;
let rover = {
direction: "N",
location: {
x: 0,
y: 0
},
// path: [{x:0,y:0}]
}
// ======================
//Turn left function
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!. Rover is now facing ${rover.direction}`);
}
//Turn right function
function turnRight(rover) {
switch (rover.direction) {
case "N":
rover.direction = "E";
break;
case "E":
rover.direction = "S";
break;
case "S":
rover.direction = "W";
break;
case "W":
rover.direction = "N";
break;
}
console.log(`turnRight was called!. Rover is now facing ${rover.direction}`);
}
// Function for moving rover
function moveForward(theRover) {
//Variables
let locationX = theRover.location.x;
let locationY = theRover.location.y;
let roverDir = theRover.direction;
// Restricted Moves
if ((roverDir === "N" && locationY <= 0) ||
(roverDir === "E" && locationX >= 9) ||
(roverDir === "S" && locationY >= 9) ||
(roverDir === "W" && locationX <= 0)) {
console.log(`Cannot move in that direction. The rover would move to a restricted area.
${roverInfoConsole}`);
//allowed moves
//North
} else if (roverDir === "N" && locationY <= 9) {
locationY = locationY - 1;
//East
} else if (roverDir === "E" && locationX < 9) {
locationX = locationX + 1;
//South
} else if (roverDir === "S" && locationY < 9) {
locationY = locationY + 1;
//West
} else if (roverDir === "W" && locationX < 9) {
locationX = locationX - 1;
}
theRover.location.x = locationX;
theRover.location.y = locationY;
console.log(`moveForward was called.
Current rover position is x:${locationX} y:${locationY}
Current rover direction is ${roverDir} `);
};
turnRight(rover);
moveForward(rover);
moveForward(rover);
turnRight(rover);
moveForward(rover);
moveForward(rover);