我有一点问题我无法解决。我做了某种游戏来按键,我使用数字生成器来生成你需要按下的键的数量。当然,你需要能够看到你需要按的字母,而不仅仅是js看到的数字。
<script>
var key = Math.floor(Math.random() * 26 + 97);
var p = -10;
var x;
key = 13;
var letter;
function lespres(event) { //action when key pressed
clock ();
document.getElementById('key2b').innerHTML = "PRESS KEY: " + key;
x = event.which;
document.getElementById('keypressed').innerHTML = "KEY PRESSED: " + x;
if(x == key) { //correct key action
p = +p + 10;
document.getElementById('punten').innerHTML = "POINTS: " + p ;
key = Math.floor(Math.random() * 26 + 97);
document.getElementById('key2b').innerHTML = "PRESS KEY: " + key;
}
else { //incorrect key action
p = +p - 5;
document.getElementById('punten').innerHTML = "POINTS: " + p ;
}
}
</script>
我创建了一个将密钥号翻译成字母的功能,但我不知道如何在我的代码中实现它。
function lettergen() {
if (key == 97) {
letter = a;
}
else if (key == 98) {
letter = b;
}
else if (key == 99) {
letter = c;
}
} //and all the way to z
如果我将此函数放入函数lespres并将“key”更改为
中的“letter”document.getElementById('key2b').innerHTML = "PRESS KEY: " + key;
它说“未定义”。 有人能帮帮我吗?帮助我很感激:D
答案 0 :(得分:0)
function lettergen() {
// !! avoiding side-effects. variable 'letter' can be defined in higher scope
var letter = 'unknown';
if (key == 97) {
letter = a;
} else if (key == 98) {
letter = b;
} else if (key == 99) {
letter = c;
}
//and all the way to z
// !!!
return letter;
}
函数不返回任何内容(返回var KEY_TO_LETTER = {
97: 'a',
98: 'b',
}
function lettergen(key) {
return KEY_TO_LETTER[key]
}
)
var express = require("express");
var app = express();
var router = express.Router();
var path = __dirname + '/views/';
router.use(function (req,res,next) {
console.log("/" + req.method);
next();
});
app.use('/assets', [
express.static(__dirname + '/assets/'),
]);
router.get("/",function(req,res){
res.sendFile(path + "");
});
app.use("/",router);
app.use("*",function(req,res){
res.sendFile(path + "404.html");
});
P.S。如果你重写你的功能,也许会更简单。尝试使用object而不是if-else语句。
<script src="assets/global/plugins/jquery.min.js" type="text/javascript"></script>
<script src="assets/global/plugins/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>