Help Center / Survey editor / Components / Embed API

Embed API

Embed API

Interface Name

Parameters

Interface Code

Case Study

  • year parameter: Represents the year.
  • name parameter: Represents the name of the Chinese zodiac.
    var zodiacs = ['Mouse', 'Ox', 'Tiger', 'Rabbit', 'Dragon', 'Snake', 'Horse', 'Goat', 'Monkey', 'Chicken', 'Dog', 'Pig'];
    // Define a function to get the zodiac sign corresponding to a given year
    var getZodiac = function (year) {
        // The Chinese lunar zodiac signs repeat every 12 years, and the corresponding zodiac sign is determined by taking the remainder of the year.
        // However, due to the fact that the starting year of the zodiac signs (such as the Jiazi year corresponding to the Year of the Mouse) does not fully correspond to the Gregorian calendar year,
        // a simple adjustment method is adopted here: process the remainder to match the actual zodiac sign.
        // Note: The processing method here is a simplified example and does not accurately reflect the conversion between the lunar and Gregorian calendars.
        var extra = year % 12;
        extra = extra < 4 ? 12 - 4 + extra : extra - 4;

        return zodiacs[extra];
};

// Return a function that takes a data object as a parameter and checks if the year matches the zodiac sign.
return function(data) {

    var year = Number(data.year.trim()); // Extract and convert the year to a number.
    var name = data.name.trim(); // Extract and remove any leading or trailing whitespace from the zodiac sign name.

    // Check if the zodiac sign for the given year or the previous year matches the provided zodiac sign name.
    // Considering that the date of the Lunar New Year may fall at different times within the Gregorian calendar year, two years are checked here.
    var valid = getZodiac(year) === name || getZodiac(year - 1) === name;

    // Return a result object containing the matching result and the corresponding message.
    return {
        result: valid, 
        message: valid ? '' : 'The zodiac sign and year do not match'
    }
}

Using embed APIs