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/
相关文章:
- Rails引擎:使主机应用程序javascript可用于Mounted Engine
- Windows 8应用程序Javascript和SQlite(数据库已锁定)
- 以下 Chrome 扩展程序 JavaScript 代码片段究竟是如何工作的
- 是否有一种从应用程序Javascript代码中自动生成序列图的方法
- <嵌入>或者<对象>标签视频播放错误处理程序-JavaScript
- 未捕获的语法错误:) 在 MVC 应用程序 JavaScript asp.net 参数列表后丢失
- 内联委托事件处理程序 Javascript
- 如何将excel文件导入Web应用程序(javascript/d3/html)
- 目录扫描程序JavaScript HTML
- 检查是否没有Internet连接-弹出-Windows 10应用程序(Javascript)
- 如何在WinRT应用程序(Javascript、C#)中区分Windows Phone 8.1和Windows 8.1
- jQuery Mobile Windows应用商店应用程序:JavaScript运行时错误:无法获取属性'ind
- 移动设备中的引导程序JavaScript问题
- Web应用程序Javascript,用于处理来自iOS设备摄像头的信息流(不是应用程序!)
- 在构建应用程序Javascript MVC的过程中,score/buildjs抛出了一个没有解释的错误
- Iframe应用程序JavaScript SDKpublish对话框错误(代码102)
- 如何在windows8商店应用程序(javascript)中播放youtube视频
- AJAX应用程序JavaScript加载问题
- 为什么获胜't我的引导程序javascript在本地运行
- 用于在纯客户端测试应用程序(javascript/jquery)中维护状态和管理i/o的体系结构