George Li
George Li

Reputation: 27

Javascript - Multiple Arrays containing Variables

I'm new to Javascript and finding some difficulty in a task writing a program.

My task will be to write a program that will allow a user to enter their date of birth. The program then proceeds to give the corresponding Chinese Zodiac sign in an image and the number of days the user has been alive.

What will be input?

Constants we will use

Create and appropriately name constants to store the following values:

My code so far:

var year = prompt('Enter year of birth as a 4 digit integer') // A prompt to enter the year of birth.
var month = prompt('Enter the name of the month of birth') // A prompt to enter the month of birth.
var date = prompt('Enter day of birth as an integer') // A prompt to enter the date of birth.
    var month = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]
    var month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
    var month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
    var month = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"]

I'm having trouble with the input of the month of birth. I'm trying to write a code to assume user will enter at least the first three letters of a month name, but this could be longer and could contain upper case characters, so it could be jan, Jan, january, or January, or other month names.

Any help will be appreciated!

George

Upvotes: 1

Views: 795

Answers (2)

Michael Berkowski
Michael Berkowski

Reputation: 270599

Use .substr() to shorten their input, and .toLowerCase() to convert it to lowercase. Then match it to your array of months (the lowercase version). Here's a bit to help you get started:

var month = prompt('Enter the name of the month of birth');
// Chop everything after the first 3 characters and make it lowercase
month = month.substr(0,3).toLowerCase();
// Store your array in months, differently named than the month input
var months = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];

// You can then use array.indexOf() to locate it in the array
// Not available in older browsers though
var pos = months.indexOf(month);
if (pos >= 0) {
   // valid month, number is pos
}

P.S. Don't forget the ; at the end of each statement!

Upvotes: 3

Josh Noe
Josh Noe

Reputation: 2798

Combine all of your possible month strings into one array:

var year = prompt('Enter year of birth as a 4 digit integer') // A prompt to enter the year of birth.
var month = prompt('Enter the name of the month of birth') // A prompt to enter the month of birth.
var date = prompt('Enter day of birth as an integer') // A prompt to enter the date of birth.
var possibleMonths = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec", 
    "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 
    "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", 
    "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"]

Then, iterate through that one array to check to see if your user entered one of those months:

var isValidMonth = false;

for (var i = 0; i < possibleMonths.length; i++){
    if (possibleMonths[i] === month){
        isValidMonth = true;
        break;
    }
}

alert(month + " is a valid month: " + isValidMonth);

Upvotes: 0

Related Questions