JavaScript – 走十分钟步行 – 如何正确访问数组元素
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JavaScript – 走十分钟步行 – 如何正确访问数组元素,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3285字,纯文字阅读大概需要5分钟。
内容图文
![JavaScript – 走十分钟步行 – 如何正确访问数组元素](/upload/InfoBanner/zyjiaocheng/741/4bb2752ef3e145e6b40414812c1c4cb4.jpg)
如果您不熟悉此挑战,请按以下说明操作:
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的位置等于walk步行结束时的finalDestination变量,则isValidWalk函数应该返回true.
我知道可能有其他方法可以解决这个问题,但我想继续遵循我已经建立的逻辑,如果可能的话.
我想知道我的问题是我是不是正确访问了数组中的元素?即是走路[i]在这里正确获取数组元素?
if (walk[i] === "n") {
person + north;
}
这就是应该在这个想象中的网格上移动这个人的东西,但显然它没有做任何事情.我应该尝试访问数组中的元素并检查它们是否等于“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;
}
其次,person和finalDestination是数组,所以===表示引用相等.也就是说,如果两个变量都引用内存中的相同位置,则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;
并且为了更清晰的代码,通过用i<替换i< = 10来确保你的for循环不会超过walk的结束. 10或i< walk.length.
内容总结
以上是互联网集市为您收集整理的JavaScript – 走十分钟步行 – 如何正确访问数组元素全部内容,希望文章能够帮你解决JavaScript – 走十分钟步行 – 如何正确访问数组元素所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。