Dev.to WebDev 🛠 Dev 👁 0 📖 3 min read

Build a Daily Horoscope App with JavaScript

A daily horoscope app is a fun beginner project because it combines three useful skills: working with dates, picking content based on the date, and rendering it nicely. In this tutorial, we will build one that shows toda

A daily horoscope app is a fun beginner project because it combines three useful skills: working with dates, picking content based on the date, and rendering it nicely. In this tutorial, we will build one that shows today's reading for your zodiac sign — and we will treat the reading the right way, as a daily reflection, not a prediction.

If you want to understand what a horoscope actually is before you build around it, this horoscope guide explains how a rashifal is made, the difference between Sun sign and Moon sign, and how to read daily, weekly, monthly, and yearly readings without superstition.

Let us start with the HTML — a sign picker, a card for today's reading, and a date display:

<label for="sign">Choose your sign:</label>
<select id="sign">
  <option value="Aries">Aries</option>
  <option value="Taurus">Taurus</option>
  <option value="Gemini">Gemini</option>
  <option value="Cancer">Cancer</option>
  <option value="Leo">Leo</option>
  <option value="Virgo">Virgo</option>
  <option value="Libra">Libra</option>
  <option value="Scorpio">Scorpio</option>
  <option value="Sagittarius">Sagittarius</option>
  <option value="Capricorn">Capricorn</option>
  <option value="Aquarius">Aquarius</option>
  <option value="Pisces">Pisces</option>
</select>

<div id="card">
  <h2 id="signName"></h2>
  <p id="date"></p>
  <p id="reading"></p>
  <p><strong>Lucky focus:</strong> <span id="focus"></span></p>
</div>

Now the JavaScript. We store a few short readings per sign and pick one based on the day of the year, so the reading changes every day but stays the same for everyone with that sign on that day:

const readings = {
  Aries: [
    'A good day to start something you have been postponing.',
    'Speak first in the meeting today — your idea is ready.',
    'Slow down in the evening; rest is also progress.'
  ],
  Taurus: [
    'A small saving today will feel good by the end of the month.',
    'Finish one pending task before starting anything new.',
    'Good food and good company — keep the evening simple.'
  ],
  Gemini: [
    'A conversation today could open a door you did not expect.',
    'Write down the idea before it slips away.',
    'Say less, listen more — someone needs to be heard.'
  ]
  // Add the other nine signs the same way.
};

const focuses = ['Work', 'Health', 'Relationships', 'Money', 'Learning'];

function dayOfYear(date) {
  const start = new Date(date.getFullYear(), 0, 0);
  return Math.floor((date - start) / 86400000);
}

function showHoroscope() {
  const sign = document.getElementById('sign').value;
  const today = new Date();
  const list = readings[sign] || ['A calm, steady day. Take it one step at a time.'];
  const index = dayOfYear(today) % list.length;

  document.getElementById('signName').textContent = sign;
  document.getElementById('date').textContent = today.toDateString();
  document.getElementById('reading').textContent = list[index];
  document.getElementById('focus').textContent =
    focuses[dayOfYear(today) % focuses.length];
}

document.getElementById('sign').addEventListener('change', showHoroscope);
showHoroscope();

How it works: dayOfYear converts today's date into a number, and the modulo operator (%) picks one reading from the list. Because the number only changes once a day, everyone with the same sign sees the same reading all day — and a new one tomorrow. The same trick picks the day's lucky focus.

A few ideas to extend it: add all twelve signs with three readings each, save the user's sign in localStorage so it is remembered, add a weekly view that shows the next seven days, or fetch real horoscope data from a public API and compare it with your own readings. And remember the honest framing: this is a reflection tool, not a fortune-teller — say so in the app itself.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.