diff options
| author | Saksham Mittal <gotlouemail@gmail.com> | 2022-04-02 19:15:26 +0530 |
|---|---|---|
| committer | Saksham Mittal <gotlouemail@gmail.com> | 2022-04-02 19:15:26 +0530 |
| commit | 991fb1191a2680bc0827a85407792e95cc36356f (patch) | |
| tree | 45a31dea62cbc5577c4ca40583372f8e30cb4d02 | |
| parent | fda6bdf1ebf59f1f86f122186ba726c1e83881f7 (diff) | |
| download | purple-991fb1191a2680bc0827a85407792e95cc36356f.tar.gz | |
Add attempt counter and disable further play if won
| -rw-r--r-- | index.html | 4 | ||||
| -rw-r--r-- | script.js | 32 |
2 files changed, 29 insertions, 7 deletions
diff --git a/index.html b/index.html index 5438389..39427fa 100644 --- a/index.html +++ b/index.html @@ -8,9 +8,11 @@ </head> <body> <h1>Guessing Game</h1> - <form onsubmit="hello(event)"> + <form onsubmit="checkAnswer(event)"> <input type="text" placeholder="Guess" autofocus> </form> <p id="result"></p> + <p id="win"></p> + <p id="attempt"></p> </body> </html> diff --git a/script.js b/script.js index 5f3ad23..6854ba5 100644 --- a/script.js +++ b/script.js @@ -2,13 +2,33 @@ let answer = "hello"; let form = document.querySelector('form'); let input = document.querySelector('input'); +let name = document.querySelector('#result'); +let win = document.querySelector('#win'); +let attempt = document.querySelector('#attempt'); -function hello(event) { +let exit = false; +let tries = 0; + +function checkAnswer(event) { event.preventDefault(); - let name = document.querySelector('#result'); - if (input.value.length == 5) { - name.innerHTML = `${input.value}`; - } else { - name.innerHTML = ""; + if (!exit) { + if (input.value.length == 5) { + tries++; + name.innerHTML = `${input.value}`; + if (input.value == answer) { + win.innerHTML = "You won!"; + exit = true; + } else { + win.innerHTML = "Not quite right!"; + } + if (tries == 1) { + attempt.innerHTML = tries + " attempt taken"; + } else { + attempt.innerHTML = tries + " attempts taken"; + } + + } else { + name.innerHTML = ""; + } } } |
