Javascript 函数未接收来自<;的用户输入;表格>;标签

Javascript 函数未接收来自<;的用户输入;表格>;标签,javascript,html,forms,Javascript,Html,Forms,我正在尝试制作一个简单的程序,包括一个音乐间隔游戏。它将让用户尝试用一组给定的注释回答正确的时间间隔。但是,我的check()函数有问题,它将读取用户的答案并用正确的答案进行检查。问题是,当我运行程序并尝试单击检查按钮时,不会得到任何响应 下面是JavaScript代码: var notes = ['A','B','C','D','E','F','G']; var accidentals = ['b','#']; var notesFull = []; for (var i = 0; i &l

我正在尝试制作一个简单的程序,包括一个音乐间隔游戏。它将让用户尝试用一组给定的注释回答正确的时间间隔。但是,我的
check()
函数有问题,它将读取用户的答案并用正确的答案进行检查。问题是,当我运行程序并尝试单击
检查
按钮时,不会得到任何响应

下面是JavaScript代码:

var notes = ['A','B','C','D','E','F','G'];
var accidentals = ['b','#'];
var notesFull = [];

for (var i = 0; i < notes.length; i++) {
    notesFull.push(notes[i] + accidentals[0]);
    notesFull.push(notes[i]);
    notesFull.push(notes[i] + accidentals[1]);
}
notesFull.splice(5,2);
notesFull.splice(12,2);

var intervals = ["U", "m2", "M2", "m3", "M3", "P4", "T", "P5", "m6", "M6", "m7", "M7"];
function game() {
    var setIntOne = Math.floor(Math.random()*notesFull.length);
    var setIntTwo = Math.floor(Math.random()*notesFull.length);
    var oneToTwo = setIntOne - setIntTwo;
    if (oneToTwo < 0) {
        oneToTwo = oneToTwo * -1
    }
    var ques = "What is the interval going UP from " + notesFull[setIntTwo] + " to " + notesFull[setIntOne] + "?";
    document.getElementById("ques").innerHTML = ques;
}

function check() {
    var usrInp = document.getElementById("usrInp");
    if (usrInp.elements[0].value === intervals[oneToTwo]) {
        var resp = "Correct!";
    }
    else if (usrInp.elements[0].value === "null") {
        var resp = "Please input an interval";
    }
    else {
        var resp = "Sorry! Try again";
    }
    document.getElementById("resp").innerHTML = resp;
}
var注释=['A','B','C','D','E','F','G'];
风险值意外=['b','#'];
var notesFull=[];
对于(变量i=0;i
下面是HTML代码:

<button onclick="game()">Click here for test</button>
<p id="ques"></p>
<form id="usrInp">
    Type Answer Here: <input type="text" name="usrAns">
</form>
<button onclick="check()">Check</button>
<p id="resp"></p>
点击此处进行测试

在此处键入答案: 检查


在浏览器中检查控制台:

“未定义oneToTwo”

也许您可以将
var oneToTwo
移动到函数之外

例如:

var notes = ['A','B','C','D','E','F','G'];
var accidentals = ['b','#'];
var notesFull = [];
var oneToTwo;
for (var i = 0; i < notes.length; i++) {
    notesFull.push(notes[i] + accidentals[0]);
    notesFull.push(notes[i]);
    notesFull.push(notes[i] + accidentals[1]);
}
notesFull.splice(5,2);
notesFull.splice(12,2);

var intervals = ["U", "m2", "M2", "m3", "M3", "P4", "T", "P5", "m6", "M6", "m7", "M7"];
function game() {
    var setIntOne = Math.floor(Math.random()*notesFull.length);
    var setIntTwo = Math.floor(Math.random()*notesFull.length);
    oneToTwo = setIntOne - setIntTwo;
    if (oneToTwo < 0) {
        oneToTwo = oneToTwo * -1
    }
    var ques = "What is the interval going UP from " + notesFull[setIntTwo] + " to " + notesFull[setIntOne] + "?";
    document.getElementById("ques").innerHTML = ques;
}

function check() {
    var usrInp = document.getElementById("usrInp");
    if (usrInp.elements[0].value === intervals[oneToTwo]) {
        var resp = "Correct!";
    }
    else if (usrInp.elements[0].value === "null") {
        var resp = "Please input an interval";
    }
    else {
        var resp = "Sorry! Try again";
    }
    document.getElementById("resp").innerHTML = resp;
}
var注释=['A','B','C','D','E','F','G'];
风险值意外=['b','#'];
var notesFull=[];
var oneToTwo;
对于(变量i=0;i
我正在使用学校发行的chromebook,他们阻止了控制台的使用:/@AZNPNOY2000我已经将
var oneToTwo移动了函数外部,并更新了我的答案。无需担心。成功了。谢谢顺便说一句。现在只需要努力得到正确的计算。。。