gpt4 book ai didi

javascript - 使用变量更新 javascript 中的单词

转载 作者:行者123 更新时间:2023-12-03 05:24:06 24 4
gpt4 key购买 nike

我正在尝试获取我编写的代码,以便在总人口达到一定数量时在单词之间进行更改。我这样做的原因是能够将 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>

最佳答案

您需要颠倒第一个函数的顺序,首先检查较大的城市规模。按照你的方式,它会为超过 250 的任何内容找到 civnum = 1 (Halmet);由于其余的都是 else 语句,因此它永远不会到达它们。编辑: 第二个函数作为 开关会更干净。 要清理评估函数,请参阅下面@connexo的答案。

关于javascript - 使用变量更新 javascript 中的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41229500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com