2015-02-06 61 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." 
    }