-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
120 lines (95 loc) · 3.43 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const startGameEl = document.getElementById('start-game')
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 startGameCon = document.getElementById('start-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'];
let randomWord;
let score = 0;
let time;
let difficulty = localStorage.getItem('difficulty') !== null ? localStorage.getItem('difficulty') : 'medium';
difficultySelect.value = difficulty;
let maxScore = localStorage.getItem('maxScore') !== null ? localStorage.getItem('maxScore') : 0;
function startGame() {
time = 10;
startGameCon.style.display = 'none';
//focus on text/input element on reload
text.focus();
}
//start counting down
const timeInterval = setInterval(updateTime, 1000);
function addWordToDOM() {
// randomWord = words[Math.floor(Math.random() * words.length)];
const options = {
method: 'GET',
headers: {
'X-RapidAPI-Key': '8a3ae32fdamsh3f69042bdb0c81ap103d85jsnb58c0de7942e',
'X-RapidAPI-Host': 'random-word-generator2.p.rapidapi.com'
}
};
fetch('https://random-word-generator2.p.rapidapi.com/word.php?generator=words&api_key=5w36eV0FZJu9QIPlpR18', options)
.then(response => response.json())
.then(data => {
word.innerHTML = data.word;
randomWord = word.innerHTML;
});
}
addWordToDOM();
function updateScore() {
score++;
scoreEl.innerHTML = score;
}
function updateTime() {
time--;
timeEl.innerHTML = time + 's';
if (time === 0) {
clearInterval(timeInterval);
gameOver();
}
}
function gameOver() {
if (score > maxScore) {
localStorage.setItem('maxScore', score);
}
endgameEl.innerHTML = `
<h1>Time ran out <i class="fa-solid fa-hourglass-end"></i></h1>
<h3>Your final score: ${score}</h3>
<h3>Your highest score: ${score > maxScore ? score : maxScore}</h3>
<button onclick="window.location.reload()">Play Again</button>
`;
endgameEl.style.display = 'flex';
}
//Event Listeners
startGameEl.addEventListener('click', startGame);
text.addEventListener('input', e => {
const insertedText = e.target.value;
if (insertedText === randomWord) {
addWordToDOM();
updateScore();
e.target.value = '';
//add time according to difficulty
if (difficulty === 'easy') {
time += 5; //adds 4 sec to timer
}
else if (difficulty === 'medium') {
time += 4; //adds 3 sec
}
else {
time += 3; //adds 2 sec
}
updateTime();
}
});
settingsBtn.addEventListener('click', () => settings.classList.toggle('hide'));
//set difficulty
settingsForm.addEventListener('change', e => {
difficulty = e.target.value;
localStorage.setItem('difficulty', difficulty);
})