2017-01-01 6 views
0

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();> 

感謝。

+0

あなたはjsFiddleで投稿できますか? 私はあなたの問題がattritubeのスペースだと思っています。これは次のようになります: 'id =" EndSection "' instand of: 'id =" EndSection "' – itzikb

+0

まず、すべての解析コードを削除します。これはjavaScriptオブジェクトなので、解析する必要はありません。次に、 '<>'をクリックし、[mcve] – mplungjan

+2

を作成します。変数 "boolean"の命名を避けることを目標にしています。他の理由の中には、それが何をしているのかは記述されていない。 – seemly

答えて

0

様々な小さな問題は、作業からあなたのコードを停止されています

1. あなたの名前とスコアは文字列だけである - 彼らは「予期しないトークン」例外が発生しなければならないJSON.parseしようとしています。あなたはそれを見ていませんか? 2.

あなたは(、Chromeで、F12キーを押しをし、「コンソール」をクリックしてください)JavaScriptの例外を見ることができていることを確認してくださいlocalStorage.setItem(「SavedHighList」このようなミスを避けるためにlocalStorage.setItem(「SavedHighListObjectz」。べきです、変数としてあなたの鍵を保存します?

var storageKey="SavedHighListObjectz"; 
... 
localStorage.getItem(storageKey) 
... 

3. あなたが唯一のプレーヤーのためのpタグを持っているこれは(あなたが#1、#2を固定した後)あなたの例のコードは明白であってもよいが、他の4つのトップスコアのhtmlを追加するまでdocument.getElementById("Player2").innerTextに爆弾を送るでしょう

+1

JavaScriptの自動型変換後、 'null == undefined'がtrueです。厳密な等式を使用したテストでは、すべての疑いが取り除かれます。 'if(localStorage.getItem(" SavedHighListObjectz ")=== null)' – traktor53

+0

ありがとう@ Traktor53あなたは正しいですか? –

関連する問題