Javascript天气程序

Javascript weather program

本文关键字:程序 Javascript      更新时间:2023-09-26

我以前有一个问题,但现在已经修复了。下面是一个新问题。

<!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>

好的,所以它会提示用户输入2个输入,但它只显示临时输入的信息,我需要它来显示这两个信息,有什么建议吗?

以下是一些需要查看的主要内容:

  • document.write很危险,您应该向DOM写入相反看看SO的这个问题:正确的方法是什么使用Javascript编写HTML?。如果您还没有介绍DOM,我现在还不担心。如果你的老师告诉你使用document.write。只要知道它在现实世界中是不合适的。

  • 您的getLight函数使用的是赋值运算符,而不是比较运算符。说sky = sun等于说"将可变天空设置为可变太阳的值"(这导致到下一点)。您需要使用比较运算符sky === sun

  • 您的"getLight"不是将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."
    }

您有两个问题:

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

其次,您需要将字符串(sun、snow等)放在引号中,以便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/