创建此代码时,我的目标是能够设置每种天气类型的独特特征,并能够通过生成随机数来随机设置一种天气。
我创建了11种不同的天气类型,使它们成为Weather的所有对象。然后,我做了一个getWeather函数,该函数将允许我获取一个随机的天气类型,然后将其独特的特性(id,type,healthChange,mileChange)保存到Weather。但是,它似乎不起作用。我认为该错误是由于我的随机天气函数造成的,我认为一旦获得随机数,我对下一步的工作没有正确的认识。有人可以帮我吗?我还认为,这还不止于此。我不确定weather(veryHot)是否在做什么。我想要做的是允许我随机选择天气,并获取它的变量,以便我可以操纵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);
}
}
答案 0 :(得分:1)
将对象放入数组,然后根据数组的长度生成一个随机数。现在,如果您添加或删除天气对象,则无需进行任何更改。
function Weather(id,type,healthChange,mileChange,probability) {
this.id = id;
this.type = type;
this.healthChange = healthChange;
this.mileChange = mileChange;
this.probability = probability;
}
var arrWeather = [new Weather("1", "Very Hot","-9",".7",".1"),
new Weather("2", "Hot","-3",".9",".1"),
new Weather("3", "Warm","1","1",".2"),
new Weather("4", "Cool","1",".95",".1"),
new Weather("5", "Cold","-5",".8",".1"),
new Weather("6", "Very Cold","-12",".7",".1"),
new Weather("1", "Rain","-4",".6",".1"),
new Weather("1", "Heavy Rain","-8",".4",".05"),
new Weather("1", "Snow","-15",".3",".05"),
new Weather("1", "Blizzard","-30",".1",".05"),
new Weather("1", "Heavy Fog","-3",".5",".05")];
function getRandomWeather() {
var randomNumber = Math.floor(Math.random() * arrWeather.length);
return arrWeather[randomNumber];
}
var weather = getRandomWeather();
console.log(weather);
//Lets populate the HTML
for (var property in weather) {
if (weather.hasOwnProperty(property)) {
document.querySelector("#weather ." + property).innerHTML = weather[property];
}
}
<div id="weather">
<h2 class="type"></h2>
<dl>
<dt>ID</dt>
<dd class="id"></dd>
<dt>Health Change</dt>
<dd class="healthChange"></dd>
<dt>Mile Change</dt>
<dd class="mileChange">
<dt>Probability</dt>
<dd class="probability"></dd>
</dl>
</div>
答案 1 :(得分:0)
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;
}
}