童子军需要Javascript编码的帮助

时间:2017-03-11 01:00:33

标签: javascript

我是一个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文档,&#34;

<!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个小时,没有取得任何进展,并且已经筋疲力尽了其他任何我可以得到答案的地方。我知道代码布局非常糟糕,但我甚至不知道如何做到这一点。

1 个答案:

答案 0 :(得分:0)

我没有完整的答案atm,但提示基本上要求你做的是添加第二个文本框,如温度允许一个人输入风寒的文本框。

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

HTML文档中的那些^。

然后,在javascript文件中,它希望您使用以下内容引用和存储风寒值:

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

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