From 30fe083af507350eb4053b4c651e0dc5453bf54c Mon Sep 17 00:00:00 2001 From: Anupamit Date: Tue, 28 Apr 2020 22:43:07 +0530 Subject: [PATCH] it has js --- src/index.js | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 src/index.js diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..2002031 --- /dev/null +++ b/src/index.js @@ -0,0 +1,93 @@ +var isStartGame = false, + timerElement = document.getElementById('timer'), + startButtonElement = document.getElementById('startGame'), + allImgElement = document.getElementById('images'), + timer,life,redBlocks; + +function startCounter () { + var counter = 5; + + timerElement.innerHTML = "Timer left " + counter + " sec"; + + timer = setInterval(function() { + + counter--; + timerElement.innerHTML = "Timer left " + counter + " sec"; + + if(counter < 1) { + life--; + allImgElement.children[life] && allImgElement.children[life].setAttribute('src', 'life_over.png'); + + if(life < 1) { + isStartGame = false; + timerElement.innerHTML = "Game Overrrr..."; + startButtonElement.innerHTML = 'Restart Game'; + var removeEle = document.getElementsByClassName("red"); + while(removeEle.length > 0) { + removeEle[0].classList.remove("red"); + } + clearInterval(timer); + } else { + clearInterval(timer); + startCounter(); + } + } + }, 1000); +} + +function getRandomValueArr() { + var arr = [] + while(arr.length < 5){ + var randomnumber = getRandomArbitrary(1,25) + if(arr.indexOf(randomnumber) > -1) continue; + arr[arr.length] = randomnumber; + } + return arr; +} + +function getRandomArbitrary(min, max) { + return Math.floor(Math.random() * (1 + max - min)) + min; +} + +document.getElementById("startGame").addEventListener("click", function () { + if(!isStartGame) { + life = 3; + redBlocks = 5; + isStartGame = true; + startButtonElement.innerHTML = 'Game Running'; + resetImages(); + getRandomValueArr().map(function(value, index, arr) { + document.getElementById(value).className += " red"; + }); + startCounter(); + } +}); + +document.getElementById("gameboard").addEventListener("click", function (event) { + if(isStartGame) { + if(event.target.classList.contains("red")){ + event.target.classList.remove("red"); + redBlocks--; + if(redBlocks < 1) { + isStartGame = false; + clearInterval(timer); + startButtonElement.innerHTML = 'Restart Again'; + timerElement.innerHTML = 'congrates...!!! You Won the Game.'; + alert('congrates...!!! You Won the Game.'); + } + } else { + event.target.classList.add("red"); + redBlocks++; + } + } +}); + +function resetImages() { + var imageHtmlColl = allImgElement.children, + imageHtmlCollLength = imageHtmlColl.length; + + while(imageHtmlCollLength > 0) { + imageHtmlCollLength--; + imageHtmlColl[imageHtmlCollLength].setAttribute('src', 'life.png'); + } +}