javascript - Array.sort correction -
i wondering if doing array sort correctly because i'm doing leaderboard display score highest lowest. "temp 1-5" variable has value's in them , wondering there mistake making.
<html> <!foundation page 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 playing") if (ask == 3) { name1 = prompt("what first player's name?") name2 = prompt("what second player's name?") name3 = prompt("what third player's name?") number = 3 } if (ask == 4) { name1 = prompt("what first player's name?") name2 = prompt("what second player's name?") name3 = prompt("what third player's name?") name4 = prompt("what forth player's name?") number = 4 } if (ask == 5) { name1 = prompt("what first player's name?") name2 = prompt("what second player's name?") name3 = prompt("what third player's name?") name4 = prompt("what forth player's name?") name5 = prompt("what 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 (n = 0; n < 10; n = n + 1) // 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; } (p = 0; p < 10; p = p + 1) // 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; } (a = 0; < 10; = + 1) // 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; } (c = 0; c < 10; c = c + 1) // 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; } (e = 0; e < 10; e = e + 1) // 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 score of " + temp1 + "<br>") document.write("player: " + name2 + " has score of " + temp2 + "<br>") document.write("player: " + name3 + " has score of " + temp3 + "<br>") } else if (ask == 4) { document.write("player: " + name1 + " has score of " + temp1 + "<br>") document.write("player: " + name2 + " has score of " + temp2 + "<br>") document.write("player: " + name3 + " has score of " + temp3 + "<br>") document.write("player: " + name4 + " has score of " + temp4 + "<br>") } else if (ask == 5) { document.write("player: " + name1 + " has score of " + temp1 + "<br>") document.write("player: " + name2 + " has score of " + temp2 + "<br>") document.write("player: " + name3 + " has score of " + temp3 + "<br>") document.write("player: " + name4 + " has score of " + temp4 + "<br>") document.write("player: " + name5 + " has 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 - }); var mycontent = ''; (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>
Comments
Post a Comment