Javascript JS允许我设置天气类型

Javascript JS允许我设置天气类型,javascript,html,Javascript,Html,创建此代码时,我的目标是能够设置每个天气类型的唯一特征,并能够通过生成随机数随机设置一种天气类型 我创造了11种不同的天气类型,使它们都成为天气的对象。然后我制作了一个getWeather函数,它允许我获取一个随机的天气类型,然后将其唯一的特征(id、type、healthChange、mileChange)保存到weather。然而,它似乎不起作用。我相信这个错误是通过我的随机天气函数造成的,我认为一旦我得到了随机数,我就不知道该怎么做了。有人能帮我一下吗?我也认为这比我所拥有的更多。我不确定

创建此代码时,我的目标是能够设置每个天气类型的唯一特征,并能够通过生成随机数随机设置一种天气类型

我创造了11种不同的天气类型,使它们都成为天气的对象。然后我制作了一个getWeather函数,它允许我获取一个随机的天气类型,然后将其唯一的特征(id、type、healthChange、mileChange)保存到weather。然而,它似乎不起作用。我相信这个错误是通过我的随机天气函数造成的,我认为一旦我得到了随机数,我就不知道该怎么做了。有人能帮我一下吗?我也认为这比我所拥有的更多。我不确定天气是否在起作用。我想让它做的是允许我随机选择天气,并得到它的变量,这样我就可以操纵我的HTML

function Weather(id,type,healthChange,mileChange,probability) {
    this.id = id;
    this.type = type;
    this.healthChange = healthChange;
    this.mileChange = mileChange;
    this.probability = probability;
}
veryHot = new Weather("1", "Very Hot","-9",".7",".1");
hot = new Weather("2", "Hot","-3",".9",".1");
warm = new Weather("3", "Warm","1","1",".2");
cool = new Weather("4", "Cool","1",".95",".1");
cold = new Weather("5", "Cold","-5",".8",".1");
veryCold = new Weather("6", "Very Cold","-12",".7",".1");
rain = new Weather("1", "Rain","-4",".6",".1");
heavyRain = new Weather("1", "Heavy Rain","-8",".4",".05");
snow = new Weather("1", "Snow","-15",".3",".05");
blizzard = new Weather("1", "Blizzard","-30",".1",".05");
heavyFog = new Weather("1", "Heavy Fog","-3",".5",".05");

