Daily Horoscope

I took a slightly different route to implement what I’ve learned so far. I used today’s date with Date() into my getDate arrow function I re-format the date into a more readable format.
I then created a getStarSign arrow function that takes a day and a month and returns an object that contains the starsign name, start date and end date.
I generated a random number with Math.floor() and Math.random() between 0 and 9 and use a switch statement to return a random outlook for today.
I wanted to make use of the math and date functions as well as objects and both arrow and regular functions in my project. I know I didn’t use 3 random data sets as stipulated in the project overview but I felt that this was a good way to show what’ I’ve learned so far.

const currentDate = new Date();
const currentDay = currentDate.getDate();
const currentMonth = currentDate.getMonth();

const getDate = (day, month) => {
  const months = ["January","February","March","April","May","June","July","August","September","October","November","December",];

  const getDay = (date) => {
    const suffixes = ["th","st","nd","rd","th","th","th","th","th","th",];

    if (date >= 11 && date <= 13) {
      return `${date}th`;
    } else {
      const lastDigit = date % 10;
      const indicator = suffixes[lastDigit];
      return `${date}${indicator}`;
    }
  };

  const dateString = `${getDay(day)} of ${months[month]}`;

  return dateString;
};

const getStarSign = (day, month) => {
  const starSigns = [
    { name: "Capricorn", startMonth: 0, startDay: 1, endMonth: 0, endDay: 19 },
    { name: "Aquarius", startMonth: 0, startDay: 20, endMonth: 1, endDay: 18 },
    { name: "Pisces", startMonth: 1, startDay: 19, endMonth: 2, endDay: 20 },
    { name: "Aries", startMonth: 2, startDay: 21, endMonth: 3, endDay: 19 },
    { name: "Taurus", startMonth: 3, startDay: 20, endMonth: 4, endDay: 20 },
    { name: "Gemini", startMonth: 4, startDay: 21, endMonth: 5, endDay: 20 },
    { name: "Cancer", startMonth: 5, startDay: 21, endMonth: 6, endDay: 22 },
    { name: "Leo", startMonth: 6, startDay: 23, endMonth: 7, endDay: 22 },
    { name: "Virgo", startMonth: 7, startDay: 23, endMonth: 8, endDay: 22 },
    { name: "Libra", startMonth: 8, startDay: 23, endMonth: 9, endDay: 22 },
    { name: "Scorpio", startMonth: 9, startDay: 23, endMonth: 10, endDay: 21 },
    { name: "Sagittarius", startMonth: 10, startDay: 22, endMonth: 11, endDay: 21, },
    { name: "Capricorn", startMonth: 11, startDay: 22, endMonth: 11, endDay: 31, },
  ];

  for (const sign of starSigns) {
    if (
      (currentMonth === sign.startMonth && currentDay >= sign.startDay) ||
      (currentMonth === sign.endMonth && currentDay <= sign.endDay)
    ) {
      return {
        name: sign.name,
        startDate: getDate(sign.startDay, sign.startMonth),
        endDate: getDate(sign.endDay, sign.endMonth),
      };
    }
  }
};

function generateRandomNumber(num) {
  return Math.floor(Math.random() * num);
}

const starSign = getStarSign();

