Files
trinkets/wally/app.js

36 lines
1.2 KiB
JavaScript
Raw Normal View History

// array of words to be displayed randomly
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'
];
// initial player score
let score = 0;
// 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)]}`;
}
// 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}`)
}
// click event for the green correct button, increases the score and replaces the displayed word
document.querySelector('.correct').addEventListener("click", () => {
increaseScore();
replaceWord();
});
// click event for the red wrong button, replaces the word displayed with a new one
document.querySelector('.wrong').addEventListener("click", () => {
replaceWord();
});