I'm in the process of developing a basic video game leader board that will arrange user scores against preset scores in descending order.
<html>
<!Foundation Page for building our Javascript programs>
<head>
<title>The Foundation Page </title>
<script type="text/javascript">
function leaderboard()
{
var leaderboardarray = new Array (5);
var n, temp;
ok=false;
var score
var rank
score = 150
leaderboardarray[1] = 50;
leaderboardarray[2] = 60;
leaderboardarray[3] = 180;
leaderboardarray[4] = 120;
leaderboardarray[5] = score;
while(!ok)
{
ok = true
for (n=1; n<=5; n=n+1)
{
if (leaderboardarray [n]<leaderboardarray [n-1])
{
leaderboardarray [n] = leaderboardarray [n-1];
ok = false;
}
}
}
for (n=5; n>=1; n=n-1)
document.write (leaderboardarray [n] + "<br>");
}
</script>
</head>head>
<body BGCOLOR="WHITE">
<h2>The Foundation Page </h2>
<hr>
<script LANGUAGE="Javascript"> leaderboard() </script>
</body>
</html>
The program is showing the arrays correctly, but I am facing an issue where it does not output values from highest to lowest. Instead, when a higher value is inserted after another, it duplicates the same value. If anyone has suggestions on how to tackle this problem, please advise. I'm still relatively new to programming, so apologies if I'm missing something obvious. Thank you!