2015-02-06 59 views
-3

我有一个前面的问题,但我得到它现在我有一个新的问题下面是代码。Javascript天气计划

<!DOCTYPE html> 
 
<html lang="en"> 
 
<title>Weather</title> 
 
<head> 
 
<script> 
 
\t 
 
\t var temp = prompt("Enter the temp from outside"); 
 
\t var sky = prompt("tell us what it is like outside put ether sun, rain, snow (it is case sensitive so follow orders.)"); 
 

 
\t function getHot(temp) { 
 
\t \t if (temp >= 100) { 
 
\t \t document.write ("Bet u wish you could go naked but shorts and T-shirt will do."); 
 
\t \t } 
 
\t \t else if (temp >= 60) { 
 
\t \t document.write ("Ok thats a bit better but i would say shorts and T-shirt would be good.") 
 
\t \t } 
 
\t \t else if (temp >= 40){ 
 
\t \t document.write ("Getting a bit nippy out maybe get some pants and a jacket.") 
 
\t \t } 
 
\t \t else if (temp >= 0){ 
 
\t \t document.write ("Sucks to be you right now put on you big boy pants get a sweater and put a heavy coat on.") 
 
\t \t } 
 
\t \t else if (temp <= -1){ 
 
\t \t document.write ("Stay inside you fool theres no need to freeze to death.") 
 
\t \t } 
 
\t \t else { 
 
\t \t document.write ("you mess with me i mess with you refresh to do this right.") 
 
\t \t } 
 
\t \t } 
 
\t function getLight(sky) { 
 
\t if (sky = sun){ 
 
\t document.write ("Enjoy the light get out your house it's good for you inless it's to hot or to cold then hide from the light.") 
 
\t } 
 
\t else if (sky = rain){ 
 
\t document.write ("Can't beleave you have to ask this but get a umbrella and maybe a poncho.") 
 
\t } 
 
\t else if (sky = snow){ 
 
\t document.write ("If you are afraid of getting wet then use a umbrella other then that bundle up.") 
 
\t } 
 
\t else { 
 
\t document.write ("not going to tell you again follow what i say refresh and do it again.") 
 
\t } 
 
\t } 
 
\t 
 
\t getHot(temp); 
 
\t getLight(sky); 
 
\t 
 
</script> 
 
</head> 
 
</html>

确定,所以它会提示输入2个输入用户但随后只显示用于临时输入信息,我需要它同时显示任何建议?

+2

不要使用'document.write',请参阅[规范(http://www.w3.org/警告TR/HTML5/dom.html#文件撰写%28%29)。改用DOM方法。 – Oriol 2015-02-07 00:01:04

+0

您能否详细说明我们还没有在我的课程中介绍DOM? – 2015-02-07 00:03:23

+0

'sky = snow'应该是'sky == snow'' – 2015-02-07 00:06:07

回答

0

你有2个问题:

首先,您需要使用等于比较运算符(可变==值)而不是一个等号,它只是更新变量的值

第二,你需要把你的字符串(太阳,雪等)放在引号中,以便JavaScript知道把这些值当作严格gs,否则它会假设这些是变量名称。

var temp = prompt("Enter the temp from outside"); 
var sky = prompt("tell us what it is like outside put ether sun, rain, snow (it is case sensitive so follow orders.)"); 
function getHot(temp) { 
    if (temp >= 100) { 
    alert("Bet u wish you could go naked but shorts and T-shirt will do."); 
    } 
    else if (temp >= 60) { 
    alert("Ok thats a bit better but i would say shorts and T-shirt would be good.") 
    } 
    else if (temp >= 40){ 
    alert("Getting a bit nippy out maybe get some pants and a jacket.") 
    } 
    else if (temp >= 0){ 
    alert("Sucks to be you right now put on you big boy pants get a sweater and put a heavy coat on.") 
    } 
    else if (temp <= -1){ 
    alert("Stay inside you fool theres no need to freeze to death.") 
    } 
    else { 
    alert("you mess with me i mess with you refresh to do this right.") 
    } 
    } 
function getLight(sky) { 
if (sky == "sun"){ 
alert("Enjoy the light get out your house it's good for you inless it's to hot or to cold then hide from the light.") 
} 
else if (sky == "rain"){ 
alert("Can't beleave you have to ask this but get a umbrella and maybe a poncho.") 
} 
else if (sky = "snow"){ 
alert("If you are afraid of getting wet then use a umbrella other then that bundle up.") 
} 
else { 
alert("not going to tell you again follow what i say refresh and do it again.") 
} 
} 

getHot(temp); 
getLight(sky); 

下面是的jsfiddle一个工作版本的链接:http://jsfiddle.net/6Lhm0u91/2/

+0

感谢您接受评分 – 2015-02-07 01:58:38

1

下面的几个主要的东西过目:

  • 文件撰写是很危险的,你应该写DOM 代替。看看这个问题从SO:What is the correct way to write HTML using Javascript?。如果你还没有覆盖DOM,我现在不会担心它。如果您的老师告诉您,请使用document.write。只知道它在现实世界中不适合。

  • 您的getLight函数使用的是赋值运算符,而不是 比较运算符。说sky = sun是相当于说 '设置变量天空的变量太阳的价值(这导致 到下一个点)。您需要使用比较运算符sky === sun

  • 你“getLight”是sky变量的值进行比较不是字符串'sun''rain',而是命名为sun未定义的变量,`雨,等你需要确保你的包裹你的字符串加引号。

因此,所有的一切,它应该像这样的:

if (sky === 'sun'){ 
    //output is a DOM element. See the link above on how to access it, or just use document.write if thats what your teacher wants. 
     output.innerHTML = "Enjoy the light get out your house it's good for you inless it's to hot or to cold then hide from the light." 
    }