首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Codewar错误上的Javascript迷宫运行器

Codewar错误上的Javascript迷宫运行器
EN

Stack Overflow用户
提问于 2018-06-08 04:01:27
回答 1查看 410关注 0票数 0

我已经用我的方式通过了codewars,我遇到了mazerunner (https://www.codewars.com/kata/maze-runner/train/javascript),我已经被难住了大约2天!

代码语言:javascript
复制
function mazeRunner(maze, directions) {

//find start value  

var x = 0; //x position of the start point
var y = 0; //y position of the start point

for (var j = 0 ; j < maze.length ; j++){
if (maze[j].indexOf(2) != -1){
  x = j;
  y = maze[j].indexOf(2)
}
      } // end of starting position forloop

console.log(x + ', ' + y)


  for (var turn = 0 ; turn < directions.length ; turn++){


if (directions[turn] == "N"){
 x -= 1;
}
if (directions[turn] == "S"){
 x += 1;
}
if (directions[turn] == "E"){
 y += 1;
}
if (directions[turn] == "W"){
 y -= 1;
}

 if (maze[x][y] === 1){
 return 'Dead';
 }else if (maze[x][y] === 3){
 return 'Finish';
 }

if (maze[x] === undefined || maze[y] === undefined){
return 'Dead';
}

}

return 'Lost';

}

当我运行这个脚本时,它适用于大多数场景,但是在最后一个场景中,我得到了以下错误

代码语言:javascript
复制
TypeError: Cannot read property '3' of undefined
at mazeRunner
at /home/codewarrior/index.js:87:19
at /home/codewarrior/index.js:155:5
at Object.handleError

任何帮助都将不胜感激!我要在这件事上拔掉头发!

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50749351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档