Skip to content

Commit d539c56

Browse files
authored
Update index.html
1 parent 7b8a300 commit d539c56

File tree

1 file changed

+101
-2
lines changed

1 file changed

+101
-2
lines changed

index.html

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<meta http-equiv="X-UA-Compatible" content="ie=edge">
77
<title>Word Contest</title>
8-
<link rel="stylesheet" href="css/bootstrap.min.css">
8+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
9+
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
910
</head>
1011
<body class="bg-dark text-white">
1112
<header class="bg-secondary text-center p-3 mb-3">
@@ -50,6 +51,104 @@ <h5>Instructions</h5>
5051
</div>
5152
</div>
5253

53-
<script src="js/app.js"></script>
54+
<script>
55+
window.addEventListener('load', init);
56+
//Globals
57+
58+
//Available levels
59+
const levels = {
60+
easy: 5,
61+
mediaum: 2,
62+
hard: 2
63+
}
64+
//To change level
65+
const currentLevel = levels.hard;
66+
67+
let time = currentLevel,
68+
score = 0,
69+
isPlaying;
70+
71+
//DOM Elements
72+
const wordInput = document.querySelector('#word-input');
73+
const currentWord = document.querySelector('#current-word');
74+
const scoreDisplay = document.querySelector('#score');
75+
const timeDisplay = document.querySelector('#time');
76+
const message = document.querySelector('#message');
77+
const seconds = document.querySelector('#seconds');
78+
79+
const words = ['words', 'stan', 'books', 'physics'];
80+
81+
//Initialize game
82+
function init(){
83+
//show number of seconds in UI
84+
seconds.innerHTML = currentLevel;
85+
//Load word from array
86+
showWord(words);
87+
//start matching on word input
88+
wordInput.addEventListener('input', startMatch);
89+
//call countdown every second
90+
setInterval(countDown, 1000);
91+
//check the game status
92+
setInterval(checkStatus, 50);
93+
}
94+
95+
//start match
96+
function startMatch(){
97+
if(matchWords()){
98+
isPlaying = true;
99+
time = currentLevel + 1;
100+
showWord(words);
101+
wordInput.value = '';
102+
score++;
103+
}
104+
105+
//If score is -1 display 0
106+
if(score === -1){
107+
scoreDisplay.innerHTML = 0;
108+
} else {
109+
scoreDisplay.innerHTML = score;
110+
}
111+
}
112+
113+
//Match the current word to word input
114+
function matchWords(){
115+
if (wordInput.value === currentWord.innerHTML) {
116+
message.innerHTML = 'Correct!!!';
117+
return true;
118+
} else {
119+
message.innerHTML = '';
120+
return false;
121+
}
122+
}
123+
124+
//Pick and show random word
125+
function showWord(words){
126+
//
127+
const randIndex = Math.floor(Math.random() * words.length);
128+
currentWord.innerHTML = words[randIndex];
129+
}
130+
131+
//COunt Down
132+
function countDown(){
133+
//Make sure time is not run out
134+
if(time > 0){
135+
//Decrease the time
136+
time--;
137+
} else if(time === 0){
138+
//Game is over
139+
isPlaying = false;
140+
}
141+
//Show time
142+
timeDisplay.innerHTML = time;
143+
}
144+
145+
//check game status
146+
function checkStatus(){
147+
if(!isPlaying && time === 0){
148+
message.innerHTML = 'Game Over!!!';
149+
score = -1;
150+
}
151+
}
152+
</script>
54153
</body>
55154
</html>

0 commit comments

Comments
 (0)