如果我知道她接受的月份,我如何确定孕妇的出生月份?

时间:2015-09-25 23:11:08

标签: javascript arrays

我尝试了以下代码并取得了成功:



function pregnancy(){
	var letKnow =prompt("what month did you take in?");
	var find = new Date;
	var allocate= new Array();
	month[0] = "JANUARY";
	month[1] = "FEBRUARY";
	month[2] = "MARCH";
	month[3] = "APRIL";
	month[4] = "MAY";
	month[5] = "JUNE";
	month[6] = "JULY";
	month[7] = "AUGUST";
	month[8] = "SEPTEMBER";
	month[9] = "OCTOBER";
	month[10] = "NOVEMBER";
	month[11] = "DECEMBER";
	var discover= allocate[find.getmonth()];
	var capsensitive = letKnow.toLowerCase();
	var birthEquation = discover - capsensitive;
	alert("Congratulations, you will put to birth in " + birthEquation);
}
pregnancy();




在我尝试运行它时,似乎没有任何工作

2 个答案:

答案 0 :(得分:3)

我认为这是最好的方式。

<script>
  function putToBirth(month) {
    return month == 3 ? 12 : (month + 9) % 12;
  }

  var letKnow = -Infinity;

  while (isNaN(letKnow) || letKnow < 1 || letKnow > 12 )
    letKnow = prompt("What month did you take it in? (1 to 12)");

  alert("Congratulations, you will put to birth in month #" + putToBirth(Number(letKnow)));
</script>

答案 1 :(得分:1)

为了保留您的逻辑,这是一个可行的解决方案:

function pregnancy(){
    var letKnow =prompt("what month did you take in?");
    var monthArray = new Array();
    monthArray[0] = "JANUARY";
    monthArray[1] = "FEBRUARY";
    monthArray[2] = "MARCH";
    monthArray[3] = "APRIL";
    monthArray[4] = "MAY";
    monthArray[5] = "JUNE";
    monthArray[6] = "JULY";
    monthArray[7] = "AUGUST";
    monthArray[8] = "SEPTEMBER";
    monthArray[9] = "OCTOBER";
    monthArray[10] = "NOVEMBER";
    monthArray[11] = "DECEMBER";
    var upperMonth = letKnow.toUpperCase();
    var monthValue = monthArray.indexOf(upperMonth);
    var birthValue = (monthValue + 9) % 12;
    alert("Congratulations, you will put to birth in " +     monthArray[birthValue]);
}
pregnancy();

也许您现在可以与您的代码进行比较并了解您的错误。