簡體   English   中英

火星探測器卡塔。 可以轉彎或前進

[英]Mars rover Kata. Can either turn or advance

我正在做這個 kata,其中我有一個漫游者,我需要能夠在 10x10 的板上發出指令。 Rover 一次只能進行 1 次移動。 要么轉身,要么前進。

當我console.log提前功能時,我沒有在板上得到更新的流動站 position。 我想不通。 我寫了所有東西並找到了解決方案,但后來我打破了它,覆蓋了一些東西,無法弄清楚我刪除或更改了什么。

 // 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 " 

但它不會更新 position。 這是我實際得到的整個控制台日志:

"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

我認為您必須遞增/遞減theRover.location.xtheRover.location.y而不是您正在使用的臨時變量。

有兩件事會改變你的結果:

1) 你的函數都沒有返回任何東西,所以記錄他們的 output 是沒有意義的。 刪除它會刪除日志中所有undefined的條目。

2) 更重要的是,在 moveForward function 中,您使用LocationXLocationY變量計算新位置,但您從不更新流動站對象的位置屬性。 因此,下次運行 function 時,位置值將再次從流動站 object 獲取,它們仍然與上次相同,因為它們從未更新。

我還刪除了您的日志記錄中的一些重復/冗余。

工作演示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM