I want to compare two strings in JavaScript, but my code isn't working.
I think if(answer==correct_answer_arr[0]) is wrong, but I don't know
what the problem is, or how I can fix it.
*I already checked that "correct_answer_arr" has no problem, which has correct information
JS Code:
var correct_answer_arr = new Array();
$('document').ready(function() {
var str;
var request = new XMLHttpRequest();
request.open('get', 'http://localhost:8080/new/text.jsp', true);
request.onload = function() {
str = request.responseText;
correct_answer_arr = str.split(",");
};
request.send();
});
function keycheck(event) {
if (event.keyCode == 13) {
var answer = $("input").val();
if (answer == correct_answer_arr[0]) {
alert("correct");
} else {
alert("wrong");
}
}
}
==will compare strings, what makes you think it is wrong? console.log both values to see if they are what you think they are$('document')won't select the document, it is$(document)<input>on your page? The comparison will be 'wrong' if you don't have a valid answer in$("input").val().keycheckbefore the request has returned?responseText?