Tips for assigning date ranges to arrays and then conducting comparisons with date input information

I'm developing an application with two date input fields. The current function I have calculates the sum of the digits in each field until it reaches a single digit, and then displays the result along with some text.

Now, I need to create another function that considers only the month and day (disregarding the year) from the input field. It should match this date with one of 12 different date ranges to determine the astrology sign associated with it. For example, a date like 04/09 would be matched with "Aries". The challenge here lies in comparing the input date with the 12 predefined date ranges.

I'm unsure how to structure this new function. It seems like I'll have to create an array with the 12 date ranges and somehow validate the input against this array.

Any suggestions? Here's my existing JavaScript and HTML code:

var ZodiacSigns = ['Aries', 'Taurus', 'Gemini', 'Cancer', 'Leo', 'Virgo', 'Libra', 'Scorpio', 'Saggitarius', 'Capricorn','Aquarius', 'Pisces'];

function getSum() {
// Place the id's of the input and output elements into respective arrays
let inputs = ['dateInput1','dateInput2'];
let outputs = ['result1','result2'];

inputs.forEach(function(input, index){

const inputValue = document.getElementById(input).value;
var sum = 0;
for (var i = 0; i < inputValue.length; i++) {
  const num = parseInt(inputValue.charAt(i));
  if (!isNaN(num)) {
    sum += num;
  }
}
const total = (sum - 1) % 9 + 1;

document.getElementById(outputs[index]).textContent = "Your number is: " + total;

});
}

<div class="container">

    <div class="cell-1" id="centerElement">
        <div id="cell-1-nest-l"></div>
        <div id="cell-2-nest-l"></div>
        <div id="cell-3-nest-l"></div>
        <div id="cell-4-nest-l"><h3>your name</h3></div>
        <div id="cell-5-nest-l"></div>
        <div id="cell-6-nest-l"><input type="text" class="nameStyle1" id="nameInput1"></div>

</div>

    <div class="cell-2" id="centerElement" ><img onclick="getSum();" 
src="file:///Users/Nineborn/Downloads/My%20Post%20(5).png" alt=""></div>

    <div class="cell-3" id="centerElement" >
            <div id="cell-1-nest"></div>
            <div id="cell-2-nest"></div>
            <div id="cell-3-nest"><h3>their name</h3></div>
            <div id="cell-4-nest"></div>
            <div id="cell-5-nest"><input type="text" class="nameStyle1" id="nameInput2"> 
 </div>
            <div id="cell-6-nest"></div>


            </div>

    <div class="cell-4" id="result1">
        <input type="date" class="dateStyle1" id="dateInput1">
            </div>

    <div class="cell-5"><button>Start Over</button></div>

    <div class="cell-6" id="result2"> 
        <input type="date" class="dateStyle2" id="dateInput2" placeholder="Their Bday">
            </div>
    <div class="cell-7"></div>

    <div class="cell-8"></div>

    <div class="cell-9"></div>

</div>

Answer №1

There are numerous methods to achieve this task. Provided below is a brief example that follows your instructions, utilizing an array of ranges for comparison.

The task of determining actual zodiac dates is left for the reader to complete.

function identifyZodiac(day, month){

    // storing start/end ranges for each zodiac sign
    let zodiacs = {
        'crab':{
            'start':[1,1],
            'end':[25,7]
        },
        'dog':{
            'start':[25,7],
            'end':[31,12]
        }
    }

    // iterating through each zodiac sign in the zodiacs object
    for(let zodiac_name in zodiacs){

        // checking if the provided day and month fall within the range for each zodiac sign
        if(
            (month >= zodiacs[ zodiac_name ].start[1] && month < zodiacs[ zodiac_name ].end[1]) && // checking month range
            (day >= zodiacs[ zodiac_name ].start[0] && day < zodiacs[ zodiac_name ].end[0]) // checking day range
        ){
            console.log(`You're a ${zodiac_name}!`); // displaying the zodiac sign
        }
    }
}

identifyZodiac(1, 1); // you're a crab!
identifyZodiac(27, 7); // you're a dog!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Elements positioned absolutely using the ::after and ::before pseudo-elements do not adhere to the very bottom of the body

Feeling a bit crazy right now! Haha. I'm facing a challenge in positioning an image at the bottom of a webpage. It seems to work fine when the page width is larger, around 1360px, but as soon as I shrink it to 1206px or less, the image gets pushed up, ...

Issue with Vue directive bind not functioning after element refresh

