过程代码的意外输出值
unexpected output value from procedural code
我有这段javascript不能工作。它应该接受用户输入并将其存储到玩家输入变量中。然后,它拆分返回的字符串并将其拆分为一个数组,然后通过oc()函数将其转换为对象。最后,函数analyzeUserInput在输入对象中查找关键字,并相应地将文本放入称为text的段落元素中。在这个例子中,如果用户输入"slash"、"poke"、"slice"、"hack"等单词,那么段落元素应该显示"你造成了4点伤害!"但事实并非如此。下面是代码:
<!DOCTYPE html>
<html>
<body>
<p>"oh no theres a monster whatchya gonna do?"</p>
<input id="plyrInput" type="text" />
<button onclick="analyzeUserInput()">Try it</button>
<p id="text"></p>
<script>
var plyrInput;
var plyrInputArray;
var plyrInputAnalysis;
function oc() {
plyrInputArray = plyrInput.split(' ');
var plyrInputObj = {};
for (var i = 0; i < plyrInputArray.length; ++i) {
plyrInputObj[plyrInputArray[i]] = ' ';
}
return plyrInputObj;
}
function analyzeUserInput() {
plyrInput = document.getElementById("plyrInput").text;
oc();
if (plyrInputAnalysis in oc(['use', 'slash', 'hack', 'wield', 'slice', 'sever', 'dismember', 'poke', 'cripple', 'maim', 'mutilate', 'chop', 'rend']) && plyrInputAnalysis in oc(['sword'])) {
document.getElementById("text").innerHTML = "You did 4 damage with your sword!";
}
}
</script>
</body>
</html>
var plyrInput;
var plyrInputArray;
var plyrInputAnalysis;
function oc() {
plyrInputArray = plyrInput.split(' ');
var plyrInputObj = {};
for (var i = 0; i < plyrInputArray.length; ++i) {
//storing these values in an object being blank is not really needed at all!
//plyrInputObj[plyrInputArray[i]] = ' ';
plyrInputObj[i] = plyrInputArray[i]; //acceptable or use the array itself!
}
return plyrInputObj;
}
function analyzeUserInput() {
//plyrInput = document.getElementById("plyrInput").text;//no such property as text
plyrInput = document.getElementById("plyrInput").value;
//you ran this function without storing it so we can't use it
//oc();
var plyrAction = oc();
//you call an undefined variable `plyrInputAnalysis`. So what are we going to do with it?
if (plyrInputAnalysis in oc(['use', 'slash', 'hack', 'wield', 'slice', 'sever', 'dismember', 'poke', 'cripple', 'maim', 'mutilate', 'chop', 'rend']) && plyrInputAnalysis in oc(['sword'])) {
document.getElementById("text").innerHTML = "You did 4 damage with your sword!";
}
}
var plyrInput;
var plyrInputArray;
var plyrInputAnalysis;
//added an acitonList for later usage for yourself
var actionList = {
'use':4,
'slash':4,
'hack':4,
'wield':4,
'slice':4,
'sever':4,
'dismember':4,
'poke':4,
'cripple':4,
'maim':4,
'mutilate':4,
'chop':4,
'rend':4
};
function oc() {
plyrInputArray = plyrInput.split(' ');
var plyrInputObj = {};
for (var i = 0; i < plyrInputArray.length; ++i) {
plyrInputObj[i] = plyrInputArray[i];
}
return plyrInputObj;
}
function analyzeUserInput() {
plyrInput = document.getElementById("plyrInput").value;
var plyrAction = oc(); //cached the returned value from oc
for(var item in plyrAction){ //looping through the plyrActions object
if(actionList[plyrAction[item]]){ //if there is a plyrAction that matches the actionsList we'll continue.
document.getElementById("text").innerHTML = "You did "+actionList[plyrAction[item]]+" damage with your sword!";
}
}
}
虽然这可能看起来比你原来的方法更复杂,但你可以为RPG游戏创建一个更好的代码实例,尽管最好是研究一个IIFE
来包装它并最小化大量代码,而不是多个功能。
例如
function analyzeUserInput() {
plyrInput = document.getElementById("plyrInput").value;
var plyrAction = plyrInput.split(' ');
var plyrInputObj = {};
for (var i = 0; i < plyrAction.length; ++i) {
plyrInputObj[i] = plyrAction[i];
}
for(var item in plyrInputObj ){
if(actionList[plyrInputObj[item]]){
document.getElementById("text").innerHTML = "You did "+actionList[plyrInputObj[item]]+" damage with your sword!";
}
}
}
相关文章:
- 意外的javascript输出
- JavaScript:控制台的意外输出.log('str' + int + int)
- 为什么console.log()在我的代码中输出意外的0
- 在文件中执行 IIFE 时出现意外输出.js
- 基本的JavaScript,如果给出意外的输出,则有多个其他
- JavaScript 二维数组填充中的意外输出
- SyntaxError:JSON.parse:解析PHP输出时出现意外字符
- 带有数组赋值的Javascript意外控制台输出;
- 在一系列对象中意外重复输出
- 为什么脚本会给出意外的输出
- Javascript输出原因意外的令牌<
- 与函数一起使用时this的值.调用产生意外输出
- 过程代码的意外输出值
- js中罕见的意外输出的调试帮助
- 输出HTML时出现意外的令牌错误
- 输出文本文件时出现意外字符串
- 如何输出JSON解析兼容JSON与PHP json_encode?JSON.parse -由于单转义双引号导致的意外令牌
- 响应.json() 上的“意外输出结束”错误
- 使用Moment.js的JS间隔生成函数:意外输出
- 使用for(..)遍历数组时的意外输出)循环