2016-10-29 40 views
-2

圖片 My program數組排序功能

我正在做一個程序是你在不同的蘇打水寫並將它們與得分率,我掙扎得到它多少分後排序的數組中蘇打水得到了。

的Html

<!DOCTYPE html> 
<html> 
<head> 

    <script src = "Assignment08.js"></script> 
    <meta charset = "utf-8"> 
</head> 

<body> 
    <h1>Innlevering 8</h1> 
    <img src="julebrus.jpg" alt="Julebrus" style="width:304px;height:228px;"><br> 

    Brustype: <input id = "brustype" /><br> 
    Poeng: <input id = "poeng" /><br> 

    <input type = "button" value = "Legg til brus" onclick = "leggTilBrus()" /> 

    <b><br>Liste over brus:</b> 
    <div id = "brusliste"></div> 

</body> 
</html> 

的Javascript

var brusliste = []; 

function Brus(sodatype, points) { // Brus = Soda (English) 
    this.brustype = sodatype; 
    this.poeng = points; 

    this.sodainfo = function() { 
     return this.brustype + " " + this.poeng; 
    } 
} 

function leggTilBrus(){ 
    var sodatype = document.getElementById("brustype").value; 
    var points = document.getElementById("poeng").value; 

    var sodas = new Brus (sodatype, points); 

    brusliste.push(sodas); 
    visArray(); 
} 

function sortFunction(tall1,tall2){ 
    return tall1.verdi-tall2.verdi; 
} 

function visArray() { 
    document.getElementById("brusliste").innerHTML = ""; 

    for (var i = 0; i < brusliste.length; i++){ 
     document.getElementById("brusliste").innerHTML += brusliste[i].sodainfo() + "<br />"; 
    } 
} 

function init() { 
    var b = new Brus(" ", " "); 

    console.log(b); 
    console.log(b.sodainfo()); 

    document.getElementById("brusliste").innerHTML = b.brustype + " <br />"; 

    brusliste.sort(sortFunction); 
    //brusliste.sort(sorteringsFunksjon()); 
} 

window.onload = init; 

我需要寫一些更獲得那個職位是接受X),無論如何,任何幫助將不勝感激。這件事正在讓我的大腦扭曲。

+0

使用算術運算可能會給出錯誤的值。而是使用三元運算符:'a.prop> b.prop? 1:a.prop Rajesh

+0

'verdi'屬性從哪裏來? – Xufox

+0

verdi屬性來自哪裏?我不確定,只是嘗試在課堂上做的事情。但是當時並沒有真正理解它,所以我現在正在崩潰和燃燒,因爲它現在 – Zuflus

回答

0

我得到它的工作

function Brus(brustype, poeng) { 
    this.brustype = brustype; 
    this.poeng = poeng; 

    this.brusinfo = function() { 
     return this.brustype + " " + this.poeng; 
    }; 
} 

var brusliste = []; 

function leggTilBrus() { 
    var brustype = document.getElementById("brustype").value; 
    var poeng = document.getElementById("poeng").value; 

    var sodas = new Brus (brustype, poeng); 

    brusliste.push(sodas); 
    visArray(); 

    document.getElementById("brustype").value = ""; 
    document.getElementById("poeng").value = ""; 
} 

function sortFunction(a, b){ 
    return b.poeng - a.poeng; 
} 


function visArray() { 
    brusliste.sort(sortFunction); 
    document.getElementById("brusliste").innerHTML = ""; 

    for (var i = 0; i < brusliste.length; i++) { 
     document.getElementById("brusliste").innerHTML += brusliste[i].brusinfo() + "<br />"; 
    } 
} 

function init() { 
    var b = new Brus(" ", " "); 

    console.log(b); 
    console.log(b.brusinfo()); 

    document.getElementById("brusliste").innerHTML = b.brusinfo() + " <br />"; 
} 

window.onload = init; 

我所做的sortfunction錯誤,並且也缺席兩場的getElementById。此區域:

 document.getElementById("brustype").value = ""; 
     document.getElementById("poeng").value = ""; 
    } 

    function sortFunction(a, b){ 
     return b.poeng - a.poeng; 
    }