Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am still having problems with this code. I used if(inputValue == temp); and it's still not working. Any other ideas? Does it work for anyone else?

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
Comments
ChauhanAjay 29-Sep-14 21:55pm    
Kindly check the image link below and let me know if you don't see a dialog box as described in the image.
http://steppic.com/preview/73f44db94b64926fddc70a8839f21078.png
Codster7 29-Sep-14 22:53pm    
I checked the image and it appears good to go. I will check it again. Thank you!
Sergey Alexandrovich Kryukov 29-Sep-14 22:47pm    
Just use the debugger.
—SA
Codster7 29-Sep-14 22:53pm    
which debugger do you recommend?
Sergey Alexandrovich Kryukov 30-Sep-14 0:12am    
I use Visual Studio...
—SA

1 solution

Kindly check the image link below and let me know if you don't see a dialog box as described in the image.
http://steppic.com/preview/73f44db94b64926fddc70a8839f21078.png
 
Share this answer
 
Comments
ChauhanAjay 29-Sep-14 23:02pm    
Can you kindly accept the answer and mark it as solved.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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