From 57acc544880364f9237ec8c9d6bdfe3dae486429 Mon Sep 17 00:00:00 2001 From: Brandon Dickson Date: Sat, 14 Nov 2020 15:14:40 +0000 Subject: [PATCH] added more logic for when a number is correct or too high/low --- script.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/script.js b/script.js index 002873b..094a51f 100644 --- a/script.js +++ b/script.js @@ -1,11 +1,35 @@ 'use strict'; +const secretNumber = Math.trunc(Math.random() * 20) + 1; +let score = 20; +document.querySelector('.number').textContent = secretNumber; + document.querySelector('.check').addEventListener('click', function () { const guess = Number(document.querySelector('.guess').value); - console.log(typeof guess); if (!guess) { document.querySelector('.message').textContent = 'You need to enter a number! 😄'; + } else if (guess === secretNumber) { + document.querySelector('.message').textContent = + 'You entered the correct number! 🎉'; + } else if (guess > secretNumber) { + if (score > 1) { + document.querySelector('.message').textContent = 'Too high, try again!'; + score--; + document.querySelector('.score').textContent = score; + } else { + document.querySelector('.message').textContent = 'You lost the game!'; + document.querySelector('.score').textContent = '0'; + } + } else if (guess < secretNumber) { + if (score > 1) { + document.querySelector('.message').textContent = 'Too low, try again!'; + score--; + document.querySelector('.score').textContent = score; + } else { + document.querySelector('.message').textContent = 'You lost the game!'; + document.querySelector('.score').textContent = '0'; + } } });