0

I am writing a program to display total test grades and avg with a loop. however when I try to test it, I cant get the final message "Good Job" to show up and I am getting a error for unbalanced tree and undefined "Avg" variable. I dont see how "avg" is undefined when it works during the loop just not after

var Testscore = 0;
var Testcount = 0;
var Total = 0;
var Avg = 0;
do {
  Testscore = prompt("Enter the test score ", 0);
  Testcount = (Testcount + 1);
  Total = parseFloat(Testscore) + parseFloat(Total);
  Avg = (Total / Testcount);
  document.write("Total test score is " + Total + "<br>");
  document.write("Average test score is " + Avg + "<br>" + "<br>");
} while (Testcount < 4)
Avg = (Total / Testcount);
if (avg > 80) {
  document.write("Good Job!");
} else {
  documet.write("Try harder..");
}
Hodrobond
  • 1,545
  • 13
  • 17

1 Answers1

0

You just had a few typos in your code!

avg and Avg are different, as variables are case sensitive.

Additionally, there's a documet instead of document

var Testscore = 0;
var Testcount = 0;
var Total = 0;
var Avg = 0;
do
    {
    Testscore = prompt("Enter the test score ",0);
    Testcount = (Testcount + 1);
    Total = parseFloat(Testscore) + parseFloat(Total);
    Avg = (Total / Testcount);
    document.write("Total test score is " + Total + "<br>");
    document.write("Average test score is " + Avg + "<br>" + "<br>");
    } while (Testcount < 4)
    Avg = (Total / Testcount);
    if (Avg > 80)
        {
          console.log("Good Job!");
        }
        else
        {
          console.log("Try harder..");
        }
Hodrobond
  • 1,545
  • 13
  • 17
  • Oh, ok usually how it is for me =) and I didnt know about the consol.log command, is there a reason I should use it over document.write? – Tstanley12 May 19 '17 at 22:02
  • That was mainly for demonstrative purposes, however [I would recommend against using Document.write](http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice). – Hodrobond May 19 '17 at 22:04
  • If you're just trying to get the result shown *somewhere* in order to see it, console is safer/more consistent than `document.write()`, my main "icky" about using it for testing is that it [clears the page](http://stackoverflow.com/questions/10873942/document-write-clears-page) – Hodrobond May 19 '17 at 22:05