I have very little experience with coding and I’ve never asked a question on here so please bear with me. I am trying to make a "word of the day" generator with html, css, and javascript specifically (please no other languages if possible), for my website. My site is hosted on one of those drag-and-drop website builders that lets you put a few lines of html code in a block. I used an AI code generator to get this javascript, and instead of changing the word each day it chose to use the randomize function. So every time I reload my website page, the function spits out a random word/definition from the array. I would like to move to the next word/definition in the array every time the day changes. I don’t even mind if it chooses a new random word from the array instead of just moving to the next object, as long as it only changes once a day and not each time you load it.
And I do want to fill it with my own words and definitions rather than pull from and API. It just makes things simpler for me if I want to change something around later, and copy it to other types of apps I’m trying to make.
Oh, and using "wordo" instead of "word", and "definitiono" instead of "definition" was entirely purposeful. I have other things like a searchable dictionary and an actual word generator on the same webpage that I’m trying to put this on, and when different elements get called the same thing it gets screwy really fast so that was my fix. Keeping the words in the word container is also intentional, it just goes with the styling on my site.
Here’s my code:
<!DOCTYPE html>
<html>
<head>
<style>
.word-container {
width: 80%;
outline: none;
font-size: 16px;
border-radius: 5px;
padding-left: 15px;
padding-right: 27px;
border: 1px solid #999;
}
</style>
</head>
<body>
<div class="word-container">
<h2 id="wordo"></h2>
<p id="definitiono"></p>
</div>
<script>
const wordos = [
{
wordo: "Abstemious",
definitiono:
"sparing or moderate in eating and drinking; temperate in diet",
},
{
wordo: "Bellicose",
definitiono: "demonstrating aggression and willingness to fight",
},
{
wordo: "Tawdry",
definitiono:
"showy but cheap and of poor quality; sordid or unpleasant",
},
];
{
const wordoOfTheDay = wordos[Math.floor(Math.random() * wordos.length)];
const wordoEl = document.getElementById("wordo");
const definitionoEl = document.getElementById("definitiono");
wordoEl.textContent = wordoOfTheDay.wordo;
definitionoEl.textContent = wordoOfTheDay.definitiono;
}
</script>
</body>
</html>
2
Answers
Try and see if this code works for you 🙂
Hello and welcome to StackOverflow! I’ve come up with a method to achieve your goal, but here’s an explanation of what you should be doing first:
Get the current word index from localStorage or use 0 as the default value if it does not exist.
Display the word and its definition using the index obtained in step 2.
As for the code, here’s the step-by-step implementation:
checkAndUpdateWordIndex()
to check and update the word index in localStorage.updateWordIndex()
to increment the word index in localStorage.displayWord()
to display the word and its definition using the index from localStorage.Now, incorporate the above functions and logic into your existing JavaScript code, and it will change the word once every day, while maintaining the same word for all page reloads in between.
Let me know it went. 🙂