简体   繁体   中英

Javascript: Calculating Min/Max/Average

I wrote this code in my html site, in Javascript, but is not working right. Most times it seems to ignore some entries and just randomly selects which is the min/max value. Also, when I tried to calculate average values, I got a string instead of a number, even though the variable is declared as 0 in the beginning. eg performing 0+1+1+2+3+5 = 011235 instead of 12. Here is the code, thanks in advance.

**EDIT: I added the student average code in the end, but it doesn't work, it doesn't show any results on the page, not even the "student" + [i] part. On the other hand, the parseInt() command worked, and made everything work as it should, thank you :)

<script language = "javascript">
function myFunction() {
var course0 = [];
var course1 = [];
var course2 = [];

var minstugrade = 100;
var maxstugrade = 0;
var minstugradetext = "";
var maxstugradetext = "";

var stuavgarr = [];
var minstuavg = 100;
var maxstuavg = 0;
var minstuavgtext = "";
var maxstuavgtext = "";

var mincougrade = 100;
var maxcougrade = 0;
var mincougradetext = "";
var maxcougradetext = "";

var mincouavg = 100;
var maxcouavg = 0;
var mincouavgtext = "";
var maxcouavgtext = "";
var couavg = 0;

//add form items to array
var x = document.getElementById("course0");
var i;
    for (i = 0; i < x.length ;i++) {
        course0.push(parseInt(x.elements[i].value));
    }   
var x = document.getElementById("course1");
var i;
    for (i = 0; i < x.length ;i++) {
        course1.push(parseInt(x.elements[i].value));
    }
var x = document.getElementById("course2");
var i;
    for (i = 0; i < x.length ;i++) {
        course2.push(parseInt(x.elements[i].value));
    }

    //calculate course & student min/max
    for (i = 0; i < course0.length; i++) {
    if (course0[i] < mincougrade) {
    mincougrade = course0[i];
    mincougradetext = "course0";
    }
    if (course0[i] > maxcougrade) {
    maxcougrade = course0[i];
    maxcougradetext = "course0";
    }
    if (course0[i] < minstugrade) {
    minstugrade = course0[i];
    minstugradetext = "student" + [i];
    }
    if (course0[i] > maxstugrade) {
    maxstugrade = course0[i];
    maxstugradetext = "student" + [i];
    }
    }   

    for (i = 0; i < course1.length; i++) {
    if (course1[i] < mincougrade) {
    mincougrade = course1[i];
    mincougradetext = "course1";
    }
    if (course1[i] > maxcougrade) {
    maxcougrade = course1[i];
    maxcougradetext = "course1";
    }
    if (course1[i] < minstugrade) {
    minstugrade = course1[i];
    minstugradetext = "student" + [i];
    }
    if (course1[i] > maxstugrade) {
    maxstugrade = course1[i];
    maxstugradetext = "student" + [i];
    }
    }

    for (i = 0; i < course2.length; i++) {
    if (course2[i] < mincougrade) {
    mincougrade = course2[i];
    mincougradetext = "course2";
    }
    if (course2[i] > maxcougrade) {
    maxcougrade = course2[i];
    maxcougradetext = "course2";
    }
    if (course2[i] < minstugrade) {
    minstugrade = course2[i];
    minstugradetext = "student" + [i];
    }
    if (course2[i] > maxstugrade) {
    maxstugrade = course2[i];
    maxstugradetext = "student" + [i];
    }
    }       

    //calculate course average
    for (i = 0; i < course0.length; i++) {
    couavg += course0[i];
    }
    couavg = couavg / course0.length
    if (couavg < mincouavg) {
    mincouavg = couavg;
    mincouavgtext = "course0";
    }
    if (couavg > maxcouavg) {
    maxcouavg = couavg;
    maxcouavgtext = "course0";
    }

    couavg = 0;
    for (i = 0; i < course1.length; i++) {
    couavg += course1[i];
    }
    couavg = couavg / course1.length
    if (couavg < mincouavg) {
    mincouavg = couavg;
    mincouavgtext = "course1";
    }
    if (couavg > maxcouavg) {
    maxcouavg = couavg;
    maxcouavgtext = "course1";
    }

    couavg = 0;
    for (i = 0; i < course2.length; i++) {
    couavg += course2[i];
    }
    couavg = couavg / course2.length
    if (couavg < mincouavg) {
    mincouavg = couavg;
    mincouavgtext = "course2";
    }
    if (couavg > maxcouavg) {
    maxcouavg = couavg;
    maxcouavgtext = "course2";
    }

    //calculate student average
    for (i = 0; i < course0.length; i++) {
    stuavgarr[i] += course0[i];
    stuavgarr[i] += course1[i];
    stuavgarr[i] += course2[i];
    }

    for (i=0; i < stuavgarr.length; i++) {
    stuavgarr[i] = stuavgarr[i] / course0.length;
    if (stuavgarr[i] < minstuavg) {
    minstuavg = stuavgarr[i];
    minstuavgtext = "student" + [i];
    }
    if (stuavgarr[i] > maxstuavg) {
    maxstuavg = stuavgarr[i];
    maxstuavgtext = "student" + [i];
    }
    }


    document.getElementById("studmaxgrade").innerHTML = "Student that achieved the max grade is " + maxstugradetext
    document.getElementById("studmingrade").innerHTML = "Student that achieved the min grade is " + minstugradetext
    document.getElementById("studmaxavg").innerHTML = "Student that achieved the max average is " + maxstuavgtext
    document.getElementById("studminavg").innerHTML = "Student that achieved the min average is " + minstuavgtext
    document.getElementById("courmaxgrade").innerHTML = "The course in which the max grade is scored is " + maxcougradetext
    document.getElementById("courmingrade").innerHTML = "The course in which the min grade is scored is " + mincougradetext
    document.getElementById("courmaxavg").innerHTML = "The course in which the max average grade is scored is " + maxcouavgtext
    document.getElementById("courminavg").innerHTML = "The course in which the min average grade is scored is " + mincouavgtext

    }
</script>

The value of an input is a string, thus a + b will be interpreted as appending one string to another.

If you make sure the first parameter (a in this case) is an integer a + b will result in the two being mathematically adding the two

console.log( '0' + 1 + 2 + 3 + 4 );             //* outputs 01234
console.log( parseInt( 0 ) + 1 + 2 + 3 + 4 );   //* outputs 10

JSFiddle

Ok for a start you seem very confused about document.getElementById

This does not address a javascript variable at all......

This literally "gets the document element by its id". Here is an example of how to use it...

<html>
<img id='my_new_selfie' src='me.jpg'>
....
....
<script>
alert (document.getElementById('my_new_selfie').src)
</script>

This would simply pop up an alert with the text that describes the src of the document object who's id is 'my_new_selfie' that is....

[me.txt]

The reason that document.getElementById was introduced to javascript was to save developers learning the DOM (document object model) in order to access objects. It allows you to simply give you object an id and change things about it using the id

In the above example I could use a script or button to change the image source an example of this might be using the onclick event of another object on the page like a button...

onclick='document.getElementById('my_new_selfie').src='new_pic_of_me.JPG'

It is not used to identify variables in a javascript

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM