added stats func Итъ

This commit is contained in:
Alexander Popov 2017-02-26 01:29:28 +03:00
parent bc508119d7
commit 026a55c558
2 changed files with 31 additions and 14 deletions

View File

@ -2,17 +2,33 @@ var words = ['google', 'speed', 'window', 'horizon'];
var lives = 6; var lives = 6;
var gameWord = words[Math.floor(Math.random() * words.length)].toUpperCase(); var gameWord = words[Math.floor(Math.random() * words.length)].toUpperCase();
var gameAnswered = new Array(gameWord.length + 1).join('-') var gameAnswered = new Array(gameWord.length + 1).join('-');
window.onload = function() window.onload = function()
{ {
console.log(gameWord) console.log(gameWord) // for DEBUG
document.getElementById("WORD").innerHTML = gameAnswered; document.getElementById("WORD").innerHTML = gameAnswered;
document.getElementById("lives").innerHTML = 'Lives ' + lives; document.getElementById("lives").innerHTML = 'Lives ' + lives;
// init storage
var storageNames = ['gamesWon', 'wordsAnswered', 'gamesFail'];
storageNames.forEach(function(item)
{
if(null == localStorage.getItem(item))
localStorage.setItem(item, 0);
})
} }
function wrong() function wrong()
{ {
if (lives <= 0)
{
var gamesFail = localStorage.getItem('gamesFail');
localStorage.setItem('gamesFail', parseInt(gamesFail) + 1);
alert('You dead');
return;
}
lives -= 1; lives -= 1;
document.getElementById("lives").innerHTML = 'Lives ' + lives; document.getElementById("lives").innerHTML = 'Lives ' + lives;
} }
@ -22,19 +38,9 @@ function offChar(char)
var charButton = document.getElementById(char); var charButton = document.getElementById(char);
charButton.disabled = true; charButton.disabled = true;
if (gameAnswered.split('').indexOf('-') == -1)
{
alert('You Win');
return;
}
if (lives == 0) {
alert('You dead');
return;
}
var indices = []; var indices = [];
var idx = gameWord.split('').indexOf(char) var idx = gameWord.split('').indexOf(char)
if (idx == -1) if (idx == -1)
{ {
wrong(); wrong();
@ -46,7 +52,6 @@ function offChar(char)
indices.push(idx); indices.push(idx);
idx = gameWord.split('').indexOf(char, idx + 1); idx = gameWord.split('').indexOf(char, idx + 1);
} }
console.log(indices)
indices.forEach(function(item, indices) indices.forEach(function(item, indices)
{ {
@ -54,5 +59,15 @@ function offChar(char)
wordArray[item] = char; wordArray[item] = char;
gameAnswered = wordArray.join(''); gameAnswered = wordArray.join('');
document.getElementById("WORD").innerHTML = gameAnswered; document.getElementById("WORD").innerHTML = gameAnswered;
var wordsAnswered = localStorage.getItem('wordsAnswered');
localStorage.setItem('wordsAnswered', parseInt(wordsAnswered) + 1);
}) })
if (gameAnswered.split('').indexOf('-') == -1)
{
var gamesWon = localStorage.getItem('gamesWon');
localStorage.setItem('gamesWon', parseInt(gamesWon) + 1);
alert('You Win');
return;
}
} }

View File

@ -1,3 +1,5 @@
// http://paletton.com/#uid=13w0u0kllllaFw0g0qFqFg0w0aF
* { * {
margin: 0; padding: 0; outline: 0; margin: 0; padding: 0; outline: 0;
} }