0

I'm trying to make a program that determines if a grade entered is passing or failing. It stops when the user enters -1. Once the user does enter -1, it has to print out the average of the passing scores and the total amount of scores. It's not really working correctly though. What am I doing wrong?

var countTotal=0;   //variable to store total grades.
var countPassing=0; //variable to store total passing scores.
var x= 0;       //variable to contain sum of all passing scores.
var grade=parseInt(prompt("Please enter a grade."));

while (grade != (-1*1)) 
{
    if (grade > 65) {
        grade=parseInt(prompt("Please enter a grade."));
        document.write(grade + " is passing.<br>");
        x=x+grade;
        countPassing++;
    }
    else {
        grade=parseInt(prompt("Please enter a grade."));
        document.write(grade + " is failing.<br>");
    }

    countTotal++;
}

//Loop ends

document.write("Total # of grades: " + countTotal);         //Prints out total # of passing scores.
document.write("Passing average: " + ((x)/(countPassing))); //Prints out average of all passing scores.
6
  • 2
    Just to satisfy my curiousity ... what is the point of (grade != (-1*1))? Shouldn't (grade != -1) be sufficient? Commented Nov 14, 2012 at 15:04
  • yeah you're absolutely right, but I just wanted to make sure that it would work as an integer. Im quite new to this haha Commented Nov 14, 2012 at 15:08
  • document.write is a bad practice. Commented Nov 14, 2012 at 15:09
  • @user1765804: That's quite alright, I just wanted to point it out. As a note, parseInt should take care of that for you. Commented Nov 14, 2012 at 15:10
  • It's important to pass in the radix argument to parseInt: parseInt(grade, 10). Otherwise, if you get something that starts with a 0, it will be interperted as octal. Commented Nov 14, 2012 at 15:10

1 Answer 1

1

Try working through it by hand. The user enters the very first grade. Then the loop starts. Immediately, you ask for a new grade, discarding the first one. If you move the prompt down to the end of the loop, it should work. Like this:

while (grade != (-1*1)) 
{
    if (grade > 65) {
        document.write(grade + " is passing.<br>");
        x=x+grade;
        countPassing++;
    }
    else {
        document.write(grade + " is failing.<br>");
    }

    grade=parseInt(prompt("Please enter a grade."));
    countTotal++;
}
Sign up to request clarification or add additional context in comments.

2 Comments

oh ok, that makes sense. For the life of me, I could not figure out why it would do that. It actually seemed to be discarding the second value entered, but this works. Thanks.
Hmm. I tested it, and it discarded the first one. For some reason, the average was wrong too before I changed it, but it seems to work now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.