function getWeather() {
    var randomNumber = Math.floor(Math.random() * 100);

    if(randomNumber <= 10 ) {
        weather(veryHot);
    }
    if((randomNumber > 10) && (randomNumber <= 20)) {
        weather(hot);
    }
    if((randomNumber > 20) && (randomNumber <= 40)) {
        weather(warm);
    }
    if((randomNumber > 40) && (randomNumber <= 50)) {
        weather(cool);
    }
    if((randomNumber > 50) && (randomNumber <= 60)) {
        weather(cold);
    }
    if((randomNumber > 60) && (randomNumber <= 70)) {
        weather(veryCold);
    }
    if((randomNumber > 70) && (randomNumber <= 80)) {
        weather(rain);
    }
    if((randomNumber > 80) && (randomNumber <= 85)) {
        weather(heavyRain);
    }
    if((randomNumber > 85) && (randomNumber <= 90)) {
        weather(snow);
    }
    if((randomNumber > 90) && (randomNumber <= 95)) {
        weather(blizzard);
    }
    if((randomNumber > 95) && (randomNumber <= 100)) {
        weather(heavyFog);
    }
}
功能天气(id、类型、健康变化、里程、概率){
this.id=id;
this.type=type;
this.healthChange=healthChange;
this.mileChange=mileChange;
这个概率=概率;
}
veryHot=新天气(“1”、“非常热”、“-9”、“0.7”、“0.1”);
炎热=新天气(“2”、“炎热”、“3”、“9”、“1”);
温暖=新天气(“3”、“温暖”、“1”、“1”、“2”);
凉爽=新天气(“4”、“凉爽”、“1”、“0.95”、“0.1”);
寒冷=新天气(“5”、“寒冷”、“-5”、“0.8”、“0.1”);
veryCold=新天气(“6”、“非常冷”、“-12”、“0.7”、“0.1”);
雨=新天气(“1”、“雨”、“-4”、“0.6”、“0.1”);
天雨=新天气(“1”、“大雨”、“-8”、“0.4”、“0.05”);
雪=新天气(“1”、“雪”、“-15”、“0.3”、“0.05”);
暴雪=新天气(“1”、“暴雪”、“-30”、“0.1”、“0.05”);
重雾=新天气(“1”、“大雾”、“-3”、“0.5”、“0.05”);
函数getWeather(){
var randomNumber=Math.floor(Math.random()*100);
if(随机数10)&(随机数20)&&(随机数40)&(随机数50)&(随机数60)&(随机数70)&(随机数80)&(随机数85)&(随机数90)&(随机数95)&(随机数
函数天气(id、类型、健康变化、米勒换、概率){
this.id=id;
this.type=type;
this.healthChange=healthChange;
this.mileChange=mileChange;
这个概率=概率;
}
veryHot=新天气(“1”、“非常热”、“-9”、“0.7”、“0.1”);
炎热=新天气(“2”、“炎热”、“3”、“9”、“1”);
温暖=新天气(“3”、“温暖”、“1”、“1”、“2”);
凉爽=新天气(“4”、“凉爽”、“1”、“0.95”、“0.1”);
寒冷=新天气(“5”、“寒冷”、“-5”、“0.8”、“0.1”);
veryCold=新天气(“6”、“非常冷”、“-12”、“0.7”、“0.1”);
雨=新天气(“1”、“雨”、“-4”、“0.6”、“0.1”);
天雨=新天气(“1”、“大雨”、“-8”、“0.4”、“0.05”);
雪=新天气(“1”、“雪”、“-15”、“0.3”、“0.05”);
暴雪=新天气(“1”、“暴雪”、“-30”、“0.1”、“0.05”);
重雾=新天气(“1”、“大雾”、“-3”、“0.5”、“0.05”);
函数getWeather(){
var randomNumber=Math.floor(Math.random()*100);

如果(随机数10)&(随机数20)&(随机数40)&(随机数50)&(随机数60)&(随机数70)&(随机数80)&(随机数85)&(随机数90)&(随机数95)&(randomNumber将对象放入一个数组,然后根据数组的长度生成一个随机数。现在,如果添加或删除天气对象,则无需更改任何内容

功能天气(id、类型、健康变化、里程、概率){
this.id=id;
this.type=type;
this.healthChange=healthChange;
this.mileChange=mileChange;
这个概率=概率;
}
var arrWeather=[新天气(“1”、“酷热”、“-9”、“0.7”、“0.1”),
新天气("二","炎热","三","九","一",,
新天气(“3”、“温暖”、“1”、“1”、“2”),
新天气(“4”、“凉爽”、“1”、“0.95”、“0.1”),
新天气(五,"冷","五","八","一"),,
新天气(“6”、“非常冷”、“-12”、“0.7”、“0.1”),
新天气("1","雨","4","6","1",,
新天气("1","大雨","-8","0.4","0.05",,
新天气("一","雪","十五","三","零五",,
新天气(“1”、“暴雪”、“-30”、“0.1”、“0.05”),
新天气("一级,"大雾,"三级,"五级,"零五级);;
函数getRandomWeather(){
var randomNumber=Math.floor(Math.random()*arrWeather.length);
返回天气[随机数];
}
var weather=getRandomWeather();
控制台.日志(天气);
//让我们填充HTML
for(天气中的var属性){
if(weather.hasOwnProperty(property)){
document.querySelector(“#weather.+property).innerHTML=weather[property];
}
}

身份证件
健康变化
换车里程
可能性

您何时调用getWeather函数?函数weather需要5个参数,我不确定您是否可以像这样传递天气。将天气实例存储到数组中,而不是单独的变量,并创建一个介于0和weatherArray.length-1之间的随机整数。这样,您可以从数组中随机获取天气。@teemu和每个weather实例中所有独特的东西呢?它们仍然存在于您正在创建的对象中,只是您存储对象的方式发生了变化。weather函数在做什么。我尝试了
function weather(weather){console.log(weather)}
并且它工作得非常好。我们可以知道所需的输出吗?嘿@lixas现在当您说“return veryHot”,它实际返回到哪里,以及在返回后如何访问它。我很感激您的回答。我不知道您将如何使用此函数,但在浏览器控制台中调用函数getWeather()时,它将返回
Weather{id:“3”,键入:“Warm”,healthChange:“1”,mileChange:“1”,probability:.2}
function Weather(id,type,healthChange,mileChange,probability) {
    this.id = id;
    this.type = type;
    this.healthChange = healthChange;
    this.mileChange = mileChange;
    this.probability = probability;
}
veryHot = new Weather("1", "Very Hot","-9",".7",".1");
hot = new Weather("2", "Hot","-3",".9",".1");
warm = new Weather("3", "Warm","1","1",".2");
cool = new Weather("4", "Cool","1",".95",".1");
cold = new Weather("5", "Cold","-5",".8",".1");
veryCold = new Weather("6", "Very Cold","-12",".7",".1");
rain = new Weather("1", "Rain","-4",".6",".1");
heavyRain = new Weather("1", "Heavy Rain","-8",".4",".05");
snow = new Weather("1", "Snow","-15",".3",".05");
blizzard = new Weather("1", "Blizzard","-30",".1",".05");
heavyFog = new Weather("1", "Heavy Fog","-3",".5",".05");

function getWeather() {
    var randomNumber = Math.floor(Math.random() * 100);

    if(randomNumber <= 10 ) {
        return veryHot;
    }
    if((randomNumber > 10) && (randomNumber <= 20)) {
        return hot;
    }
    if((randomNumber > 20) && (randomNumber <= 40)) {
        return warm;
    }
    if((randomNumber > 40) && (randomNumber <= 50)) {
        return cool;
    }
    if((randomNumber > 50) && (randomNumber <= 60)) {
        return cold;
    }
    if((randomNumber > 60) && (randomNumber <= 70)) {
        return veryCold;
    }
    if((randomNumber > 70) && (randomNumber <= 80)) {
        return rain;
    }
    if((randomNumber > 80) && (randomNumber <= 85)) {
        return heavyRain;
    }
    if((randomNumber > 85) && (randomNumber <= 90)) {
        return snow;
    }
    if((randomNumber > 90) && (randomNumber <= 95)) {
        return blizzard;
    }
    if((randomNumber > 95) && (randomNumber <= 100)) {
        return heavyFog;
    }
}