Skip to content

Commit

Permalink
added more logic for when a number is correct or too high/low
Browse files Browse the repository at this point in the history
  • Loading branch information
brandond98 committed Nov 14, 2020
1 parent 52e302f commit 57acc54
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion script.js
Original file line number Diff line number Diff line change
@@ -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';
}
}
});

0 comments on commit 57acc54

Please sign in to comment.