I wanted to share my first experience with JavaScript. I've been taking JS courses for the past 2 months and finally got the first chance to code a small guess-my-number project.
(Everything is completely coded by me and took 4 hours in total. I wanted to test my skills.)
Am I on the right path? Is the code clean enough and what can I possibly improve?
Code is also available as a Codepen Link.
'use strict';
// Generating a random value and storing it in a variable
let secretNumber = Math.floor(Math.random() * 10 + 1);
let score = 20;
let highScore = 0;
document.querySelector('.check').addEventListener('click', function () {
const guess = Number(document.querySelector('.guess').value);
document.querySelector('.score').textContent = score;
// If there is no value in the input
if (!guess) {
document.querySelector('.message').textContent = 'Please insert a number';
} else if (score < 1) {
document.querySelector('.message').textContent = 'You lost';
document.querySelector('body').style.backgroundColor = '#FF0000';
}
// If the guess is right
else if (guess === secretNumber) {
document.querySelector('.message').textContent = 'You Won!!';
document.querySelector('body').style.backgroundColor = '#008000';
document.querySelector('.qmark').textContent = secretNumber;
document.querySelector('.highScore').textContent = score;
}
// If the guess is higher than the secret number
else if (guess > secretNumber) {
document.querySelector('.message').textContent = 'Too high';
score--;
}
// If the guess is lower than the secret number
else if (guess < secretNumber) {
document.querySelector('.message').textContent = 'Too Low!';
score--;
}
// Setting highScore
else if (score > highScore) {
highScore = score;
document.querySelector('.highScore').textContent = highScore;
}
});
// Reset the game
document.querySelector('.reset').addEventListener('click', function () {
score = 20;
secretNumber = Math.floor(Math.random() * 10 + 1);
document.querySelector('.message').textContent = 'Start Guessing...';
document.querySelector('.score').textContent = score;
document.querySelector('.qmark').textContent = '?';
document.querySelector('.guess').value = '';
document.querySelector('body').style.backgroundColor = '#043030';
});
* {
font-family: 'Jura', sans-serif;
color: azure;
margin: 0;
padding: 0;
width: auto;
}
body {
background-color: #043030;
height: 999px;
}
.boxContainer {
display: flexbox;
height: 999px;
}
.boxup {
border-bottom: 6px white solid;
flex-direction: column;
height: 400px;
}
.reset {
font-size: xx-large;
float: left;
margin: 30px;
padding: 10px;
border-radius: 10px;
background-color: rgb(1, 7, 7);
}
.numberbtwn {
float: right;
margin: 50px;
font-size: larger;
}
.title {
position: absolute;
top: 15%;
left: 35%;
font-size: 60px;
}
.qmark {
background-color: rgb(1, 7, 7);
position: absolute;
width: 200px;
height: 150px;
left: 45%;
top: 33%;
text-align: center;
font-size: 110px;
}
.boxdown {
display: flex;
height: 590px;
}
.left {
flex-direction: row;
width: 50%;
height: 100%;
}
input {
margin-left: 150px;
margin-top: 150px;
margin-right: 368px;
width: 300px;
height: 150px;
background-color: rgb(4, 48, 48);
border: white 7px solid;
top: 20%;
font-size: 150px;
text-align: center;
}
/* Hiding the up and down arrow from the browsers */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
.check {
font-size: 40px;
padding: 30px;
margin: 50px;
margin-left: 200px;
margin-right: 442px;
border-radius: 20px;
background-color: rgb(1, 7, 7);
}
.right {
flex-direction: row;
width: 50%;
height: 100%;
}
.message {
margin: 140px;
font-size: 50px;
}
p {
margin: 100px;
font-size: 30px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link rel="stylesheet" href="stylesheet.css" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Jura&display=swap"
rel="stylesheet"
/>
</head>
<body>
<div class="boxContainer">
<div class="boxup">
<button class="reset">RESET</button>
<p class="numberbtwn">Between 1-20</p>
<h1 class="title">Guess My Number!</h1>
<div class="qmark">?</div>
</div>
<div class="boxdown">
<div class="left">
<input class="guess" type="number" />
<br />
<button class="check">Check!</button>
</div>
<div class="right">
<p class="message">Start Guessing...</p>
<p>🔥🔥 Score:<span class="score"> </span></p>
<p>🏆🏆 Highscore:<span class="highScore"> </span></p>
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>