javascript - 为什么我的 JS 程序无法运行?

标签 javascript

我之前发布了一个类似的问题,但它被删除了,因为我觉得我在描述我的问题是什么方面做得很差。

请在此处查看我当前的计划:

http://rich2233.comoj.com/

我的 fiddle : http://jsfiddle.net/rich2233/JGvAD/

我正在尝试模拟这个游戏:http://javascript.internet.com/games/concentration.html

我的版本不会使用图像,而是使用简单的数字。游戏结束后,它会提醒用户需要尝试多少次才能匹配所有内容。它也不会记录时间。

我认为我的代码在逻辑上一切正常,但它无法运行。我是 JS 新手,我的调试技巧有限,但我似乎找不到任何语法错误。有人可以帮助让这个程序运行吗?我们将不胜感激。

var attempts = 0;
var firstClick;             
var secondClick;
var numFoundPairs = 0;         
var click = true;

function display(id)
{
  attempts++;
  if (click == false)
  return;
  var num = parseInt(id);
  if (firstClick == null)
  {
  firstClick = num; 
  document.getElementById(id).value = a[num];
  return;
  }
  if (secondClick == null)
  secondClick = num;

 if (firstClick == secondClick)
 {  
 secondClick = null;
 return;
 }

 document.getElementById(boxNum).value = a[num];
 if (document.getElementById(firstClick).value != 
 document.getElementById(secondClick).value )
  {
  click = false;
  setTimeout ('hide(' + secondClick + ')', 3000);
  setTimeout ('hide(' + firstClick  + ')', 3000);
 }
 else
 {
 /* Show the identical blocks */
 firstClick = null;
 secondClick = null;
 }
 /* If the player found the eight pairs (since 16/2 = 8) */
 if (numFoundPairs == 8)
 {
  /* Let them know how many attempts it took them */
   alert ("You took " + Math.floor(String(attempts)/2) + " attempts.");  
  }
}
/* Function to hide the particular id */
function hide (id)
{
document.getElementById(id).value = "     ";
click = true;
}

/* Create the array of number and distribute them randomly on the grid */
function create()
{
a = new Array (1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 
             6, 6, 7, 7, 8, 8);
for (var i = 0; i < a.length; i++)
{
var idx = Math.floor (Math.random() * a.length);
var tmp = a[i];
a[i] = a[idx];
a[idx] = tmp;
}
}

最佳答案

http://rich2233.comoj.com/处的错误发生在 memory.js 的第 28 行。

Uncaught ReferenceError :boxNum 未定义

您应该考虑使用 JavaScript 控制台,例如 Firebug (适用于 Firefox)或 Chrome 开发者工具(自带,按 CTRL+SHIFT+J 打开它们)。 Firefox 也有一个内置的 JavaScript 错误窗口。

它们会帮助您立即发现此类错误。

关于javascript - 为什么我的 JS 程序无法运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7870677/

相关文章:

javascript - 获取jquery/css/javascript中同类元素的最小宽度

javascript - 如何在mounted() vuejs中销毁或阻止挂载事件

javascript - ReactJS:更改子组件的状态

javascript - 如何在 php 中将数组值设置为 javascript 函数闭包?

javascript - 如何比较两个数组并获取每个匹配项的计数

javascript - jQuery 用户界面 : How to check whether another instance of widget is opened?

Javascript onclick 事件可以显示但不能隐藏

javascript - 提供穷人的仪表板应用程序通信

javascript - 多个 SystemJS/JSPM 包的无摩擦开发

javascript - 如何使用 jquery 或 javascript 从 html 表中删除文本框而不丢失文本框值