【发布时间】:2012-09-07 20:17:54
【问题描述】:
我无法在此函数中返回字符串。对于初学者,我是一个一周前刚开始学习 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;
}
【问题讨论】:
-
我们需要查看您的其他功能脚本(
showDateTime()&getMap())来确定问题。 -
你没有正确关闭多行注释,所以你的代码被注释掉了......
-
不确定这是否是拼写错误,但您错误地关闭了评论。
-
假设你想返回 timeStr 只需添加 return timeStr;到函数底部
标签: javascript string function return