在javascript函数中返回字符串时遇到问题

时间:2012-09-14 16:19:45

标签: javascript string function return

我在此函数中返回字符串时遇到问题。对于初学者来说,我是一个刚刚开始学习javascript的初学者,如果我说错了,请提前抱歉。我被告知声明一个名为timeStr的变量等于从showDate()函数返回的值以及mapNum到getMap()函数。 showDateTime()和getMap()函数都在我正在访问的文件中,名为datetime.js。我想知道我犯错误的地方以及如何纠正错误,谢谢。

<script src="datetime.js" type="text/javascript"></script>
<script type="text/javascript">
function test(){
/*
   timeStr is a text string containing the current date and time
   mapNum is the number of the map to display in the planisphere
/*
var timeStr = showDateTime();
var mapNum = getMap();
}
</script>

更新:datetime.js位于:

/*
   New Perspectives on JavaScript, 2nd Edition
   Tutorial 1
   Case Problem 1

   Function List:
   showDate
      Used to return a text string containing the current date and time.
   getMap
      Used to the determine the current sky map number to display with the online        planisphere

*/


function showDateTime() {
   var thisDate = new Date();
   var thisWDay=thisDate.getDay();
   var thisDay=thisDate.getDate();
   var thisMonth=thisDate.getMonth();
   var thisYear=thisDate.getFullYear();
   var mName = new Array("January", "February", "March", "April", "May", 
       "June", "July", "August", "September", "October","November", "December");
   var hours=thisDate.getHours();
   var minutes=thisDate.getMinutes();
   ampm = hours >=12 ? " pm" : " am";
   hours = hours > 12 ? hours-12 : hours;
   minutes = minutes < 10 ? "0"+minutes : minutes;
   return mName[thisMonth]+" "+thisDay+", "+thisYear + ", " + hours + ":" + minutes + ampm;
}

function getMap() {
   thisTime = new Date();
   hour = thisTime.getHours();
   month = thisTime.getMonth();
   mapNumber = (month*2+hour)%24;
   return mapNumber;
}

3 个答案:

答案 0 :(得分:4)

你已经评论了所有内容。

更改

/*
   timeStr is a text string containing the current date and time
   mapNum is the number of the map to display in the planisphere
/*

/*
   timeStr is a text string containing the current date and time
   mapNum is the number of the map to display in the planisphere
*/

答案 1 :(得分:0)

正如你可以通过语法高亮显示看到的那样,你还没有正确终止你的评论。

/*开始多行注释,*/结束。

答案 2 :(得分:0)

您的评论应该以{{1​​}}而不是*/结尾 如果你看一下问题中突出显示的语法,你会发现它都是灰色的。