PDA

View Full Version : Please help with displaying averages javascript



unz
11-05-2011, 01:15 PM
I cannot get my code to work to display the averages of four grades, can anyone with more experience have a look and see if they can help? Many thanks in advance.

<?xml version="1.0" encoding="utf-8"?>


<head><title></title><script language="javascript" type="text/javascript">


<!--


function average() {


var gradeone = getelementbtid('grade1');=parseInt(document.myform .grade1.value);


var gradetwo = getelementbtid('grade2');=parseInt(document.myform .grade2.value);


var gradethree = getelementbtid('grade3');=parseInt(document.myform .grade3.value);


var gradefour = getelementbtid('grade4');=parseInt(document.myform .grade4.value);


var average=(grade1+grade2+grade3+grade4)/4;


document.write("The average of your marks is: "+average);


}

Window.alert(average);



if ((average >=90) && (average <=100)) {

document.write("You got an A!");

}

else

((average >=80) && (average <=90)) {

document.write("You got an B!");

}

else

((average >=70) && (average <=80)) {

document.write("You got an C!");

}



else

((average >=60) && (average <=70)) {

document.write("You got an D!");

}



else

{

document.write("You failed");

}










-->


</script></head>


<body>




<FORM>


Input first grade:<INPUT TYPE=TEXT NAME="grade1" id="grade1" SIZE=20 >


<BR><BR>Input second grade:<INPUT TYPE=TEXT NAME="grade2" id="grade2" SIZE=20 >

<BR><BR>Input third grade:<INPUT TYPE=TEXT NAME="grade3" id="grade3" SIZE=20 >

<BR><BR>Input fourth grade:<INPUT TYPE=TEXT NAME="grade4" id="grade4" SIZE=20 >

<BR><BR><input type="button" value="Calculate" onClick="calculation()">



</FORM>
</body>



</html>

weegillis
11-25-2011, 05:53 PM
First point to consider: Where is the DOCTYPE? What we see in the OP is an XML declaration, not one for HTML.

Back in the day when standards were emerging and XHTML doctype emerged with stricter standards based syntax, it was the norm for some pages to use an XML declaration to switch a browser into quirks mode. It's possible the @OP might have seen this in some older source code and mistook it for an HTML document type, which it is not.

Second, we're seeing an awful lot of inconsistency in the markup style. I.E.; (1) mixed case in tags and attributes; (2) mixed use of quotes/no quotes on attribute values; to name two examples. This could be cleaned up a bit to make the code more readable, and easier to debug. Suggest using all lowercase on tags and attributes; and, quote every attribute value, for clarity and consistency.

Please, give this clean up a whirl, and re-post. It will be easier to suggest a solution (if one is even needed) when the code is tidy and consistent.

DaveSawers
11-26-2011, 04:49 AM
var gradeone = getelementbtid('grade1');=parseInt(document.myform .grade1.value);
var gradetwo = getelementbtid('grade2');=parseInt(document.myform .grade2.value);
var gradethree = getelementbtid('grade3');=parseInt(document.myform .grade3.value);
var gradefour = getelementbtid('grade4');=parseInt(document.myform .grade4.value);
var average=(grade1+grade2+grade3+grade4)/4;


Well DUH! You define variables gradeone, etc. then try to use grade1, etc..

weegillis
11-26-2011, 02:26 PM
Methinks that @OP has likely failed this class assignment already, and hasn't need nor inclination to return for ours, or anybody's responses; ergo, mine above hovered around the edges. The HTML is atrocious, and the script errors go without saying. Until a spammer littered the thread yesterday, it sat idle for three weeks without a response. Where would one begin to answer the OP?

Of course we can see a number of errors, or fuzzy logic in the source code, not the least of which is pointed out above, none of which will ever be parsed, owing to errant syntax:
... ;= ... on top of the fact that the 'average()' method never gets called. Even with correct syntax and consistent variable names, it won't matter.
<input type="button" value="Calculate" onClick="calculation()">

The IF ELSE statement is something else to behold, with all conditions tested for that will never exist. The only result can be 'You failed' since 'average' is out of its scope, and technically undefined when handed to alert() AT PAGE LOAD.

If @OP had actually written the source, they would have seen these errors. As it stands, most of the code looks like copy/paste from different sources, with zero understanding on the part of @OP. Seems we have a student who wants to pass by taking shortcuts. Can only wish them good luck with that.