My approach involves utilizing vue.js to create forms, where all fields are structured within a JavaScript objects array. Here is an example of the structure I use: { type: "input", mask: "date", default: "2018/04/14" }, { type: "input", ...

Using AngularJS to show/hide elements within a colgroup tag

Looking to create a dynamic table allowing users to hide certain columns. Wondering if it's possible to use ng-show with colgroup or col tags instead of adding ngshow to each cell individually. Struggling to make it work... <colgroup ng-repeat="mt ...

The error message "TypeError: (0, _style.default) is not a function" occurred when using jest along with material

My current configuration looks like this: // .jestrc.json ... "moduleNameMapper": { "style$": "<rootDir>/tests/mock.ts" } // mock.ts export default {} This setup is typically used to exclude assets from jest to pre ...

How can you replace a public method in a specific class in Javascript?

I have a JavaScript class that I need to test: TestClass = function { // some code that utilizes initializeRequest this.initializeRequest = function() { if (window.XMLHttpRequest) { return new XMLHttpRequest(); ...

How can I verify if an SVG file has a reliable source? (having troubles converting SVG to canvas)

While attempting to use an SVG generated by a chart plugin (https://wordpress.org/plugins/visualizer/), I am facing issues retrieving the source of the SVG-image being created. Interestingly, when using other SVGs with the same code, everything works perfe ...

Failure to send AJAX POST data

When the login button is clicked, I want to send the contents of the email text box. However, nothing happens when I click the login button. Here is the file directory structure: Model/ <-- configuration.php is located inside the Model ...

Eliminating duplicate CSS properties from multiple CSS files can help streamline your stylesheets

I am dealing with a situation where I have two CSS files containing redundant CSS properties. For example, in foo.css: #test { border: 0; font-size: 16px; } In bar.css: #test { border: 0; font-size: 32px; width: auto; } My goal is ...

Preserve dropdown selections in JavaScript even when the page is refreshed

Two dropdown menus are present, where the second dropdown menu is dependent on the selection made in the first dropdown. However, after refreshing the page following an ajax update, the second dropdown does not retain the previously selected choice. How ...

MUI - Material-table/core - Checkbox selection malfunctioning on click event

UPDATE : The matter also pertains to Material Ui's Data table. I attempted to replicate the issue using the provided example in the documentation but encountered the same problem. I have been struggling with an issue related to the selection feature ...

Tips for transforming a container div into a content slider

Working with Bootstrap 3, a custom div has been created as shown below: <div class="second-para"> <div class="container"> <div class="second-section"> <div class="c ...

Can you explain the reference point used in the `drawImage()` function on a canvas?

Here is an inquiry concerning the usage of the drawImage() function. var x = (i-j)*(img[0].height/2) + (ctx.canvas.width/2)-(img[0].width/2); var y = (i+j)*(img[0].height/4); abposx = x + offset_x; abposy = y + offset_y; ctx.drawImage ...

Mootools tweening color transition denied for child elements when color is explicitly defined in CSS

Check out this link for testing: http://jsfiddle.net/EnJSM/ You might observe that by removing "color: #6CB5FF;", the transition behaves differently - it only works for the second part of the line. I'm interested to see what solution you come up wit ...

What steps can I take to stop Firefox from automatically adding vertical scrollbars to a select dropdown when I change the background color?

Having an issue with changing the background color of a select dropdown. It works fine in Chrome and Safari, but when I try to change the background color in Firefox to anything other than #FFFFFF, the appearance of the dropdown changes drastically. It see ...

How can one deactivate a <MenuItem> component in material-ui?

Currently, I am in the process of developing a personalized combo box using React and Material-UI. This unique combo box will exhibit the chosen value within the input, present a drop-down menu with various options (MenuItems), and feature a text box at th ...

Display/Collapse SELECT choices?

Consider this scenario : <select class="form-control" name="blah" ng-model="$ctrl.form.blah" ng-options="item.id as item.name group by item.etype | uppercase for item in $ctrl.opts"></select> My goal is to toggle the display of each GROUP b ...

Create custom dynamic asset tags in AngularJS inspired by Mixture.io's features for seamless HTML templating

Curious about the potential for creating dynamic asset tags within Angular and, if so, the method to achieve this. Here's the backstory: I've been utilizing Mixture.io for templating and have become accustomed to its seamless and adaptable natur ...

Looking for a script to automate clicking on a specific web element using JavaScript

When working with Selenium, we utilize an action to interact with an element by clicking on it at specific coordinates (X, Y). action.MoveToElement(element, X, Y).Click().Build().Perform() I am looking to achieve the same functionality using Javascript. ...

The initial click on a jQuery event is not registering as expected

My webpage includes a code snippet like this: $('#language_id').change(function () { var urli = 'https://example.com/php/get_arch.php?language_id=' + language_id; $.ajax({ type: "GET", url: urli, dataType: &ap ...

Using AJAX to upload images to a MySQL database with PHP

I'm interested in experimenting with uploading an image using PHP and MySQL. My approach involves using a form to send data via AJAX. Here is my HTML code: <input type="file" name="logo" id="logo" class="styled"> <textarea rows="5" cols="5" ...