jquery - div inside if statement javascript -
for reason, div tags not displayed in webpage when program run. tested every other component , have figured out problem setting of value of div tags in if statement. problem not know how fix problem , still have program run way want to.
<div id="answerdisplay"></div> <div id="correctornot"></div> <script> var div var div2 var rightanswer var problemnum =math.floor((math.random() * 3) + 1); if (problemnum == 1){ rightanswer = 14; div = document.getelementbyid("answerdisplay"); div.textcontent = "4 + 2 * (10 - 3 * 2) / 4 + 8 = ?"; } else if (problemnum == 2){ rightanswer = 8; div = document.getelementbyid("answerdisplay"); div.textcontent = "(6 - 3) * 2 + (3^2 - 5) / 2 = ?"; } else if (problemnum == 3){ rightanswer = 6; div = document.getelementbyid("answerdisplay"); div.textcontent = "5 - 3 + 4^2 / (8 - 4 / 2) = ?"; } else if (problemnum == 4){ rightanswer = 3; div = document.getelementbyid("answerdisplay"); div.textcontent = "5^2 - (6 * 2) * 2 * (5 - 2) = ?"; } function checkrightanswer (){ var answer = document.getelementbyid('input').value if (problemnum == 1 && answer == 14){ div2 = document.getelementbyid("correctornot"); div2.textcontent = "correct"; problemnum =math.floor((math.random() * 3) + 1); } else if (problemnum == 2 && answer == 8){ div2 = document.getelementbyid("correctornot"); div2.textcontent = "correct"; problemnum =math.floor((math.random() * 3) + 1); } else if (problemnum == 3 && answer == 6){ div2 = document.getelementbyid("correctornot"); div2.textcontent = "correct"; problemnum =math.floor((math.random() * 3) + 1); } else if (problemnum == 4 && answer == 3){ div2 = document.getelementbyid("correctornot"); div2.textcontent = "correct"; problemnum =math.floor((math.random() * 3) + 1); } else { div2 = document.getelementbyid("correctornot"); div2.textcontent = "incorrect, try again"; } } </script>
your code seems work fine. don't see input container included in code snippet provided. perhaps need make sure id correct there? cleaned code bit:
var div, div2, rightanswer, problemnum = math.floor((math.random() * 3) + 1); if (problemnum === 1) { rightanswer = 14; div = document.getelementbyid("answerdisplay"); div.textcontent = "4 + 2 * (10 - 3 * 2) / 4 + 8 = ?"; } else if (problemnum === 2) { rightanswer = 8; div = document.getelementbyid("answerdisplay"); div.textcontent = "(6 - 3) * 2 + (3^2 - 5) / 2 = ?"; } else if (problemnum === 3) { rightanswer = 6; div = document.getelementbyid("answerdisplay"); div.textcontent = "5 - 3 + 4^2 / (8 - 4 / 2) = ?"; } else if (problemnum === 4) { rightanswer = 3; div = document.getelementbyid("answerdisplay"); div.textcontent = "5^2 - (6 * 2) * 2 * (5 - 2) = ?"; } function checkrightanswer() { var answer = document.getelementbyid('input').value; if (problemnum === 1 && answer === 14) { div2 = document.getelementbyid("correctornot"); div2.textcontent = "correct"; problemnum = math.floor((math.random() * 3) + 1); } else if (problemnum === 2 && answer === 8) { div2 = document.getelementbyid("correctornot"); div2.textcontent = "correct"; problemnum = math.floor((math.random() * 3) + 1); } else if (problemnum === 3 && answer === 6) { div2 = document.getelementbyid("correctornot"); div2.textcontent = "correct"; problemnum = math.floor((math.random() * 3) + 1); } else if (problemnum === 4 && answer === 3) { div2 = document.getelementbyid("correctornot"); div2.textcontent = "correct"; problemnum = math.floor((math.random() * 3) + 1); } else { div2 = document.getelementbyid("correctornot"); div2.textcontent = "incorrect, try again"; } }
<div id="answerdisplay"></div> <div id="correctornot"></div> <input type="text" id="input" />
Comments
Post a Comment