JavaScript NaN with HTML -
i'm trying create small maths game code below, keep getting not-a-number error everytime prompt comes up. i'm not entirely sure problem is. have tried round number doesn't situation. point me in right direction?
function start() { // define questions var question = []; // define answers var answer = []; // store user input var input = []; var score = 0; // when user enters answer stored input every prompt for(var = 0; < 10; i++) { var randnum = math.floor(math.random() * 100); var operator = [ '+', '-', '*', '/' ]; var ops = [math.floor(math.random() * operator.length)]; question[i] = randnum + ops[operator] + randnum; answer[i] = randnum + ops[operator] + randnum; input[i] = prompt(question[i]); } // check answer against stored answer, display 1 of either if statement for(var = 0; < answer.length; i++) { if (answer[i] == input[i]) { var outputresult = document.getelementbyid("write"); outputresult.innerhtml += "<p class = corr>question " + (i+1) + " correct.</p>"; } else { outputresult.innerhtml += "<p class = incorr>question " + (i+1) + " incorrect. answer should have been " + answer[i] + ".</p>"; } } }
<header> <h1>maths game!</h1> </header> <button onclick="start()">start</button> <!-- start game --> <div id="write"> </div> <div class="score"></div> </div>
you reference operator array incorrectly. try this:
var operator = ['+', '-', '*', '/']; var ops = math.floor(math.random() * operator.length); question[i] = randnum + operator[ops] + randnum; answer[i] = eval(question[i]); input[i] = prompt(question[i]);
Comments
Post a Comment