Array.sort校正

时间:2015-05-14 08:14:54

标签: javascript arrays sorting

我想知道我是否正在排序正确,因为我正在做一个排行榜,从最高到最低显示得分。 “临时1-5”变量已经有了它们的值,我想知道我是否有任何错误。

<HTML>
<!Foundation Page for building our Javascript programs>
<HEAD>
<TITLE>The Foundation Page </TITLE>
<SCRIPT LANGUAGE = "JavaScript">
var name1;
var name2;
var name3;
var name4;
var name5;

var temp1;
var temp2;
var temp3;
var temp4;
var temp5;

var ask;

function main() {
    start()
    totalscore()
    leaderboard()
}

function start() {
    ask = prompt("How many people are playing")
    if (ask == 3) {
        name1 = prompt("What is the first player's name?")
        name2 = prompt("What is the second player's name?")
        name3 = prompt("What is the third player's name?")
        number = 3
    }
    if (ask == 4) {
        name1 = prompt("What is the first player's name?")
        name2 = prompt("What is the second player's name?")
        name3 = prompt("What is the third player's name?")
        name4 = prompt("What is the forth player's name?")
        number = 4
    }
    if (ask == 5) {
        name1 = prompt("What is the first player's name?")
        name2 = prompt("What is the second player's name?")
        name3 = prompt("What is the third player's name?")
        name4 = prompt("What is the forth player's name?")
        name5 = prompt("What is the fifth player's name?")
        number = 5
    }
}

function randomnumber() {
    var randomnumber;
    randomnumber = Math.random() * 3;
    return (Math.floor(randomnumber + 0.2));
}

function randomnumber1() {
    var randomnumber1;
    randomnumber1 = Math.random() * 3;
    return (Math.floor(randomnumber1 + 0.2));
}

function randomnumber2() {
    var randomnumber2;
    randomnumber2 = Math.random() * 3;
    return (Math.floor(randomnumber2 + 0.2));
}

function randomnumber3() {
    var randomnumber3;
    randomnumber3 = Math.random() * 3;
    return (Math.floor(randomnumber3 + 0.2));
}

function randomnumber4() {
    var randomnumber4;
    randomnumber4 = Math.random() * 3;
    return (Math.floor(randomnumber4 + 0.2));
}

function totalscore() {
    var n;
    var p;
    var a;
    var c;
    var e;
    var score = 0;
    var score1 = 0;
    var score2 = 0;
    var score3 = 0;
    var score4 = 0;
    var total;
    var total1;
    var total2;
    var total3;
    var total4;

    total = 0
    total1 = 0
    total2 = 0
    total3 = 0
    total4 = 0

    for (n = 0; n < 10; n = n + 1) // to create total score// 
    {
        number = randomnumber();
        if (number == 0) {
            score = score + 0;
        } else if (number == 2) {
            score = score + 2;
        } else if (number == 3) {
            score = score + 3;
        }
        total = total + score;
    }

    for (p = 0; p < 10; p = p + 1) // to create total score// 
    {
        number1 = randomnumber1();
        if (number1 == 0) {
            score1 = score1 + 0;
        } else if (number1 == 2) {
            score1 = score1 + 2;
        } else if (number1 == 3) {
            score1 = score1 + 3;
        }
        total1 = total1 + score1;
    }
    for (a = 0; a < 10; a = a + 1) // to create total score// 
    {
        number2 = randomnumber2();
        if (number2 == 0) {
            score2 = score2 + 0;
        } else if (number2 == 2) {
            score2 = score2 + 2;
        } else if (number2 == 3) {
            score2 = score2 + 3;
        }
        total2 = total2 + score2;
    }
    for (c = 0; c < 10; c = c + 1) // to create total score// 
    {
        number3 = randomnumber3();
        if (number3 == 0) {
            score3 = score3 + 0;
        } else if (number3 == 2) {
            score3 = score3 + 2;
        } else if (number3 == 3) {
            score3 = score3 + 3;
        }
        total3 = total3 + score3;
    }
    for (e = 0; e < 10; e = e + 1) // to create total score// 
    {
        number4 = randomnumber4();
        if (number4 == 0) {
            score4 = score4 + 0;
        } else if (number4 == 2) {
            score4 = score4 + 2;
        } else if (number4 == 3) {
            score4 = score4 + 3;
        }
        total4 = total4 + score4;
    }
    temp1 = total
    temp2 = total1
    temp3 = total2
    temp4 = total3
    temp5 = total4
}

function leaderboard() {
    if (ask == 3) {
        document.write("Player: " + name1 + " has a score of " + temp1 + "<br>")
        document.write("Player: " + name2 + " has a score of " + temp2 + "<br>")
        document.write("Player: " + name3 + " has a score of " + temp3 + "<BR>")
    } else if (ask == 4) {
        document.write("Player: " + name1 + " has a score of " + temp1 + "<br>")
        document.write("Player: " + name2 + " has a score of " + temp2 + "<br>")
        document.write("Player: " + name3 + " has a score of " + temp3 + "<BR>")
        document.write("Player: " + name4 + " has a score of " + temp4 + "<BR>")
    } else if (ask == 5) {
        document.write("Player: " + name1 + " has a score of " + temp1 + "<br>")
        document.write("Player: " + name2 + " has a score of " + temp2 + "<br>")
        document.write("Player: " + name3 + " has a score of " + temp3 + "<BR>")
        document.write("Player: " + name4 + " has a score of " + temp4 + "<BR>")
        document.write("Player: " + name5 + " has a score of " + temp5 + "<BR>")
    }
    var leader = new Array(5);

    leader[0] = temp1;
    leader[1] = temp2;
    leader[2] = temp3;
    leader[3] = temp4;
    leader[4] = temp5;

    leader.sort(function (a, b) {
        return b - a
    });
    var myContent = '';
    for (var d = 0; d < 5; d++) {
        myContent += "score:  " + leader[d] + "<BR>";
    }
    document.getElementById("leaderboard").innerHTML = myContent;
}
</SCRIPT>
<HEAD>
<BODY>
<BODY BGCOLOUR = "WHITE">
<H2>The Foundation Page </H2>
<HR>
<SCRIPT LANGUAGE = "Javascript"> main() </SCRIPT>
</BODY>
</HTML>

0 个答案:

没有答案