1

我是 Javascript 新手,在编写程序的任务中发现了一些困难。

我的任务是编写一个程序,允许用户输入他们的出生日期。然后程序继续在图像中给出相应的中国十二生肖和用户存活的天数。

会输入什么?

  • 用户的出生年份(假设将输入有效的 4 位数年份)
  • 用户的出生月份(假设用户将至少输入月份名称的前三个字母,但这可能会更长并且可能包含大写字符,因此可能是 jan、Jan、january 或 January,或其他月份名称)
  • 用户的出生日期(以月份为单位)(假设将输入有效日期)

我们将使用的常量

创建并适当命名常量以存储以下值:

  • 包含月份缩写“JANFEBMARAPPRMAYJUNJULAUGSEPOCTNOVDEC”的字符串
  • 一天的毫秒数 1000*60*60*24
  • 中国十二生肖的周期
  • 1924年中国十二生肖开始的年份

到目前为止我的代码:

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"]

我在输入出生月份时遇到问题。我正在尝试编写一个代码来假设用户将至少输入月份名称的前三个字母,但这可能会更长并且可能包含大写字符,因此它可能是 jan、Jan、january 或 January,或者其他月份名称。

任何帮助将不胜感激!

乔治

4

2 回答 2

3

用于.substr()缩短他们的输入,并将.toLowerCase()其转换为小写。然后将其与您的月份数组匹配(小写版本)。这里有一些帮助您入门:

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
}

PS不要忘记;每个语句末尾的!

于 2012-07-25T01:59:16.463 回答
0

将所有可能的月份字符串组合成一个数组:

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"]

然后,遍历该数组以检查您的用户是否输入了其中一个月份:

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);
于 2012-07-25T02:03:47.297 回答