gpt4 book ai didi

JavaScript - 走十分钟 - 如何正确访问数组元素

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:39:06 26 4
gpt4 key购买 nike

如果您不熟悉此挑战,请参阅以下说明:

https://www.codewars.com/kata/54da539698b8a2ad76000228/train/javascript

You live in the city of Cartesia where all roads are laid out in a perfect grid. You arrived ten minutes too early to an appointment, so you decided to take the opportunity to go for a short walk. The city provides its citizens with a Walk Generating App on their phones -- everytime you press the button it sends you an array of one-letter strings representing directions to walk (eg. ['n', 's', 'w', 'e']). You always walk only a single block in a direction and you know it takes you one minute to traverse one city block, so create a function that will return true if the walk the app gives you will take you exactly ten minutes (you don't want to be early or late!) and will, of course, return you to your starting point. Return false otherwise.

到目前为止,我已经尝试过:

function isValidWalk(walk) {

//initiate person starting point
let person = [0, 0]
//establish what the ending point must be
let finalDestination = [0, 0]

let north = [0, 1]
let east = [1, 0]
let south = [0, -1]
let west = [-1, 0]

//as long as the length of the array is 10 or less, continue walk
for (let i = 0; i <= 10; i++) {
//if the letter in the array is "n", move north
if (walk[i] === "n") {
person + north;
}
//if the letter in the array is "e", move east
if (walk[i] === "e") {
person + east;
}
//if the letter in the array is "s", move south
if (walk[i] === "s") {
person + south;
}
//if the letter in the array is "w", move west
if (walk[i] === "w") {
person + west;
}
}

if (person === finalDestination) {
return true;
}
else {
return false;
}

}

这将通过 6/9 测试,但不会返回 true 以进行有效步行。

如您所见,我试图说明如果 person 的位置等于 finalDestination,则 isValidWalk 函数应返回 true在步行结束时可变。

我知道可能还有其他方法可以解决这个问题,但如果可能的话,我想继续遵循我已经建立的逻辑。

我想知道我的问题是不是我没有正确访问数组中的元素? IE。 walk[i] 是否在此处正确获取数组元素?

    if (walk[i] === "n") {
person + north;
}

这应该是在这个假想的网格上移动 person 的原因,但显然它什么也没做。我应该尝试使用什么其他语法来访问数组中的元素并检查它们是否等于“n”、“e”、“s”和“w”?

最佳答案

首先,JavaScript 数组不像数学矩阵那样工作。 array1 + array2不会将这些数组中的各个值加在一起。相反,您必须递增/递减数组中的值:

if (walk[i] === "n") {
person[0]++;
}
if (walk[i] === "e") {
person[1]++;
}
if (walk[i] === "s") {
person[0]--;
}
if (walk[i] === "w") {
person[1]--;
}

或者更简洁:

switch(walk[i]) {
case "n": person[0]++; break;
case "e": person[1]++; break;
case "s": person[0]--; break;
case "w": person[1]--; break;
}

第二,personfinalDestination是数组,所以 ===表示引用平等。即 person === finalDestination只会返回 true如果两个变量都引用内存中的相同位置。相反,您需要比较数组的各个值,例如

if (person[0] === finalDestination[0] &&
person[1] === finalDestination[1]) {
return true;
}
else {
return false;
}

或者更简洁:

return person[0] === finalDestination[0] &&
person[1] === finalDestination[1];

但是,请注意 finalDestination永远不会改变,所以你真的根本不需要那个变量。您可以将其替换为:

return person[0] === 0 && person[1] === 0;

关于要求的最后一点:

return true if the walk the app gives you will take you exactly ten minutes

您需要将其添加到函数的顶部:

if (walk.length !== 10) return false;

为了更简洁的代码,确保你的 for -loop 没有运行超过 walk 的末尾通过替换 i <= 10i < 10i < walk.length .

关于JavaScript - 走十分钟 - 如何正确访问数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55364242/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com