This is what I made today and I'm keen for a review of my code to see what things I can do better or smarter. I don't know if its relevant but it took 2 hours to code.
Is it smart to use OOP for a project like this?
I have included all the code in the snippet.
var inGame = false;
var currentWord, amountOfErrors, goodAnswers, previousWord;
var timer, time;
$(document).ready(function() {
//Check if enter is pressed
$(document).keypress(function(e) {
if(e.which == 13) {
enterWord();
}
});
$("#startBtn").click(function () {
//Start the game when user clicks
startGame();
$("#startBtn").hide();
})
});
function startGame() {
//Initialize start
inGame = true;
document.getElementById("wordField").focus();
//Reset all variables
amountOfErrors = 0;
corrections = 0;
goodAnswers = 0;
$("#currentWord").html("");
$("#errors").html("");
$("#wpm").html("");
pickWord();
//start the timer
$("#timer").show();
startTimer();
}
function stopGame () {
//Stop the timer
clearInterval(timer);
$("#timer").hide();
$("#startBtn").show();
$("#wordField").val("");
//Set game to stopped
inGame = false;
//Show player his results
$("#currentWord").html("<strong>Done...</strong>");
$("#errors").html('<strong>Amount of errors: '+amountOfErrors+'</strong>');
$("#wpm").html('<strong>Words per minute: '+goodAnswers+'</strong>');
}
function startTimer() {
//Reset timer back to 60 seconds
time = 60;
//Every 100ms the timer gets updated
timer = setInterval(function () {
if (time <= 0) {
stopGame();
}
time = time +- 0.1;
$("#timer").html("<strong>"+time.toFixed(2)+"</strong>")
},100);
}
function enterWord () {
//If player is not in game
if (inGame == false) {
//Do nothing
} else if (inGame == true) { //Is in game
checkWord();
}
}
function checkWord () {
var enteredWord = $("#wordField").val();
//Check if the player typed the correct string
if (enteredWord == currentWord) {
//if true
fadeColor("wordField", "backgroundColor", "rgb(43,234,81)");
$("#wordField").val("");
pickWord();
goodAnswers++;
previousWord = currentWord;
} else {
//if false
amountOfErrors++;
fadeColor("wordField", "backgroundColor", "rgb(234,43,43)");
}
}
function pickWord () {
//Checking how many words ther are in the array
var aantalWoorden = woorden.length;
//Amount of words + 1
aantalWoorden = aantalWoorden + 1;
//Pick a random number between the amount of words and 0
var randomWord = Math.floor((Math.random() * aantalWoorden) + 0);
//Select a random word from the array
currentWord = woorden[randomWord];
//Check if its not the same as previous word
if (currentWord == previousWord || currentWord == undefined) {
pickWord();
} else {
//Show the word
$("#currentWord").html("<strong>"+currentWord+"</strong>");
}
}
var woorden = ["value" , "meanwhile", "finally" , "base" , "spikes" ,"creature", "comforts" , "advanced" , "the snout" , "opposite", "the spoon" , "the dust cloud" , "scrape" , "natural" , "significantly" , "the hedgehog" , "create" , "offset" , "enlarge" , "treacherous" , "the direction" , "the culture" , "the territory", "the timetable"];
function fadeColor(id, property, color) {
var oProperty = $('#'+id+'').css(property);
$('#'+id+'').css(property, color);
setTimeout(function() {
$('#'+id+'').css(property, oProperty);
},300);
}
@import url('https://fonts.googleapis.com/css?family=IBM+Plex+Mono');
body {
margin: 0;
background-color: rgb(234,43,43);
color: white;
font-family: IBM Plex Mono;
}
.centerDiv {
display: flex;
flex-direction: column;
margin: auto;
width: 100%;
height: 100%;
align-items: center;
text-align: center;
}
.headerDiv {
display: flex;
flex-direction: column;
margin: auto;
width: 100%;
height: 100%;
align-items: center;
text-align: center;
margin: 10px;
margin-bottom: 10%;
}
.header {
font-size: 25px;
}
.textfield {
padding: 10px;
border-radius: 3px;
background-color: white;
color: black;
text-decoration: none;
border: none;
margin: 10px;
font-size: 25px;
transition: all 0.15s ease;
}
.textToType {
font-size: 25px;
margin: 10px;
}
.button {
padding: 10px;
border-radius: 3px;
background-color: white;
color: rgb(234,43,43);
text-decoration: none;
border: none;
margin: 10px;
font-size: 25px;
transition: all 0.15s ease;
}
.button:hover {
cursor: pointer;
transform: scale(1.05);
}
<!DOCTYPE html>
<html>
<head>
<title>Typing King</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/style.css">
<link rel="icon" href="css/favicon.png" type="image/png">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="js/typing.js"></script>
</head>
<body>
<div class="centerDiv">
<div class="headerDiv">
<a class="header">Typing King</a>
<a>Are you the next typing king?</a>
</div>
<div class="centerDiv">
<a class="textToType" id="currentWord"></a>
<input class="textfield" id="wordField" placeholder="Your input"/>
<a class="button" id="startBtn">Start</a>
<a id="timer"></a>
<a id="wpm"></a>
<a id="corrections"></a>
<a id="errors"></a>
</div>
</div>
</body>
</html>