Chinese Zodiac Calculator in JavaScript

Chinese Zodiac Calculator in JavaScript | In this article, we are going to learn how to make a Chinese Zodiac Calculator in Javascript. It can also be known as Sheng Xiao or Shu Xiang. It features 12 animals; Monkey, rooster, dog, pig, rat, ox, tiger, rabbit, dragon, snake, horse, and sheep.

As it consists of 12 animals, the zodiac reduces each year to a number from 0 to 12. We use the modulus operator, which divides a number and returns its remainder. We will use this reminder to determine the zodiac sign.

The remainder and sign relate to each other as follows:-
0: Monkey
1: Rooster
2: Dog
3: Pig
4: Rat
5: Ox
6: Tiger
7: Rabbit
8: Dragon
9: Snake
10: Horse
11: Sheep

For example, if we divide 2001 by 12, the remainder is 9, thus the Chinese Zodiac Sign of people born in 2001 is Snake.

Chinese Zodiac Sign Java Program

Chinese Zodiac Calculator in JavaScript – Program

Let’s try to implement this step by step in JavaScript:-

Step 1: Get input year from the user.

In this step, we try to get input from the user, the year for which they wish to calculate the Chinese Zodiac Sign. We will use the window.prompt() method to get the input. The code for these steps looks like the following:-

let year = window.prompt('Enter a year')

Step 2: Write a function to calculate the results.

We will use a switch case statement along with a modulus operator to calculate the Zodiac sign. The function takes in a parameter ‘year’ which we use to calculate the results. 

The switch case statement returns a string depending on the remainder of the modulus operation performed on the year parameter.

function calculateZodiac(year) {
    switch (year % 12) {
        case 0:
            return 'Monkey';
            break;
        case 1:
            return 'Rooster';
            break;
        case 2:
            return 'Dog';
            break;
        case 3:
            return 'Pig';
            break;
        case 4:
            return 'Rat';
            break;
        case 5:
            return 'Ox';
            break;
        case 6:
            return 'Tiger';
            break;
        case 7:
            return 'Rabbit';
            break;
        case 8:
            return 'Dragon';
            break;
        case 9:
            return 'Snake';
            break;
        case 10:
            return 'Horse';
            break;
        case 11:
            return 'Sheep';
            break;
    }
}

Step 3: Displaying the output

In this step, we run the function we created in the previous step and display the results to the user. We will use a simple console.log() method to display it.

let result = calculateZodiac(2030)
console.log(result)

Let us see the complete program for the Chinese Zodiac Calculator in JavaScript

function calculateZodiac(year) {
    switch (year % 12) {
        case 0:
            return 'Monkey';
            break;
        case 1:
            return 'Rooster';
            break;
        case 2:
            return 'Dog';
            break;
        case 3:
            return 'Pig';
            break;
        case 4:
            return 'Rat';
            break;
        case 5:
            return 'Ox';
            break;
        case 6:
            return 'Tiger';
            break;
        case 7:
            return 'Rabbit';
            break;
        case 8:
            return 'Dragon';
            break;
        case 9:
            return 'Snake';
            break;
        case 10:
            return 'Horse';
            break;
        case 11:
            return 'Sheep';
            break;
    }
}

let result = calculateZodiac(2030)
console.log(result)

Output:-

Dog

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *