Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote a program that prompts user input for a math question. It says "Correct" if the input is correct, but it also says "correct" if the input is not correct. Can't find the error. Any suggestions? Thank you!
XML
<script language="JavaScript"></script>
<style type="text/css">
.style1 {
    font-family: Arial;
    font-size: x-small;
}
</style>
</head>

<body><p><script><!-- hide from old browser

var inputText, feedback;   // variables for holding user input and feedback

var inputValue, loopCounter, correctCount, temp;  // integer variables



loopCounter = 0;  // set the starting value of variables

correctCount = 0;



var num1, num2;



num1 = Math.round(Math.random()*100);

num2 = Math.round(Math.random()*100);



inputText = window.prompt("What's " + num1 + " plus " + num2 + "? Enter -1 to stop.", "0");



inputValue = parseInt(inputText);



while (inputValue != -1){

          temp = num1 + num2;

          if (inputValue == temp){

            correctCount++;

            feedback = "Correct! ";

          } else if (inputValue != temp) {

            feedback = "Sorry. Not correct";

          }

          loopCounter++;

          num1 = Math.round(Math.random()*100);

          num2 = Math.round(Math.random()*100);

          inputText = "";

          inputText = window.prompt("" + feedback + "Now, what's " + num1 + " plus " + num2 + "? (Enter -1 to stop:)", "0");

          inputValue = 0;

          inputValue = parseInt(inputText);

}



document.write("<font size='5' color='#0000FF'><b>You have got " + correctCount + " correct out of " + loopCounter + " tries.</b></font>")
Posted
Updated 29-Sep-14 15:27pm
v4

Change the following line from
JavaScript
if (inputValue = temp)


To

JavaScript
if (inputValue == temp)
 
Share this answer
 
Dude Your Code Just Works Fine.

Use FireBug in mozilla to debug your code
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900