首页 > 解决方案 > 我需要一周中的一天,然后一周中的其他 3 天包含功能

问题描述

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

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

标签: javascriptarrays

解决方案


你调用weekdays[dayOfWeek]你在循环之外定义的,所以它总是在同一天。这就是问题所在。


推荐阅读