2016-12-19 4 views
0

私は総人口が特定の数に達したときに単語間を変更するためのコードを取得しようとしています。私はこれをしなかった理由は、ローカルストレージにcivnumの値を保存し、ユーザーが戻ってあなたがしたいと思うWebページ変数を使ってjavascriptで単語を更新する

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

+0

町の名の(civnumによって)ルックアップマップを作成します。 –

答えて

1

に来るとき、それを使用することができるようになりました最初の関数の順序を逆にして、より大きな都市サイズを最初に確認します。あなたが持っている方法では、それ以上のものについてはcivnum = 1(Halmet)を見つけるでしょう。残りはelseステートメントなので、決してそれらに到達しません。編集: 第2の関数は、 switchとしてきれいになります。 評価関数をクリーンアップするには、以下の@ connexoの答えを参照してください。

0

コメントはありませんがコメントは多すぎます。あなたのfunction evaluate()大幅アレイとそのインデックス(0から始まる、あなたは1を減算する必要がある理由のthats)を利用することによって短縮することができます。

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"; } 
関連する問題