Not able to pass variable from one function to another in Javascript -


new javascript , html. in nutshell, attempting update score if user enters correct sum of random math problem. not able pass answer of problem function checks if user entered correct. want score increase one. reason function passing parameter "score" not being recognized it. here quick example of code. more concerned logic instead of looks right of program. know right score not update correctly problem lies in passing of "answer" variable. work through score increase once function @ least gets called. thank time!

    <html> <title></title> <head> <link rel="stylesheet" type="text/css" href="style.css">  </head>  <body onload = "displayproblem()">  <script>  function displayproblem(){  var answer = (firstnum) + (secondnum);      var firstnum = (math.floor(math.random()*10) + 1);  var secondnum = (math.floor(math.random()*10) + 1);   document.getelementbyid("qstns").innerhtml = firstnum +" + " + secondnum    +" = ";   }    function checkanswer(){   var useranswer = document.getelementbyid("answr").value;     if(useranswer === answer){     updatescore();     console.log("score updated");   } console.log("did not update score"); 

}

function displaynext(){  document.getelementbyid("next").style.display = "block"; }  function hidenext(){  document.getelementbyid("next").style.display = "none";  }  function updatescore(){   var score = 0;   score++;   document.getelementbyid("score").innerhtml = score;  }  function clearanswer(){   document.getelementbyid("answr").value = " "; }    </script>  <div id="main">  <div id="qstns">    </div>   </div> <input type="text" id="answr"> score: <label id="score"> 0 </label>  <button type="sumbit" id="submit" onclick="displaynext(), checkanswer    ()">check</button> <button type="submit" id="next" onclick="displayproblem(), hidenext(),    clearanswer()" style="display:none;">next</button>   </body>  </html> 

the prob every time call function "updatescore" reset value of "score" zero.

one way this: read current score first increase...

function updatescore(){   var score = pareseint(document.getelementbyid("score").innerhtml);   score++;   document.getelementbyid("score").innerhtml = score; } 

or this: set score "global" increase...

var score = 0; function updatescore(){   score++;   document.getelementbyid("score").innerhtml = score; } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -