html - Javascript string comparison -
Therefore, im trying to create interactive quiz and im almost complete. Only the answer part of the investigation fails badly, I have arrays for questions, options and correct answers, but the comparison just fails.
function check (clicked_id) {document.getElementById ("question"). Value = "" + questionArray [i]; document.getElementById ("OptionA"). Value = "" + aArray [i]; document.getElementById ("OptionB"). Value = "+ + bArray [i]; document.getElementById ("OptionC") Value = "+ + cArray [i]; document.getElementById ("OptionD"). Value = "+ + dArray [i]; var selected = document.getElementById (clicked_id) .value; Var answers = answer [i]; document.getElementById ("text"). innerHTML = Answer + "& lt; br & gt; + Selected; If (selected == answer) {document.getElementById ("comparison"). InnerHTML = "true"; } And {document.getElementById ("comparison"). InnerHTML = "wrong"; }} and html part
& lt; input onclick = "check (this.id)" type = "button" value = "Click here to start" id = "question" /> gt; & Lt; Br> & Lt; Br> & Lt; Input onclick = "check (this.id)" type = "button" value = "" id = "option" /> & Lt; Input onclick = "check (this.id)" type = "button" value = "" id = "optionsb" /> & Lt; Input onclick = "check (this.id)" type = "button" value = "" id = "option c" /> & Lt; Input onclick = "check (this.id)" type = "button" value = "" id = "option" /> < Div id = "text" & gt; & Lt; / Div & gt; & Lt; Div id = "comparison" & gt; & Lt; / Div & gt; For any reason, it just does not work and why do not I understand it.
You are adding white space on the value front, but before the comparison is done, Never remove at least I can not see if you do. Use it.
var selected = document.getElementById (clicked_id) .value.trim ();
Comments
Post a Comment