2017-09-19 11 views
-1

私はHtml/Javscriptで単語ゲームをプログラミングしています。 あなたはその言葉を推測する必要があります。手紙が正しいとき、手紙は緑の背景を得る(これは働く)。 しかし、今、私がしたい文字が単語であるが、右の位置に、背景が黄色になりますされていない場合という(これは問題です)入力が配列内にあるかどうかを確認する方法

for (i = 0; i < invoer.length; i++) { 
    if(rand[i] == invoer[i].value.toUpperCase()) {       
    if(countClick == 1) { 
     // Here the background will become green when its correct 
     invoer[i].value = rand[i]; 
     var invoer1 = document.hetForm.L01; 
     invoer1[i].value = rand[i]; 
     invoer[i].style.background = "green"; 
     invoer1[i].style.background = "green";      
    } 

    if(countClick == 2) { 
     invoer[i].value = rand[i]; 
     var invoer2 = document.hetForm.L02; 
     invoer2[i].value = rand[i]; 
     invoer[i].style.background = "green"; 
     invoer2[i].style.background = "green"; 
    } 

    if(countClick == 3) { 
     invoer[i].value = rand[i]; 
     var invoer3 = document.hetForm.L03; 
     invoer3[i].value = rand[i]; 
     invoer[i].style.background = "green"; 
     invoer3[i].style.background = "green"; 
    } 

    if(countClick == 4) { 
     invoer[i].value = rand[i]; 
     var invoer4 = document.hetForm.L04; 
     invoer4[i].value = rand[i]; 
     invoer[i].style.background = "green"; 
     invoer4[i].style.background = "green"; 
    } 

    if(countClick == 5) { 
     invoer[i].value = rand[i]; 
     invoer[i].style.background = "green"; 
    } 

    // DOE EEN GROENE ACHTERGROND ALS DE LETTER JUIST IS. 
    invoer[i].style.background = "green"; 
    goed++; 
    } else { 
    if(rand.indexOf(invoer[i].value)) { 
     invoer[i].style.background = "yellow"; 
    } else { 
     alert(invoer[i].value); 
    } 
    } 
} 

invoerは私の入力の名前です。 randは私の配列のランダムな値です。

コードは、正しい位置にないか正しくない文字を黄色に変えます。

+0

'if(rand.indexOf(invoer [i] .value)> -1)'? '-1'が偽でなければならないにもかかわらず 'if(-1){alert( 'true')}' ' 0'が真でなければならないので、どちらの場合でも 'if(true)'だけでなく '-1'と照合してください。 – Nope

+0

@Franまだ同じフォールトはありません –

+2

あなたの質問タイトルはあなたの実際の質問をすべて説明しているとは思わない... – wally

答えて

0

あなたのコードはうまくいくようですが、問題はあなたが行っているテストが間違っているようです。 indexOfは、配列内で見つかったインデックス、または-1を返します。インデックスは0から始まり、そしてあなたがtruthy値をテストしている文字が、配列の最初のインデックスに発見された場合を除き、これは常に正しくなりますので...これであなたの場合を修正:

if(rand.indexOf(invoer[i].value.toUpperCase())>-1) 

また、あなたの上のIFステートメントでやったように、大文字小文字の区別がない場合は.toUpperCase()も使用してください。あなたのコードの代わりにあなたのコードがそれを示していないが、私はランドは首都が移入されたと推定...

1

var rand = ["A", "B", "C", "D"]; 
var input = "BEND"; 

for (var index = 0; index < input.length; index++) { 

    var colorCode = getColorCodeForInputLetter(index, input[index]); 
    console.log(input[index], "=>", colorCode); 

} 

function getColorCodeForInputLetter(index, letter) { 

    // Check if the letter is in the rand Array 
    if (rand.indexOf(letter) !== -1) { 

     // Check if the index of the letter is equals to the index of the letter in the array 
     if (rand.indexOf(letter) === index) { 
      console.log(letter, "correct"); 
      return "green"; 
     } else { 
      console.log(letter, "in rand"); 
      return "yellow"; 
     } 

    } else { 
     console.log(letter, "not in rand"); 
     return "red"; 
    } 

} 

私はノードと出力にランドにこのをしましたです。

B in rand 
E not in rand 
N not in rand 
D correct 
関連する問題