const horoscopeBrief = () => {
  switch (starSign.name) {
    case "Capricorn":
      return "Capricorn, (from Latin: “Goat-Horned”) also called Capricornus and the Goat, in astrology, the 10th sign of the zodiac, considered as governing the period from about December 22 to about January 19. One explanation of the fishtail with which the goat is often represented is found in the Greek myth of Pan, who, to avoid the monster Typhon, jumped into the water just as he was changing into animal shape. The half above water assumed the shape of a goat while the lower half, the tail, assumed the shape of a fish.";
      break;
    case "Aquarius":
      return "Aquarius, (Latin: “Water Bearer”) in astronomy, zodiacal constellation lying in the southern sky between Capricornus and Pisces, at about 22 hours right ascension and 10° south declination. It lacks striking features, the brightest star, Sadalmelik (Arabic for “the lucky stars of the king”), being of magnitude 3.0.";
      break;
    case "Pisces":
      return "Pisces, (Latin: “Fishes”) in astronomy, zodiacal constellation in the northern sky between Aries and Aquarius, at about 1 hour right ascension and 15° north declination. The vernal equinox, the point where the Sun’s annual apparent path takes it north of the celestial equator and from which celestial longitude and right ascension are measured, lies in Pisces. The constellation contains only faint stars without any striking grouping; the brightest star, Eta Piscium, has a magnitude of 3.6.";
      break;
    case "Aries":
      return "Aries, (Latin: “Ram”) in astronomy, zodiacal constellation in the northern sky lying between Pisces and Taurus, at about 3 hours right ascension and 20° north declination.";
      break;
    case "Taurus":
      return "Taurus, (Latin: “Bull”) in astronomy, zodiacal constellation lying in the northern sky between Aries and Gemini, at about 4 hours 20 minutes right ascension and 16° north declination. The constellation’s brightest star, Aldebaran (Arabic for “the follower”; also called Alpha Tauri), is the 14th brightest star in the sky, with a magnitude of 0.85. The constellation also contains the Crab Nebula (M1) and the Pleiades and Hyades star clusters.";
      break;
    case "Gemini":
      return "Gemini, (Latin: “Twins”) in astronomy, zodiacal constellation lying in the northern sky between Cancer and Taurus, at about 7 hours right ascension and 22° north declination. Its brightest stars are Castor and Pollux (Alpha and Beta Geminorum); Pollux is the brighter of the two, with a magnitude of 1.15, and is the 17th brightest star in the sky. The summer solstice, the northernmost point reached by the Sun in its annual apparent journey among the stars, lies in Gemini. This constellation also contains the isolated pulsar Geminga.";
      break;
    case "Cancer":
      return "Cancer, (Latin: “Crab”) in astronomy, zodiacal constellation lying in the northern sky between Leo and Gemini, at about 8 hours 25 minutes right ascension and 20° north declination. It contains the well-known star cluster called Praesepe, or the Beehive. Its brighest star, Al Tarf (Arabic for “the end” [of one of the crab’s legs]), also called Beta Cancri, is quite dim, with a magnitude of 3.6.";
      break;
    case "Leo":
      return "Leo, (Latin: “Lion”) in astronomy, zodiacal constellation lying in the northern sky between Cancer and Virgo, at about 10 hours 30 minutes right ascension and 15° north declination. Regulus (Latin for “little king”; also called Alpha Leonis), the brightest star, is of magnitude 1.35. The November meteor shower called the Leonids has its radiant, or point of apparent origin, in Leo. Many of the stars in Leo form an asterism called the Sickle.";
      break;
    case "Virgo":
      return "Virgo, (Latin: “Virgin”) in astronomy, zodiacal constellation lying in the southern sky between Leo and Libra, at about 13 hours right ascension and 2° south declination. The constellation’s brightest star, Spica (Latin for “head of grain,” also called Alpha Virginis), is the 15th brightest star in the sky, with a magnitude of 1.04. Virgo contains the nearest large cluster of galaxies, the Virgo cluster, in which is located the giant elliptical galaxy Virgo A and PSR 1257+12, the pulsar around which the first extrasolar planets were discovered in 1992.";
      break;
    case "Libra":
      return "Libra, (Latin: “Balance”) in astronomy, zodiacal constellation in the southern sky lying between Scorpius and Virgo, at about 15 hours 30 minutes right ascension and 15° south declination. Its stars are faint; the brightest star, Zubeneschamali (Arabic for “northern claw,” as it was earlier regarded as part of Scorpius; also called Beta Librae), has a magnitude of 2.6.";
      break;
    case "Scorpio":
      return "Scorpius, (Latin: “Scorpion”) also called Scorpio, in astronomy, zodiacal constellation lying in the southern sky between Libra and Sagittarius, at about 16 hours 30 minutes right ascension and 30° south declination. Its brightest star, Antares (Alpha Scorpii), the 15th brightest star in the sky, has a magnitude of 1.1. Its name comes from the Greek for “rival of Ares” (i.e., rival of the planet Mars) and was probably given because of the star’s red colour and brightness. The brightest X-ray source in the sky, Scorpius X-1, is found in this constellation.";
      break;
    case "Sagittarius":
      return "Sagittarius, (Latin: “Archer”) in astronomy, zodiacal constellation in the southern sky lying between Capricornus and Scorpius, at about 19 hours right ascension and 25° south declination. The centre of the Milky Way Galaxy lies in the radio source Sagittarius A*. Near the western border of Sagittarius is the winter solstice, the southernmost point reached by the Sun in its apparent annual journey among the stars. This constellation also contains the Lagoon and Trifid nebulas. The brightest star is Kaus Australis (from the Arabic for “bow” and the Latin for “southern,” respectively; it is also called Epsilon Sagittarii), with a magnitude of 1.9. Many of the stars are arranged in the prominent asterism called the Teapot.";
      break;
    default:
      break;
  }
};

