localstorageに保存されている高得点を作成しようとしています。アプリケーションは、その最後に5つのスコアを保存して表示するクイズアプリケーションです(完成しているかどうかに関係なく)。ここでの問題は、in HTMLファイルで作成したpタグが、 jsファイルで作成したハイスコアリストの値。ここにコードがあります:Pタグが更新されない
次に、私は5人のプレイヤーがいるまで2つのpタグを持つdivクラスプレーヤーをコピーします。
javascriptのコード:事前に
< function SaveToDataBase(){
//Checks if they completed the quizz, and check if there time is
//enough to be top5, if its enough, then it will run newEntry
var boolean = false;
var Trigger = "score";
HighScoreList.forEach(function(entry){
if(entry.hasOwnProperty(Trigger)){
var value = entry[Trigger];
/*you look in your scores, and his total time is less than theres, then he is
added to the list*/
if(thetotaltimer < value){
boolean = true;
}
}
});
if(boolean){
newEntry();
}
}
function newEntry(){
//inserts on index
HighScoreList.splice(5, 0, {name: currentPlayer, score: thetotaltimer });
//sort scores
HighScoreList.sort(function(a,b){
return a.score - b.score;
});
//starts at index 5, then takes away the index right after it (number 6).
HighScoreList.splice(5,1);
localStorage.setItem("SavedHighListObjectz", JSON.stringify(HighScoreList));
document.getElementById("Player1").innerText = JSON.parse(HighScoreList[0].name);
document.getElementById("Score1").innerText = JSON.parse(HighScoreList[0].score);
document.getElementById("Player2").innerText = JSON.parse(HighScoreList[1].name);
document.getElementById("Score2").innerText = JSON.parse(HighScoreList[1].score);
document.getElementById("Player3").innerText = JSON.parse(HighScoreList[2].name);
document.getElementById("Score3").innerText = JSON.parse(HighScoreList[2].score);
document.getElementById("Player4").innerText = JSON.parse(HighScoreList[3].name);
document.getElementById("Score4").innerText = JSON.parse(HighScoreList[3].score);
document.getElementById("Player5").innerText = JSON.parse(HighScoreList[4].name);
document.getElementById("Score5").innerText = JSON.parse(HighScoreList[4].score);
}
function init(){
//this if-statement runs if there is no highscore list already saved
if(localStorage.getItem("SavedHighListObjectz") == undefined) {
HighScoreList = [
{'name': "Sam", 'score': 300000},
{'name': "Markus", 'score': 554000},
{'name': "Cody", 'score': 2210000},
{'name': "David", 'score': 39000000},
{'name': "Jackson", 'score': 55500000}
];
localStorage.setItem("SavedHighList", JSON.stringify(HighScoreList));
}else{
//or else we load this
HighScoreList = JSON.parse(localStorage.getItem("SavedHighListObjectz"))
}
}
init();
questions();>
感謝。
あなたはjsFiddleで投稿できますか? 私はあなたの問題がattritubeのスペースだと思っています。これは次のようになります: 'id =" EndSection "' instand of: 'id =" EndSection "' – itzikb
まず、すべての解析コードを削除します。これはjavaScriptオブジェクトなので、解析する必要はありません。次に、 '<>'をクリックし、[mcve] – mplungjan
を作成します。変数 "boolean"の命名を避けることを目標にしています。他の理由の中には、それが何をしているのかは記述されていない。 – seemly