Javascript天气程序

时间:2015-02-06 23:57:56

标签: javascript

我之前遇到过这个问题,但我现在已经解决了这个问题,我现在有一个新问题是代码。



<!DOCTYPE html>
<html lang="en">
<title>Weather</title>
<head>
<script>
	
	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) {
		document.write ("Bet u wish you could go naked but shorts and T-shirt will do.");
		}
		else if (temp >= 60) {
		document.write ("Ok thats a bit better but i would say shorts and T-shirt would be good.")
		}
		else if (temp >= 40){
		document.write ("Getting a bit nippy out maybe get some pants and a jacket.")
		}
		else if (temp >= 0){
		document.write ("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){
		document.write ("Stay inside you fool theres no need to freeze to death.")
		}
		else {
		document.write ("you mess with me i mess with you refresh to do this right.")
		}
		}
	function getLight(sky) {
	if (sky = sun){
	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.")
	}
	else if (sky = rain){
	document.write ("Can't beleave you have to ask this but get a umbrella and maybe a poncho.")
	}
	else if (sky = snow){
	document.write ("If you are afraid of getting wet then use a umbrella other then that bundle up.")
	}
	else {
	document.write ("not going to tell you again follow what i say refresh and do it again.")
	}
	}
	
	getHot(temp);
	getLight(sky);
	
</script>
</head>
</html>
&#13;
&#13;
&#13;

好的,所以它会提示用户输入2个输入但是它只显示临时输入的信息,我需要它来显示任何建议吗?

2 个答案:

答案 0 :(得分:1)

以下是一些需要关注的重要事项:

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

  • 您的getLight函数正在使用赋值运算符,而不是 比较运算符。说sky = sun相当于说 &#39;将变量sky设置为变量sun的值&#39; (这导致 到下一点)。您需要使用比较运算符sky === sun

  • 你的&#39; getLight&#39;是将sky变量的值与字符串'sun''rain'进行比较,而是将未定义的变量(sun,`rain等)进行比较。您需要确保自己是用引号括起你的字符串。

总而言之,它应该类似于:

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."
    }

答案 1 :(得分:0)

你有两个问题:

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

其次,您需要将字符串(太阳,雪等)放在引号中,以便JavaScript知道将这些值视为字符串,否则它会假定它们是变量名。

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/