-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript2.js
More file actions
72 lines (63 loc) · 2.06 KB
/
script2.js
File metadata and controls
72 lines (63 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var containerElement = document.getElementById("container");
var hsTable = document.getElementById("tableBody");
var goback = document.getElementById("gobackButton");
var hsButton = document.getElementById("hsButton");
var highscoreArray = [];
function init() {
populateTable();
};
function populateTable() {
var storedHighscore = JSON.parse(localStorage.getItem("highscoreArray"));
if(storedHighscore !== null) {
highscoreArray = storedHighscore;
//highscoreArray = sortScores(highscoreArray);
//console.log(storedHighscore);
highscoreArray = sortScores (highscoreArray);
} else {
return;
};
for (var i = 0; i<highscoreArray.length; i++) {
var row = hsTable.insertRow(i);
var cell0 = row.insertCell(0);
var cell1 = row.insertCell(1);
var cell2 = row.insertCell(2);
//console.log(highscoreArray[i].initial + " & " + highscoreArray[i].score);
cell0.innerHTML = i+1;
cell1.innerHTML = highscoreArray[i].initial;
cell2.innerHTML = highscoreArray[i].score;
};
};
function sortScores(hsArray) {
var high;
var newArray = [];
while (hsArray.length >= 1) {
high = 0;
for (var n = 1; n<hsArray.length; n++) {
if (hsArray[n].score > hsArray[high].score) {
high = n;
//excahnge the objects
//console.log("high index: " + high);
}
}
var highscoreObj = {
initial: hsArray[high].initial,
score: hsArray[high].score
};
//console.log("high "+ hsArray[high].initial + " , " + hsArray[high].score);
hsArray.splice(high, 1);
newArray.push(highscoreObj);
}
//console.log("new Array");
console.log (newArray);
return newArray;
};
goback.addEventListener("click", function() {
event.preventDefault();
window.location.href = "./index.html";
});
hsButton.addEventListener("click", function() {
console.log("in hsButton event");
localStorage.clear();
location.reload();
});
init();