I'm working on a fun code project where I aim to display random animal names in different colors that change when a specific keyboard key is pressed. The challenge I'm facing is that the first random animal name in a random color only appears after pressing either the "r" or "t" key, rather than showing up right at the beginning when the script starts running. I've attempted placing the initial display outside the event listener function but then encountered issues with saving the occurrence in the saveData array. I prefer not to add additional code just to save the first occurrence if possible. Here's a snippet of my code:
<!DOCTYPE html>
<html lang="en">
...
The desired output should look like this:
const showAnimal = () => {
// Code for displaying random animals in colors
}
document.addEventListener('keydown', (event) => {
// Event listener for changing animals on key press
});
showAnimal();
I need assistance in ensuring that a random name in a random color appears as soon as the script begins and is correctly saved in the saveData array along with subsequent occurrences triggered by keyboard inputs. Any suggestions on how to achieve this?