Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typing #21

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Prev Previous commit
typing
  • Loading branch information
yorkcook committed Aug 14, 2024
commit ce67ba21f74f92a88e4f6cf5c07eee4d6dd8b582
89 changes: 45 additions & 44 deletions typing-game/script.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
const word = document.getElementById('word');
const text = document.getElementById('text');
const scoreEl = document.getElementById('score');
const timeEl = document.getElementById('time');
const endgameEl = document.getElementById('end-game-container');
const settingsBtn = document.getElementById('settings-btn');
const settings = document.getElementById('settings');
const settingsForm = document.getElementById('settings-form');
const difficultySelect = document.getElementById('difficulty');
const word = document.getElementById("word");
const text = document.getElementById("text");
const scoreEl = document.getElementById("score");
const timeEl = document.getElementById("time");
const endgameEl = document.getElementById("end-game-container");
const settingsBtn = document.getElementById("settings-btn");
const settings = document.getElementById("settings");
const settingsForm = document.getElementById("settings-form");
const difficultySelect = document.getElementById("difficulty");

// List of words for game
const words = [
'sigh',
'tense',
'airplane',
'ball',
'pies',
'juice',
'warlike',
'bad',
'north',
'dependent',
'steer',
'silver',
'highfalutin',
'superficial',
'quince',
'eight',
'feeble',
'admit',
'drag',
'loving'
"sigh",
"tense",
"airplane",
"ball",
"pies",
"juice",
"warlike",
"bad",
"north",
"dependent",
"steer",
"silver",
"highfalutin",
"superficial",
"quince",
"eight",
"feeble",
"admit",
"drag",
"loving",
];

// Init word
Expand All @@ -43,15 +43,15 @@ let time = 10;

// Set difficulty to value in ls or medium
let difficulty =
localStorage.getItem('difficulty') !== null
? localStorage.getItem('difficulty')
: 'medium';
localStorage.getItem("difficulty") !== null
? localStorage.getItem("difficulty")
: "medium";

// Set difficulty select value
difficultySelect.value =
localStorage.getItem('difficulty') !== null
? localStorage.getItem('difficulty')
: 'medium';
localStorage.getItem("difficulty") !== null
? localStorage.getItem("difficulty")
: "medium";

// Focus on text on start
text.focus();
Expand Down Expand Up @@ -79,7 +79,7 @@ function updateScore() {
// Update time
function updateTime() {
time--;
timeEl.innerHTML = time + 's';
timeEl.innerHTML = time + "s";

if (time === 0) {
clearInterval(timeInterval);
Expand All @@ -96,27 +96,27 @@ function gameOver() {
<button onclick="location.reload()">Reload</button>
`;

endgameEl.style.display = 'flex';
endgameEl.style.display = "flex";
}

addWordToDOM();

// Event listeners

// Typing
text.addEventListener('input', e => {
text.addEventListener("input", (e) => {
const insertedText = e.target.value;

if (insertedText === randomWord) {
addWordToDOM();
updateScore();

// Clear
e.target.value = '';
e.target.value = "";

if (difficulty === 'hard') {
if (difficulty === "hard") {
time += 2;
} else if (difficulty === 'medium') {
} else if (difficulty === "medium") {
time += 3;
} else {
time += 5;
Expand All @@ -127,10 +127,11 @@ text.addEventListener('input', e => {
});

// Settings btn click
settingsBtn.addEventListener('click', () => settings.classList.toggle('hide'));
settingsBtn.addEventListener("click", () => settings.classList.toggle("hide"));

// Settings select
settingsForm.addEventListener('change', e => {
settingsForm.addEventListener("change", (e) => {
difficulty = e.target.value;
localStorage.setItem('difficulty', difficulty);
localStorage.setItem("difficulty", difficulty);
});
//start