2012-12-31 35 views
0

經過大量修改後的項目(感謝他們的每一個輸入) 我仍然無法循環我的automateLottery(); 我的控制檯不斷告訴我 -循環獲取 - document.getElementById(「random_0」+ i).value = rnd;

類型錯誤:的document.getElementById(...)爲空 的document.getElementById( 「random_0」 + I)。價值= RND;

我已經把我的項目上http://jsfiddle.net/Ulleruller/2q2ne/17/

任何機構可以讓我知道這意味着什麼?

比我能完成它 -

T'anks

var match3 = 0, match4 = 0, match5 = 0, match6 = 0; 
var startLottery = false; 
var numberOfWeeks = 0; 
var numberOfMatches = 0; 
var totalWinnings = 0; 
var totalMatchedThisRound=0; 
var matchedNumbers = 0; 

var addUpMatches = function(matched){ 
matchedNumbers = matchedNumbers + matched;//---add up amount of matched numbers 
totalMatchedThisRound = matchedNumbers; // ----store in new variable for 
}; 

var declareMatches = function(){//----declare matched numbers and keep totals 
switch(totalMatchedThisRound){ 
     case 3: 
     console.log("Ching!! 3 matches"); 
     //console.log(amountOfThreeMatches); 
     match3 = match3 + 1; 
     totalWinnings = totalWinnings + 10; 
     break; 
     case 4: 
     console.log("Ching!! 4 matches"); 
     match4 = match4 + 1; 
     totalWinnings = totalWinnings + 70; 
     break; 
     case 5: 
     console.log("Ching!! 5 matches"); 
     match5 = match5 + 1; 
     totalWinnings = totalWinnings + 60000; 
     break; 
     case 6: 
     console.log("Ching!! 6 matches"); 
     match6 = match6 + 1; 
     totalWinnings = totalWinnings + 1000000; 
     break; 

     default: 
     console.log("Alas only 2 or less matches"); 
     break; 
      }; 

        }; 

var clearCount = function(matched){ 
matchedNumbers = 0; //clears amount of matched numbers per round 
}; 

var cleartempTotal = function(matched){ 
totalMatchedThisRound = 0; //clears amount of matched numbers per round 
}; 

var start = function(){ 
startLottery = true; 

}; 

var stop = function(){ 
startLottery = false; 

}; 


function chooseNumbers() {//---chosen numbers check for doubles 
    var checkForDouble ; 
    var pickedNumbersArray= []; 

    for(var t = 1; t < 7; t++){ 
     checkForDouble = parseInt(document.getElementById("picked_number_0" + t).value); 
         document.getElementById("confirm_0" + t).value = ("You have picked"+" " +document.getElementById("picked_number_0" + t).value); 
         document.getElementById("confirm_0" + t).style.backgroundColor="green"; 

     for(var s = 0; s < 6; s++){ 
       if(checkForDouble == pickedNumbersArray[s]){ 
        document.getElementById("confirm_0" + t).value = ("Already chosen"); 
        document.getElementById("confirm_0" + t).style.backgroundColor="red" 
       } 
     } 
    pickedNumbersArray.push(checkForDouble); 
    } 
    }; 


var drawLotteryNumbers = function() { //generate numbers ensure unique 
var compareArray= []; 
var rnd =0; 

for(var i = 1; i < 7; i++){ 
      rnd = Math.floor((Math.random()*10)+1); 
    while(rnd === compareArray[0]||rnd === compareArray[1]||rnd === compareArray[2]||rnd === compareArray[3]||rnd === compareArray[4]||rnd === compareArray[5]){ 
       rnd = Math.floor((Math.random()*10)+1); 
      }; 
      compareArray.push(rnd); 
      document.getElementById("random_0" + i).value = rnd; 
        }; 
       };    

function automateLottery(){// generate - compare - stats 
numberOfWeeks = numberOfWeeks + 1;//tally up rounds 
drawLotteryNumbers(); 

var pickedNumber; 
    for(var e = 1; e < 7; e++){ 
     document.getElementById("compare_0" + e).style.backgroundColor="white"; 
     document.getElementById("compare_0" + e).value=("No matches"); 
     pickedNumber = parseInt(document.getElementById("picked_number_0" + e).value); 

     for(var i = 1; i < 7; i++){ 
      if(pickedNumber == document.getElementById("random_0" + i).value){ 
       document.getElementById("compare_0" + e).style.backgroundColor="green"; 
       document.getElementById("compare_0" + e).value=("You have matched number" + " "+ pickedNumber); 
       addUpMatches(1); 
      } 
     } 
    } 

    declareMatches();//----Add up per round and total 
    document.getElementById("amountOfMatched").value = ("You have matched" + " " +totalMatchedThisRound + " "+ "numbers"); 
    document.getElementById("weeks").value = ("You have played for" + " " +numberOfWeeks + " "+ "weeks"); 
    document.getElementById("moneyWon").value = ("You have won in total £"+ totalWinnings); 
    document.getElementById("amountOfThreeMatches").value = ("You have so far had" + " " +match3 + " "+ "matches of three" ); 
    document.getElementById("amountOffourMatches").value = ("You have so far had" + " " +match4 + " "+ "matches of four" ); 
    document.getElementById("amountOffiveMatches").value = ("You have so far had" + " " +match5 + " "+ "matches of five" ); 
    document.getElementById("amountOfsixMatches").value = ("You have so far had" + " " +match6 + " "+ "matches of six" ); 

    clearCount();//clears amount matched every round 
    cleartempTotal();//clears total matched every round 
    }; 


while(startLottery === true){ 
automateLottery(); 
    }; 
+0

這是很多代碼 - 你應該把它縮小到與當前問題相關的代碼你在問。 – jbabey

+0

這是很多代碼,*和*你省略了相關的HTML。也許你可以在http://jsfiddle.net上放置一個簡化的完整版本? – apsillers

+0

它現在似乎在jsFiddle上工作。 – diolemo

回答

0

這意味着正是它告訴你。沒有元素的id爲「random_0」+我在這種情況下將是「random_01」到「random_06」

+0

through random_06:P – jbabey

+0

噓。我還沒有喝過咖啡。 :P – sgrif

+0

雖然有。隨機1-6是我隨機生成的彩票號碼。 – UllerUller

相關問題