嗨我需要让我的游戏打印并显示更早的顶级结果并告诉我它是高分还是低分。但是现在它只是在我猜2次时才显示lowscore ..它甚至没有显示高分......我的本地存储可能有一些probs,但我不知道是什么.. maby你们中的一些人比我更容易如果你试着运行它就能理解这个问题:)。
enter code here
<html>
<head>
<title>Landmine Terror</title>
</head>
<body>
<h1>Landmine Terror</h1>
<p>Warning: Your browser will self-destruct if you step on the landmine!</p>
<label>Pick a Number from 1-100 to approach the treasure:</label>
<input id="user-input" type="text" />
<b><label id="games-won"></label></b>
<br/>
<input id="submit-button" type="button" value="Guess"; />
<br />
<button onclick= "localStorage.clear(); window.location.reload();">Reset highscore></button>
<p id="lista"></p>
<br/>
<p id="highscore"></p>
<br/>
<input id="reset-button" type="button" value="New game" />
<br/>
<b><label id="response"></label></b>
<br/>
<p id="gissningar"></p>
<br/>
<div id="user_progress"></div>
<link rel="stylesheet" href="css/themes/default/jquery.mobile-1.4.4.min.css" />
<script src="js/jquery.js"></script>
<script src="js/jquery.mobile-1.4.4.min.js"></script>
<script src="js/apotek.js"></script>
<script type="text/javascript">
var generateRandom = function() {
return parseInt(Math.random() * 100, 10) + 1;
}
var higher = 'Try a higher number!';
var lower = 'Try a lower number!';
var correct = 'Congrats, you got the right number!';
var promptMessage = 'Enter a number between 1 and 100';
var newGameStarted = 'A new game has started!';
var checkNumber = function(userNumber, randomNumber) {
//console.log(randomNumber)
if (userNumber < 1 || userNumber > 100 ) {
return promptMessage;
}
else if (userNumber > randomNumber) {
return lower;
}
else if (userNumber < randomNumber) {
return higher;
}
else {
gamesWon++;
return (correct + number.length);
}
}
function gissningarna(){
var gissningar = document.getElementById("gissningar").innerHTML =
"Gissningar:" + (number.length);
}
var randomNumber = generateRandom();
var gamesWon = 0;
$('#games-won').text(gamesWon + ' games won');
window.onload= function () {
printList();
}
var number=[]
function sortering()
{
var x=document.getElementById("lista");
number.push(document.getElementById("user-input").value);
number.sort(function(a, b){return a-b});
x.innerHTML= "Gissade nummer: " + (number.join(', '));
}
var answer = function() {
$('#response').show();
var userNumber = $('#user-input').val();
var checkResult = checkNumber(userNumber, randomNumber);
$('#user-input').focus(function(){
$('#user-input').val('');
});
$('#response').text(checkResult)
$('#games-won').text(gamesWon + ' games won')
};
window.onload= function () {
printList();
}
function highscore() {
var locallangd = localStorage.length;
//console.log(localStorage.length);
var sorteringArray = new Array();
for (var ind = 0; ind < locallangd; ind++){
var idNyckel = localStorage.key(ind);
var arrayValues = localStorage.getItem(idNyckel);
sorteringArray.push(arrayValues);
}
sorteringArray.sort(function(a, b){return a-b});
if (number.length < sorteringArray[0]){
alert("rekord");
}else{
if (number.length > sorteringArray[locallangd -1]){
alert("bottenrekord");
}
}
}
function sparaData()
{
var newDate = new Date();
var idNyckel = newDate.getTime();
var arrayRecord = new Array();
var userNumber = document.getElementById("user-input").value;
if (userNumber == randomNumber) {
arrayRecord.push(number.length);
}
try {
highscore();
localStorage.setItem(idNyckel, arrayRecord);
} catch (e) {
if (e == QUOTA_EXCEEDED_ERR) {
alert("Quota exceeded!");
}
}
printList();
return false;
}
function printList(){
var strResult ="";
var antalPoster = localStorage.length;
for (var i =0; i<antalPoster; i++){
var idNyckel = localStorage.key(i);
var arrayValues = localStorage.getItem(idNyckel);
var values = arrayValues.split("<li>");
var userName = values[0];
if (userName === "") {
}else{
strResult += userName + "<br></li>";
}
document.getElementById("highscore").innerHTML = strResult;
}
}
$('#submit-button').on('click', answer);
$('#submit-button').on('click', sortering);
$('#submit-button').on('click', gissningarna);
$('#submit-button').on('click', sparaData);
$('#reset-button').on('click', function (e) {
randomNumber = generateRandom();
$('#user-input').val('');
$('#response').text(newGameStarted);
$('#lista').text("");
$('#gissningar').text("");
number = []
});
</script>
答案 0 :(得分:0)
所以你试图存储一个数组。假设数组被称为分数。你可以用:
写出来localStorage.setItem("Scores", JSON.stringify(scores));
并阅读:
scores = JSON.parse(localStorage.getItem("Scores"));
更好的是,使用http://rhaboo.org