2013-10-19 80 views
0

嘿,我試圖做一個數組,它會讀取所有的內容,然後檢查與用戶提供的內容,但它是如此的發生,當我選擇「ATI Radeon HD 2600概述」它的工作原理但是當我選擇「ATI的Radeon X1950系列」或其任何內容後,它並不能工作,只是掛了整個系統這裏是代碼Array無法讀取

function setValue() { 
    myVariable = document.forms["myform"]["gname"].value; 
    myVariable2 = document.forms["myform"]["gpc"].value; 
    myVariable3 = document.forms["myform"]["procesor"].value; 
    myVariable4 = document.forms["myform"]["ram"].value; 
    myVariable5 = document.forms["myform"]["os"].value; 
    var gname = ["Prince of Persia: The Forgotten Sands", "Gears of War", "yes"]; 
    var gpc = ["Radeon HD 2600 Pro", "GeForce 8600 GTS 512MB", "ATI Radeon 5000 Series", "ATI Radeon 4000 Series", "ATI Radeon 3000 Series", "ATI Radeon HD 2900 Overview", "ATI Radeon HD 2600 Overview", "ATI Radeon X1950 Series", "ATI Radeon X1800 Series", "ATI Radeon X1650 Series"]; 
    var procesor = ["Core 2 Duo E4500 2.2GHz", "Athlon 64 X2 Dual Core 4400+", "yes"]; 
    var ram = ["3 GB", "2 GB", "1 GB"]; 
    var os = ["Windows Xp", "Windows 7", "Windows Vista", "Windows 8"]; 
    var canRun = false; 
    for (i = 0; i < gname.length; i++) 
    if (myVariable === gname[i]) //changed from 0 to i here 
    { 
     for (i = 0; i < gpc.length; i++) { 
      if (myVariable2 === gpc[i]) //changed from 0 to i here 
      { 
       for (i = 0; i < procesor.length; i++) { 
        if (myVariable3 === procesor[i]) //changed from 0 to i here 
        { 
         for (i = 0; i < ram.length; i++) { 
          if (myVariable4 === ram[i]) //changed from 0 to i here 
          { 
           for (i = 0; i < os.length; i++) { 
            if (myVariable5 === os[i]) //changed from 0 to i here 
            { 
             //but when my input is in the array it can run 
             canRun = true; 
            } 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 

    if (canRun) { 
     alert("yes this game can run"); 
    } else { 
     alert("No, This game cannot run"); 
    } 
}; 
+0

縮進怎麼辦?這是來自你的編輯器還是錯誤的複製/粘貼? – elclanrs

+0

哪個標識 – user2826868

+2

確切:(:(你說) – mplungjan

回答

3

這是因爲循環變量i的,在每個for循環您需要使用不同的循環變量,如i, j, k, l等...類似於

function setValue() { 
    myVariable = document.forms["myform"]["gname"].value; 
    myVariable2 = document.forms["myform"]["gpc"].value; 
    myVariable3 = document.forms["myform"]["procesor"].value; 
    myVariable4 = document.forms["myform"]["ram"].value; 
    myVariable5 = document.forms["myform"]["os"].value; 
    var gname = ["Prince of Persia: The Forgotten Sands", "Gears of War", "yes"]; 
    var gpc = ["Radeon HD 2600 Pro", "GeForce 8600 GTS 512MB", "ATI Radeon 5000 Series", "ATI Radeon 4000 Series", "ATI Radeon 3000 Series", "ATI Radeon HD 2900 Overview", "ATI Radeon HD 2600 Overview", "ATI Radeon X1950 Series", "ATI Radeon X1800 Series", "ATI Radeon X1650 Series"]; 
    var procesor = ["Core 2 Duo E4500 2.2GHz", "Athlon 64 X2 Dual Core 4400+", "yes"]; 
    var ram = ["3 GB", "2 GB", "1 GB"]; 
    var os = ["Windows Xp", "Windows 7", "Windows Vista", "Windows 8"]; 
    var canRun = false; 
    for (i = 0; i < gname.length; i++) 
    if (myVariable === gname[i]) //changed from 0 to i here 
    { 
     for (j = 0; j < gpc.length; j++) { 
      if (myVariable2 === gpc[j]) //changed from 0 to i here 
      { 
       for (k = 0; k < procesor.length; k++) { 
        if (myVariable3 === procesor[k]) //changed from 0 to i here 
        { 
         for (l = 0; l < ram.length; l++) { 
          if (myVariable4 === ram[l]) //changed from 0 to i here 
          { 
           for (m = 0; m < os.length; m++) { 
            if (myVariable5 === os[m]) //changed from 0 to i here 
            { 
             //but when my input is in the array it can run 
             canRun = true; 
            } 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 

    if (canRun) { 
     alert("yes this game can run"); 
    } else { 
     alert("No, This game cannot run"); 
    } 
}; 

演示:Fiddle

+0

謝謝你的工作 – user2826868