2016-07-02 104 views
0

我在Javascript中使用了我的HTML網站編寫了這段代碼,但工作不正確。大多數時候它似乎忽略了一些條目,只是隨機選擇最小/最大值。另外,當我嘗試計算平均值時,即使變量在開始時聲明爲0,我也得到了一個字符串而不是數字。例如執行0 + 1 + 1 + 2 + 3 + 5 = 011235而不是12. 這裏是代碼,在此先感謝。使用Javascript:計算最小/最大/平均值

**編輯:我在最後添加了學生平均代碼,但它不起作用,它不在頁面上顯示任何結果,甚至沒有「學生」+ [i]部分。在另一方面,parseInt函數()命令的工作,並提出了一切工作,因爲它應該謝謝:)

<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> 
+0

使用'parseFloat'投字符串轉換爲數字變量。 – GuyT

+1

輸入的'value'將**總是**給你一個String。即使變量被定義爲Number,您現在也將其重新分配給... String。 –

回答

1

輸入的值是一個字符串,因此A + B將被解釋爲一個附加字符串到另一個。

如果你確保第一參數(在這種情況下)是一個整數A + B會導致兩個數學上增加兩個

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

JSFiddle

0

好的一開始你看起來很困惑 的document.getElementById

這並沒有解決一個javascript變量在所有......

這實際上「通過其ID獲取文檔元素」。 下面是如何使用它的一個例子...

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

這隻會彈出一個警告與描述 文檔對象的src誰的ID是「my_new_selfie」 是文字.. ..

[me.txt]

那的document.getElementById引入JavaScript的是節省開發者學習DOM(文檔對象模型),以便訪問的對象的理由。 它可以讓你簡單地給你對象的ID,並使用id

在上面的例子中,我可以使用腳本或按鈕來改變圖像源 這樣的例子改變它的東西可能使用的onclick事件就像一個按鈕,頁面上的其他對象...

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

的它不是用來識別一個javascript

+0

我使用document.getElementById來捕獲一個表單,而不是一個變量。你不能在這裏看到這個表單,因爲它是在一般的HTML中編碼的,而不是javascript部分。 – SpK

相關問題