|
5 | 5 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
6 | 6 | <meta http-equiv="X-UA-Compatible" content="ie=edge"> |
7 | 7 | <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"> |
9 | 10 | </head> |
10 | 11 | <body class="bg-dark text-white"> |
11 | 12 | <header class="bg-secondary text-center p-3 mb-3"> |
@@ -50,6 +51,104 @@ <h5>Instructions</h5> |
50 | 51 | </div> |
51 | 52 | </div> |
52 | 53 |
|
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> |
54 | 153 | </body> |
55 | 154 | </html> |
0 commit comments