I'm pretty new to JavaScript and I've run into an issue I can't find a solution to. I'm developing a game for my APCS final project and I'm trying to add a leaderboard to it. To find the top players, I'm putting all the high scores in an array, sorting it from greatest to least, and then searching for 5 usernames whos scores match the first 5 numbers in the array. I'm using AppLab to create it and AppLab has a built-in database feature, and so that's what "readRecords is for. My issue though is that when I populate the array with a for loop, the populations don't exist outside of the function even though the array variable was created outside of the function, here's the code...
function leaderGrabEasy() {
var leaderScores = [];
var leaders = [];
readRecords("userData",{},function(records) {
for (var i = 0; i < records.length; i++) {
leaderScores.push(records[i].E_highscore);
}
leaderScores.sort(function(a, b){return b-a});
});
readRecords("userData",{E_highscore:leaderScores[0]},function(records) {
for (var i = 0; i < records.length; i++) {
leaders.push(records[i].username);
}
console.log(leaders);
});
}
The issue occurs when I try to read the database column "E_highscores" for whatever is in "leaderScores[0]"
readRecords("userData",{E_highscore:leaderScores[0]},function(records) {
But because the array is empty outside of the first function, that spot in the array is empty. Thanks in advance for any help!
-Indoors