const todaysForecast = () => {
  const rand = generateRandomNumber(10);

  switch (rand) {
    case 0:
      return "Don’t let FOMO take you on too many tangents today. You’re only one person in the end and can’t show up to five simultaneous events. Instead of trying to do it all, simplify your schedule by skipping activities you didn’t want to participate in to begin with. Your ruler, mental Mercury, is in sync with organized Saturn, allowing you to concentrate on what’s most important. Pick a high-priority endeavor, and give it your undivided attention.";
      break;
    case 1:
      return "Your magnetism is off the charts, thanks to today’s trine between expressive Mercury in your communication camp and authoritative Saturn in your popularity zone. Don’t get stretched too thin while trying to give each and every admirer a minute of your time. Prioritize ride-or-die friends and pare down your plans, bringing together different circles if that makes it easier to see all the people you want to see. Grab some blankets and shareable snacks and put out the word that you’re going to tonight’s movie in the park. Come one, come all!";
      break;
    case 2:
      return "You’d like nothing more than to tuck into a pint of salted caramel gelato today, but what’s driving that impulse? If that summer treat is also a way to numb painful feelings, today’s fortifying connection between mindful Mercury and stabilizing Saturn offers another alternative: Dig a little deeper. Perhaps you’re craving companionship or a creative outlet to balance the repetition of your 9-5. Get in touch with the person who always sweetens up your mood. You could be hatching plans for a visit before you hit the sack tonight.";
      break;
    case 3:
      return "Don’t be so humble! With expressive Mercury in your fifth house of fame fist-bumping capable Saturn in your sign today, you can toot your own horn without coming across as a diva. You’ve clearly accomplished enough to deserve a round of applause. But how can you use your impressive experience to make a difference in the world? Can you see an opening to give others a leg up? Put tech and well-connected friends to good use now. A shout-out on social media can raise your profile AND shine a spotlight on a worthy cause.";
      break;
    case 4:
      return "Get ready to glow! Today’s healthy harmony between brainy Mercury and structured Saturn directs you toward wellness-boosting choices. Do a guided meditation after you roll out of bed, order an iced matcha instead of a caramel frappuccino on your way to work and take breaks to just center and connect to your breath throughout the day. A walk in nature will ground you, even if all you can squeeze in is cutting through the park on your way home. Stopping to smell the flowers may feel like a cliché, but it’s a guaranteed mood lifter.";
      break;
    case 5:
      return "What’s the point in going it alone when you could ask the VIPs in your life to help you out? Today’s Mercury-Saturn harmony blesses you with added game, making this an ideal time to call in a few favors. Is a bureaucracy proving impossible to navigate? Maybe a seasoned colleague can help you work the system. Who can fault you for getting assistance and offering your sincere thanks? If you feel like you should return the favor, ask how you can contribute to their latest project.";
      break;
    case 6:
      return "Even the most capable can appreciate the value of forming dynamic duos when two heads are better than one. Today could produce a winning partnership as clever Mercury in your relationship realm joins forces with your galactic guardian, structured Saturn, in your communication camp. While a magnetic personality may appeal to you temporarily, timekeeper Saturn prompts you to form a union that has staying power. If you’re already engaged in talks, spell out your terms clearly so there’s no room for misunderstanding. You’re not usually one to shy away from calling the shots; don’t make an exception now!";
      break;
    case 7:
      return "Is a relationship feeling more heavy than hot lately? A night out with your squad could be just what you need. If you start a venting session, you’ll soon discover that nobody’s love life is totally perfect. You might even see your bae in a whole new light and wind up feeling lucky compared to others. The unattached could savor their single status after listening to your friends’ relationship woes. Try blowing off excess steam via a challenging workout. It might even put you in the mood to pounce on the object of your newfound affection.";
      break;
    case 8:
      return "Use your charisma to lure in your target. Today’s decorous Mercury-Saturn trine discourages you from chasing after anyone or anything in an obvious way. Instead, craft an appealing pitch that pulls people in for a closer look. You’ll have them wrapped around your finger, and they’ll end up feeling like the pursuer. Don’t broadcast your ideas to the masses, though. Run them by your inner circle. They’ll give you a big confidence boost that prepares you to go wide.";
      break;
    case 9:
      return "Strengthen your brilliant ideas with solid data today, and you can’t lose. With savvy Mercury in your sign fist-bumping ambitious Saturn in your visionary ninth house, you’ll have a knack for wedding style and substance. Support your assertions with conclusive research. And instead of simply pitching an inspired concept, make it a one-two punch by including the airtight game plan you’ve mapped out to get a team to your desired destination. Expect a chorus of yesses to follow.";
      break;
  }
};

