[英]Uncaught TypeError Cannot read property 'toLowerCase' of undefined
Currently taking data from an API to create a Jeopardy like game, My issue occurs when I try to take the variable from the API and pass it to checkUserAnswer
and then proceed to use toLowerCase()
on it I receive that error and from what I have gathered from other questions I have no idea how to fix this....any suggestions? 当前从API获取数据以创建像游戏一样的危险游戏,当我尝试从API中获取变量并将其传递给checkUserAnswer
,然后继续在其上使用toLowerCase()
时,出现了我的问题,我收到了该错误,从其他问题中收集的信息,我不知道如何解决。
Code: 码:
var gamePts;
function game(){
this.start = function(click){
if(click == true){
checkUserAnswer();
}else{
getQuestion();
}
}
function getQuestion(){
$.ajax({
url:"http://jservice.io/api/random",
dataType: "json"
})
//after the ajax call the data is sent to the populateQuestion method.
.done(function(data){populateQuestion(data);})
.fail(function(jqXHR, textStatus, errorThrown){
showError(errorThrown);
});
}
function populateQuestion(data){
$("#Category").html(data[0].category.title);
$("#question").html(data[0].question);
var $correct = (data[0].answer);
checkUserAnswer($correct);
}
function checkUserAnswer(correct){
//allows the user answer to appear.
var Correct = this.correct;
$("#useAns").show();
var $userGuess = ($('#AnsBx').val()).toLowerCase();
//var $Correct = correct.toUpperCase();
//$("#userA").val(userGuess);
//$("#userA").html(userGuess);
//$("#userA").html($userGuess);
$("#Result").show();
$("#Result").html(Correct);
if($userGuess === Correct.toLowerCase()){
$("#userA").html($userGuess)
}
else{
$("#userA").html("wrong");
}
// can't figure out why the toUpperCase won't work, getting cannot read property 'toUpperCase' of undefined
}
function guessCount(){
var guesses;
//if user answer = game anwser 1 try and win = true, print number of tries
//if user answer != game anwser ct+1 anwser again
//if user try = 10 end game show answer to question.
}
}
//was last working on the comparasion to allow the game to be functional.
this.start = function(click){
if(click == true){
//no passing argument.
checkUserAnswer();
}else{
getQuestion();
}
}
Now you are calling the method but here the correct variable has null value. 现在,您正在调用该方法,但是此处正确的变量具有空值。
function checkUserAnswer(correct){
//allows the user answer to appear.
var Correct = this.correct;
//here Correct is null
$("#useAns").show();
var $userGuess = ($('#AnsBx').val()).toLowerCase();
//so null.toLowerCase();uncaught error .
if($userGuess === Correct.toLowerCase()){
$("#userA").html($userGuess)
}
.......
}
Edit 1:Work around I can think as of now is to hard code the correct string 编辑1:我现在可以解决的问题是硬编码正确的字符串
$("#Result").html("correct");
if($userGuess === "correct".toLowerCase()){
$("#userA").html($userGuess)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.