我是一个14岁的童子军,试图做我的编程成绩徽章,而我在这项要求上受困,我们必须计算风寒?我什至不了解我正在做的事情的背景。这是工作分配的问题,“添加另一个文本输入–例如,询问风速。5.添加一些评估风寒系数的条件语句。6.添加一些文本以显示风寒结果。 ”这是代码,

// JavaScript Document
// Note: lines that start with two backslashes are comments - not code!
var bePrepared = function () {
    // = = = = = = = declare all the variables = = = = = = = =
    var tempF, tempC, myActionText, newText;
    // get the temp (F) from the HTML page
    tempF = document.getElementById('MyInputTemp').value;
    // = = = = = = = convert the temp to Celsius (with only one decimal place)
    tempC = (5 / 9 * (tempF - 32)).toFixed(1);
    // = = = = = = = evaluate the temp (three categories) = = = = = = =
    if (tempF < 60) {
         myActionText = " Take long-johns!";
    }
    if ((tempF >= 60) && (tempC < 75)) {
         myActionText = " Have Fun!";
    }
    if (tempF >= 75) {
         myActionText = " Take Sunscreen!";
    }
    // = = = = = = = build a complete sentence = = = = = = =
    newText = "If the temperature is " + tempF + " &deg;F (" + tempC + " &deg;C): " + myActionText;
    // push the sentence back to the HTML page (using the ID of the markup element: 'myAnswer')
    document.getElementById('myAnswer').innerHTML = newText;
 };


这是一些html文档,可打开浏览器窗口,

<!DOCTYPE html>
<html>
<head>
  <title>Example Javascript Program for Boy Scout Merit Badge</title>
  <script src='JS-Example.js' type='text/javascript'></script>
</head>
<body>
  <h1>Javascript Programming Example</h1>
  <h2>Enter Temperature (&deg;F):
     <input type="text" id="MyInputTemp"/>
     <input type="button" value="Go!" onclick="bePrepared();"/>
  </h2>
  <h3 id="myAnswer"></h3>
  </body>
  </html>"


对此方法的任何帮助将不胜感激,因为我已经花了2个小时,没有取得任何进展,并且已经筋疲力尽了才能获得答案。我知道代码的布局非常糟糕,但是我什至不知道该怎么做。

最佳答案

我没有完整的答案atm,但是提示基本上希望您执行的操作是添加第二个文本框,例如一个用于温度的文本框,以允许人们输入风寒。

<input type="text" id="MyInputWindChill" />


HTML文档中的类似^的内容。

然后,在javascript文件中,它希望您使用如下所示的内容来引用和存储风冷值:

var windChill;
windChill = document.getElementById("MyInputWindChill").value;


然后,它希望使用温度和冷风值,使用if语句输出适当的响应。您可以在已经提供给您的代码中看到执行此操作的基本方法。

09-19 14:05