使用变量更新javascript中的单词

时间:2016-12-19 19:15:43

标签: javascript html

我试图获取我在总人口达到一定数量时在单词之间进行更改的代码。我这样做的原因是能够将civnum的值保存到本地存储中,然后在用户返回网页时使用它



var civnum = +localStorage.getItem('civSave');
var lumberJak = 0;
var soldier = 0;
var hunter = 0;
var breeder = 0;
var population = 0;
var totalPop = lumberJak; 
var totalPop = totalPop + soldier;
var totalPop = totalPop + hunter;
var totalPop = totalPop + breeder;
var totalPop = totalPop + population;
var townOut = document.getElementById('townName');

setInterval(function() {
	if(totalPop >= 250){
		civnum = 1;
	}else if(totalPop >= 1000){
		civnum = 2;
	}else if(totalPop >= 5000){
		civnum = 3;
	}else if(totalPop >= 10000){
		civnum = 4;
	}else if(totalPop >= 100000){
		civnum = 5;
	}else if(totalPop >= 500000){
		civnum = 6;
	}else if(totalPop >= 1000000){
		civnum = 7;
	}else if(totalPop >= 10000000){
		civnum = 8;
	}else if(totalPop >= 50000000){
		civnum = 9;
	}else if(totalPop >= 100000000){
		civnum = 10;
	}else if(totalPop >= 1000000000){
		civnum = 11;
	}else if(totalPop >= 5000000000){
		civnum = 12;
	}else if(totalPop >= 10000000000){
		civnum = 13;
	}else if(totalPop >= 100000000000){
		civnum = 14;
	}else if(totalPop >= 500000000000){
		civnum = 15;
	}else if(totalPop >= 1000000000000){
		civnum = 16;
	}else if(totalPop >= 10000000000000){
		civnum = 17;
	}else if(totalPop >= 50000000000000){
		civnum = 18;
	}else if(totalPop >= 100000000000000){
		civnum = 19;
	}else if(totalPop >= 1000000000000000){
		civnum = 20;
	}else if(totalPop >= 10000000000000000){
		civnum = 21;
	}else if(totalPop >= 100000000000000000){
		civnum = 22;
	}else if(totalPop >= 1000000000000000000){
		civnum = 23;
	}else if(totalPop >= 10000000000000000000){
		civnum = 24;
	}else{
		civnum = 0;
	}
function evaluate(){
	if(civnum === 1){
		output = townOut.textContent = "Halmet";
	}else if(civnum === 2){
		townOut.textContent = "small Village";
	}else if(civnum === 3){
		townOut.textContent = "Medium Village";
	}else if(civnum === 4){
		townOut.textContent = "Large Village";
	}else if(civnum === 5){
		townOut.textContent = "Small Town";
	}else if(civnum === 6){
		townOut.textContent = "Medium Town";
	}else if(civnum === 7){
		townOut.textContent = "Large Town";
	}else if(civnum === 8){
		townOut.textContent = "Small Kingdom";
	}else if(civnum === 9){
		townOut.textContent = "Medium Kingdom";
	}else if(civnum === 10){
		townOut.textContent = "Large Kingdom";
	}else if(civnum === 11){
		townOut.textContent = "Small City";
	}else if(civnum === 12){
		townOut.textContent = "Medium City";
	}else if(civnum === 13){
		townOut.textContent = "Large City";
	}else if(civnum === 14){
		townOut.textContent = "Small Capital";
	}else if(civnum === 15){
		townOut.textContent = "Medium Capital";
	}else if(civnum === 16){
		townOut.textContent = "Large Capital";
	}else if(civnum === 17){
		townOut.textContent = "Small Country";
	}else if(civnum === 18){
		townOut.textContent = "Medium Country";
	}else if(civnum === 19){
		tonwOut.textContent = "Large Country";
	}else if(civnum === 20){
		townOut.textContent = "Continent";
	}else if(civnum === 21){
		townOut.textContent = "Planet";
	}else if(civnum === 22){
		townOut.textContent = "Solar System";
	}else if(civnum === 23){
		townOut.textContent = "Galaxy";
	}else if(civnum === 24){
		townOut.textContent = "Intergalactic Empire";
	}else{
		document.getElementById("townName").textContent= "Thorpe";
	}
}
document.getElementById("townName").textContent = evaluate();
localStorage.setItem('civSave', civnum);
}, 1);

<p id="townName"></p>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

您想要颠倒第一个功能的顺序,首先检查更大的城市尺寸。你拥有它的方式,它会发现civnum = 1(Halmet)超过250;由于其余的是else陈述,它永远不会达到它们。编辑:第二个函数更清晰为switch要清理评估函数,请参阅下面的@ connexo答案。

答案 1 :(得分:0)

不是答案,但评论的代码太多了。通过使用数组及其索引(从0开始,这就是为什么你必须减去1),你的function evaluate()可以大大缩短:

var cities = [ "Halmet", "small Village", "Medium Village", "Large Village", "Small Town", "Medium Town", "Large Town", "Small Kingdom", "Medium Kingdom", "Large Kingdom", "Small City", "Medium City", "Large City", "Small Capital", "Medium Capital", "Large Capital", "Small Country", "Medium Country", "Large Country",  "Continent", "Planet", "Solar System", "Galaxy", "Intergalactic Empire" ];

if(civnum > 0 && civnum <= cities.length){ output = cities[civnum-1] }
else { document.getElementById("townName").textContent= "Thorpe"; }