console.log(`Welcome to today's Horoscope!`);
console.log(``);
console.log(`The date is the ${getDate(currentDay, currentMonth)}.`);
console.log(`The current star-sign is ${starSign.name}.`);
console.log(
  `${starSign.name} starts on ${starSign.startDate} and finishes on ${starSign.endDate}.`
);
console.log(``);
console.log(horoscopeBrief());
console.log(``);
console.log(`This is what is in store for you today!:`);
console.log(todaysForecast());

Nice.

Note, const getStarSign = (day, month) => doesn’t actually look at its parameters. Rather than currentDate global, perhaps pass all params?

You’re defining a starSigns array of objects. You could reasonably pop horoscopeBrief in there as well.

When you get to classes, or really just objects with methods, you could have a starSign object offer a method like inSign(date).

Have fun. Keep playing.

Oh you’re absolutely right. I’m surprised I overlooked that. I definitely learned that more planning would prevent me from adjusting functions multiple times to adjust as it evolved.

I definitely learned that more planning would prevent me from adjusting functions multiple times to adjust as it evolved.

Adjusting functions multiple times is the process. It even has a name; refactoring. You can plan for the stuff you see up front, but it is only as you work on a problem then you’ll worry out all the details. Elements that you couldn’t conceive of in the planning stage make themselves obvious in development. So, don’t worry, you’re doing it right.

I’ll admit I was amused enough by this that I considered a refactor. Something like:

console.log([
    // { name: "Capricorn", startMonth: 0, startDay: 1, endMonth: 0, endDay: 19 },
    { name: "Aquarius", startMonth: 0, startDay: 20, endMonth: 1, endDay: 18 },
    { name: "Pisces", startMonth: 1, startDay: 19, endMonth: 2, endDay: 20 },
    { name: "Aries", startMonth: 2, startDay: 21, endMonth: 3, endDay: 19 },
    { name: "Taurus", startMonth: 3, startDay: 20, endMonth: 4, endDay: 20 },
    { name: "Gemini", startMonth: 4, startDay: 21, endMonth: 5, endDay: 20 },
    { name: "Cancer", startMonth: 5, startDay: 21, endMonth: 6, endDay: 22 },
    { name: "Leo", startMonth: 6, startDay: 23, endMonth: 7, endDay: 22 },
    { name: "Virgo", startMonth: 7, startDay: 23, endMonth: 8, endDay: 22 },
    { name: "Libra", startMonth: 8, startDay: 23, endMonth: 9, endDay: 22 },
    { name: "Scorpio", startMonth: 9, startDay: 23, endMonth: 10, endDay: 21 },
    { name: "Sagittarius", startMonth: 10, startDay: 22, endMonth: 11, endDay: 21, },
    { name: "Capricorn", startMonth: 11, startDay: 22, endMonth: 11, endDay: 31, },
].map((x, idx) => ({ idx, name: x.name, start: { mm: x.startMonth + 1, dd: x.startDay }, brief: horoscopeBrief(x) })));

Results:

[
    {
        idx: 0,
        name: 'Aquarius',
        start: { mm: 1, dd: 20 },
        brief: 'Aquarius, (Latin: ...'
    },
    {
        idx: 1,
...

I didn’t like the endMonth and endDay for the search, I’d rather look at the next one and keep the possibility of denormalized data out of design. The idx is probably unnecessary, depending on the final search, but I hadn’t gotten that far. Or, as a final state, an end date would get folded back… Process.

Keep at it. The more refactoring you do, the better your planning stage will be. However, it’s a learning process and you don’t get to one without the other. Have fun.

1 Like

Absolutely. It’s been fun practicing what I’ve learned and implementing it into code that puts it together and does something useful. I’m very much looking forward to joining front and back-end knowledge together to create some interactive websites. Thanks for taking the time. I very much appreciate it.