【问题标题】:having problems returning a string in a javascript function [closed]在javascript函数中返回字符串时遇到问题[关闭]
【发布时间】: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


【解决方案1】:

您的评论应以*/ 结尾,而不是/* 如果您查看问题中突出显示的语法,您会发现它全部变灰。

【讨论】:

    【解决方案2】:

    正如您可能通过语法高亮看到的那样,您没有正确终止您的评论。

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

    【讨论】:

      【解决方案3】:

      您已将所有内容都注释掉了。

      改变

      /*
         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
      */
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-01-27
        • 1970-01-01
        • 2020-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多