我需要一周中的某一天,然后包含一周中的其他三天

时间:2019-06-26 21:11:50

标签: javascript arrays

我需要使用mathrandom显示当天的最高和最低温度,然后包括一周中其余时间的温度。但是,当我输入剩余一周的温度函数时,我只会得到星期四的列表……我不确定自己做错了什么。

// display the forecast
var elem = document.getElementById("moncForecast");
`enter code here`displayForecast(elem);

/*
Create a new Date object for each of the 5 forecasts, then extract the day of the
week and display it.  You can change the number of forecasts to whatever suits your needs.
*/
function displayForecast(divElem, currentDate)
{



   for (var i = 1; i <= 1; i++)
   {
     var currentDate= new Date ();
     var elem = document.getElementById ("displayDate")
      var forecast = getDateForForecast(currentDate, i);
      dayOfWeek = forecast.getDay();
      divElem.innerHTML = currentDate + " Low: " + getLowTemp() + " High: " + getHighTemp() + "<br>";

      for (var i=1; i <=5; i++) {
      divElem.innerHTML += weekdays[dayOfWeek] + " Low: " + getLowTemp () + "High" + getHighTemp () + "<br>";


   }
}

}

/*
Return a low temperature using the Math.random method.
*/

function getLowTemp()
{
   return  Math.floor(Math.random() * 5) + 10;


}

/*
Return a high temperature using the Math.random method.
*/
function getHighTemp()
{
   return  Math.floor(Math.random() * 10) + 15 ;

}

这是原始代码,希望您能够向我展示我哪里出错了,因为我无法弄清楚。

 // display Winnipeg date and time
var wpgTime = getDateByOffset(-5);
var elem = document.getElementById("wpgTime");
elem.innerHTML = wpgTime;

// display the forecast
var elem = document.getElementById("wpgForecast");
displayForecast(elem, wpgTime);

// display Toronto date and time
var torTime = getDateByOffset(-4);
elem = document.getElementById("torTime");
elem.innerHTML = torTime;

// display the forecast
elem = document.getElementById("torForecast");
displayForecast(elem, torTime);

/*
Create a new Date object for each of the 5 forecasts, then extract the day of the
week and display it.  You can change the number of forecasts to whatever suits your needs.
*/
function displayForecast(divElem, currentDate)
{
   for (var i = 1; i <= 3; i++)
   {
      var forecast = getDateForForecast(currentDate, i);
      dayOfWeek = forecast.getDay();
      divElem.innerHTML += weekdays[dayOfWeek] + ":" + getLowTemp() + getHighTemp() + "<br>";
   }
}

/*
Return a low temperature using the Math.random method.
*/

function getLowTemp()
{
   return "<span style='margin-left:15px'>Low 0&deg;C </span>";
}

/*
Return a high temperature using the Math.random method.
*/
function getHighTemp()
{
   return "<span style='margin-left:25px'>High 0&deg;C </span>";
}

以下是html代码。

 <html lang = "en">
<head>
  <title>What's the weather?</title>
  <meta charset = "UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Lato&display=swap" rel="stylesheet">
  <link href="css/styles.css" rel="stylesheet">

</head>

<body>

  <div class="main-image">
    <div class="header">
    <h1> What's the Weather? | </h1>
  </div>
    <div id="showDate"> </div>
      <div id ="nav">
          <a href="index.html">Home</a>
  <a href="news.html">News</a>
  <div class="dropdown">
    <button class="dropbtn">Weather
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content">
      <a href="winnipeg.html">Winnipeg</a>
      <a href="victoria.html">Victoria</a>
      <a href="moncton.html">Moncton</a>
    </div>
  </div>
</div>


<body style="font-family:Arial">

  <div class="weather">
     <h3>Winnipeg Weather</h3>

      <!-- Vancouver -->
      <p>Time</p>
      <div id="moncTime"></div>

      <p style="font-weight:bold">Winnipeg forecast:</p>
      <div id="moncForecast"></div>



  </div>

   <script src="example11-09a.js"></script>
   <script src="moncton.js"></script>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

您调用在循环之外定义的weekdays[dayOfWeek],因此它将始终是同一天。这就是问题。

答案 1 :(得分:0)

您代码中的某些内容给我带来了一些标志。

首先,我相信您已经创建了一个全局变量和一个本地变量。可能是全局变量被第二个变量覆盖。

第二,我实际上没有得到第一个For循环“ for(var i = 1; i <= 1; i ++)”。尽管输入了参数,但您只有一次交互。我相信这可能是您的问题,因为您不是每个工作日都经过这一次。

最后,您通过该函数收到了currentDate,但是您用今天的日期覆盖了它,这可能每次都导致相同的结果。另外,如果您对JavaScript日期格式有任何疑问,也可以在此处查看:How to format a JavaScript date