2025-08-28 10:16:31 -04:00
|
|
|
// array of words to be displayed randomly
|
2025-08-27 12:09:24 -04:00
|
|
|
const words = [
|
2025-08-28 12:12:38 -04:00
|
|
|
'red', 'car', 'bed', 'hat', 'cat', 'mat', 'pet', 'bin', 'run', 'bun',
|
|
|
|
'the', 'and', 'of', 'in', 'is', 'but', 'it', 'bat', 'bar', 'map',
|
|
|
|
'sap', 'cap', 'hen', 'ten', 'jar', 'tip', 'lip', 'fin', 'was', 'good'
|
2025-08-27 12:09:24 -04:00
|
|
|
];
|
|
|
|
|
2025-08-28 10:16:31 -04:00
|
|
|
// initial player score
|
|
|
|
let score = 0;
|
|
|
|
|
2025-08-28 11:04:44 -04:00
|
|
|
// variable to grab a random word from the array
|
|
|
|
const randomWord = words[Math.floor(Math.random() * words.length)];
|
|
|
|
|
|
|
|
// function for replacing the word displayed
|
|
|
|
function replaceWord() {
|
|
|
|
console.log('new word');
|
|
|
|
document.querySelector('h1').innerHTML = `${words[Math.floor(Math.random() * words.length)]}`;
|
|
|
|
}
|
|
|
|
|
2025-08-28 10:16:31 -04:00
|
|
|
// this function increases score variable by one, changes the score displayed, and prints it to the console
|
|
|
|
function increaseScore() {
|
|
|
|
score += 1;
|
|
|
|
document.querySelector('.player-score').innerHTML = `${score}`;
|
|
|
|
console.log(`score increased to ${score}`)
|
|
|
|
}
|
|
|
|
|
2025-08-28 11:04:44 -04:00
|
|
|
// click event for the green correct button, increases the score and replaces the displayed word
|
2025-08-28 10:16:31 -04:00
|
|
|
document.querySelector('.correct').addEventListener("click", () => {
|
2025-08-28 11:04:44 -04:00
|
|
|
increaseScore();
|
|
|
|
replaceWord();
|
2025-08-28 11:30:22 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
// click event for the red wrong button, replaces the word displayed with a new one
|
|
|
|
document.querySelector('.wrong').addEventListener("click", () => {
|
|
|
|
replaceWord();
|
2025-08-28 11:04:44 -04:00
|
|
|
});
|