商店信件的逻辑在正确的地方

时间:2017-03-27 13:22:30

标签: javascript

**我需要新的逻辑来将猜测的字母存储在正确的位置。一个叫做刽子手的游戏。因为现在如果新的正确猜测适用,前一个正确的字母将被覆盖.... **

function guessLetter() { 

var letter; 
var i;

var letterFound;
var correctLettersCount;
correctLettersCount=0;
letterBoxes = ""; 
letter = this.value;
for (i = 0; i < selectedWord.length; i++) {


    if (selectedWord.charAt(i) == letter) {   //if
        letterBoxes += "<span>" + letter + "</span >"; 


        } 
    else{   
        letterBoxes += "<span>*</span>"; 

    }
   document.getElementById("letterBoxes").innerHTML = letterBoxes; 

   }

}

1 个答案:

答案 0 :(得分:0)

您可以使用将存储*和找到的字母

的数组

在以下代码段中,此数组称为display

&#13;
&#13;
const wordToFind = "testing";
const wordDisplay = document.getElementById("word");

let display = Array.from('*'.repeat(wordToFind.length));
wordDisplay.innerHTML = display.join("");


function checkLetter(el){
  const letter = el.value.toLowerCase();
  for(let i=0,l=wordToFind.length;i<l;i++){
    if(wordToFind[i]===letter)
      display[i] = letter;
  }
  el.value = "";
  wordDisplay.innerHTML = display.join("");
}
&#13;
<input oninput="checkLetter(this)" />
<p id="word"></p>
&#13;
&#13;
&#13;