Javascript天气程序

Javascript天气程序,javascript,Javascript,我有一个以前的问题,但我得到了修复,现在我有一个新的问题,下面是代码 天气 var temp=从外部输入温度; var sky=promptell告诉我们外面是什么样的太阳、雨、雪它是区分大小写的,所以遵循命令。; 函数getHottemp{ 如果温度>=100{ 写下你希望你能裸体,但短裤和T恤就行了。; } 否则,如果温度>=60{ 好的,但我想说短裤和T恤衫就好了。 } 否则,如果温度>=40{ 记录。写一篇有点紧张的文章,也许买条裤子和夹克。 } 如果温度>=0,则为else{ 现在你

我有一个以前的问题,但我得到了修复,现在我有一个新的问题,下面是代码

天气 var temp=从外部输入温度; var sky=promptell告诉我们外面是什么样的太阳、雨、雪它是区分大小写的,所以遵循命令。; 函数getHottemp{ 如果温度>=100{ 写下你希望你能裸体,但短裤和T恤就行了。; } 否则,如果温度>=60{ 好的,但我想说短裤和T恤衫就好了。 } 否则,如果温度>=40{ 记录。写一篇有点紧张的文章,也许买条裤子和夹克。 } 如果温度>=0,则为else{ 现在你真是太差劲了穿上你的大男孩裤穿上毛衣穿上厚外套。 }
否则,如果温度这里有一些主要的事情要看:

document.write很危险,您应该向DOM写入 相反。请从SO处查看以下问题:。如果您还没有涉及DOM,我现在就不担心它。如果您的老师告诉您,请使用document.write。只需知道这在现实世界中是不合适的

getLight函数正在使用赋值运算符,而不是 比较运算符。表示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);

这里有一个指向JSFIDLE中工作版本的链接:

不要使用document.write,请参阅中的警告。请改用DOM方法。您能告诉我我们还没有在我的类中介绍DOM吗。sky=snow应该是sky==snow@Oriol我不同意,你应该对document.write保持警惕,但如果你知道它是如何工作的,它有着完全有效的用途你不应该使用它。谢谢你标记为已接受