Javascript 简单的数学游戏问题(初学者的东西)
我有一个学校项目,我们正在制作一个没有UI的小JavaScript游戏;这意味着我们只能使用提示、警报或其他弹出脚本 游戏应该可以运行,至少在我用模块把它拆开之前是这样。这是一个简单的数学游戏,用户得到随机+的问题,并且必须正确回答 问题 我似乎无法向用户提供任何提示。我在chrome开发工具中调试时也遇到了问题,你能马上看到任何错误吗?感谢所有的帮助:) 这是我的名字 这是我们的代码,我只发布了重要的部分-我忽略了index.html和Mathgame.js,因为它们似乎工作得很好,而且它们不包含很多代码 MathGame.logic.js MathGame.play.jsJavascript 简单的数学游戏问题(初学者的东西),javascript,html,math,project,Javascript,Html,Math,Project,我有一个学校项目,我们正在制作一个没有UI的小JavaScript游戏;这意味着我们只能使用提示、警报或其他弹出脚本 游戏应该可以运行,至少在我用模块把它拆开之前是这样。这是一个简单的数学游戏,用户得到随机+的问题,并且必须正确回答 问题 我似乎无法向用户提供任何提示。我在chrome开发工具中调试时也遇到了问题,你能马上看到任何错误吗?感谢所有的帮助:) 这是我的名字 这是我们的代码,我只发布了重要的部分-我忽略了index.html和Mathgame.js,因为它们似乎工作得很好,而且它们
到目前为止,我只能找出代码中的小问题。由于使用的是严格模式,因此无法全局访问窗口对象的属性。因此,您需要使用
window.alert
或设置一个变量:
var alert = this.alert; // "this" being the global, window object
我注意到的第一件事是,您的math.play
函数声明没有右括号。我修好了。但真正的问题是,在创建mathGame
属性之前,您正在引用这些属性。例如,在mathGame.play()
的定义中,您运行了函数mathGame.ui.startCountDown()
但是mathGame.ui
是在调用下面的函数中定义的。所以我把它从函数中取出,这样它就可以访问它了。这是你剧本的普遍问题
还有一部分,你调用一个对象,就好像它是一个函数一样:
correct = (question() === guess);
question
已定义为函数mathGame.logic.getQuestion()的返回值代码>这是一个字符串。我想你把它和这个混淆了:
question = mathGame.logic.getQuestion;
correct = (question() === guess); // now this works
我还修复了一些我觉得多余的东西。如果希望整个脚本处于严格模式,请在严格模式下对其创建闭包:
(function() {
"using strict";
// everything below is in strict mode
})();
以下是完整的代码:
(function() {
"using strict";
var mathGame = {},
alert = this.alert,
prompt = this.prompt;
mathGame.play = function() {
var question, guess, answer, correct, questionGuess;
// Starts game for user
mathGame.ui.startCountDown();
// Starts the timer in .logic
// mathGame.logic.startCountDown();
// Get random math
mathGame.logic = (function() {
var createQuestion, getQuestion;
createQuestion = function() {
var tal1, tal2;
tal1 = Math.ceil(Math.random() * 10);
tal2 = Math.ceil(Math.random() * 10);
return {
tal1: tal1,
tal2: tal2,
result: function() {
return tal1 + tal2;
}
};
};
getQuestion = function() {
return createQuestion();
};
return {
getQuestion: getQuestion
};
}());
question = mathGame.logic.getQuestion();
// Send random math to User
questionGuess = mathGame.ui.askMathQuestion(question.tal1, question.tal2);
// The users guess
guess = mathGame.ui.returnMathGuess;
// See if the question is the same as the guess
correct = (question === guess);
// Show the user how it went
mathGame.ui.showResult(correct, guess, question);
};
mathGame.ui = {
startCountDown: function() {
// Visa ready set go
alert("READY");
alert("SET");
alert("GO");
},
askMathQuestion: function() {
prompt("askMathQuestion");
//shows a math question to user
// return Number(prompt(value1 + symbol + value2));
// e.g. value1 = 12
// value2 = 13
// symbol = "+"
// 12 + 13
// return user guess
},
returnMathGuess: function() {},
showResult: function() {}
};
mathGame.play();
}).call(this); // global object
请注意,在代码的HTML部分,我取出了一些脚本文件,因为它们在网站中不存在。如果您再次需要它们,请参阅:
<script src="mathGame.js"></script>
<script src="mathGame.logic.js"></script>
<script src="mathGame.ui.js"></script>
<script src="mathGame.play.js"></script>
您遇到了什么问题?也许您可以将其放在JSFIDLE上,这样我们就可以看到哪些有效,哪些无效?哦,抱歉,马上将其添加到帖子中如何将多个JS文件放入JSFIDLE?单击“添加资源”选项卡并将JavaScript文件URL插入文本框中。我相信它必须是一个在线网址。太棒了!我将检查我的代码中的ASE错误,正如我所说,我已经将我的JS分为这4个文件,我将进行查看,并在我完成审阅后返回:)感谢您的回答抱歉,我对这个很陌生哈哈:这太棒了,但由于某些原因,我无法将其拆分为原始的4个文件:P您介意将其设置为可以在我的4个文件中使用吗:)?第一个文件应该具有mathGame的定义(var mathGame={}
)。然后在下一个文件中输入mathGame.logic
函数定义。下一个文件应该是mathGame.ui
定义。最后是mathGame.play
definition。-所以我应该在mathGame.js中定义mathGame.logic mathGame.ui和mathGame.play?
(function() {
"using strict";
var mathGame = {},
alert = this.alert,
prompt = this.prompt;
mathGame.play = function() {
var question, guess, answer, correct, questionGuess;
// Starts game for user
mathGame.ui.startCountDown();
// Starts the timer in .logic
// mathGame.logic.startCountDown();
// Get random math
mathGame.logic = (function() {
var createQuestion, getQuestion;
createQuestion = function() {
var tal1, tal2;
tal1 = Math.ceil(Math.random() * 10);
tal2 = Math.ceil(Math.random() * 10);
return {
tal1: tal1,
tal2: tal2,
result: function() {
return tal1 + tal2;
}
};
};
getQuestion = function() {
return createQuestion();
};
return {
getQuestion: getQuestion
};
}());
question = mathGame.logic.getQuestion();
// Send random math to User
questionGuess = mathGame.ui.askMathQuestion(question.tal1, question.tal2);
// The users guess
guess = mathGame.ui.returnMathGuess;
// See if the question is the same as the guess
correct = (question === guess);
// Show the user how it went
mathGame.ui.showResult(correct, guess, question);
};
mathGame.ui = {
startCountDown: function() {
// Visa ready set go
alert("READY");
alert("SET");
alert("GO");
},
askMathQuestion: function() {
prompt("askMathQuestion");
//shows a math question to user
// return Number(prompt(value1 + symbol + value2));
// e.g. value1 = 12
// value2 = 13
// symbol = "+"
// 12 + 13
// return user guess
},
returnMathGuess: function() {},
showResult: function() {}
};
mathGame.play();
}).call(this); // global object
<script src="mathGame.js"></script>
<script src="mathGame.logic.js"></script>
<script src="mathGame.ui.js"></script>
<script src="mathGame.play.js"></script>