2016-10-11 59 views
0

我想要做的是如果用戶在正確的位置猜測正確的字母,它會輸出綠色的字母。如果用戶猜到答案中的字母,但是出現在錯誤的地方,它將以紅色輸出。如果用戶猜到一個不在答案中的字母,它將以黑色輸出。我正在努力弄清楚如何使它能夠檢查信件是否在代碼中,但不是正確的地方。如何在此代碼中檢查猜測字符是否在字符串中?

<script> 
var secret = 'OPIMNC'; 
function init(){ 
    var button = document.getElementById('startButton'); 
    button.onclick = myButtonClick; 
} 
function myButtonClick(){ 
    var userTry = document.getElementById('userGuess').value; 
    var ul = document.getElementById('guessList'); 
    var li = document.createElement('li'); 
    for (var i=0; i < secret.length; i++) { 
     var found = false; 
     if ((userTry.charAt(i))===(secret.charAt(i))) { 
      li.innerHTML += userTry.charAt(i).fontcolor('green'); 
      found = true; 
     } 

     //if character is in the code but in the wrong place, output the font in red 
     else if ???? { 
      for (var j=0; j < secret.length; j++) { 
      ?????????????? 
      } 
      found = true; 
      li.innerHTML += userTry.charAt(i).fontcolor('red'); 


     else if (found===false) { 
      li.innerHTML += userTry.charAt(i).fontcolor('black'); 
     } 
    } 
    ul.appendChild(li); 
} 

window.onload = init; 
</script> 
+0

用戶是否輸入一個字或一個字母? – Weedoze

+0

他們輸入6個字母 – Lopez78

+0

Okok我正在創建一個片段 – Weedoze

回答

0

var secret = 'ABCDEF'; 
 

 
function init() { 
 
    var button = document.getElementById('startButton'); 
 
    button.onclick = myButtonClick; 
 
} 
 

 
function myButtonClick() { 
 
    var userTry = document.getElementById('userGuess').value; 
 
    if(userTry.length !== 6){ 
 
    document.getElementById('error').innerHTML = "I need 6 characters !"; 
 
    return; 
 
    } 
 
    document.getElementById('error').innerHTML = ""; 
 
    
 
    var ul = document.getElementById('guessList'); 
 
    ul.innerHTML = ""; //Clear list 
 
    
 
    for (var i = 0; i < secret.length; i++) { 
 
    var color; 
 
    if (userTry.charAt(i) === secret.charAt(i)) { 
 
     //Correct index 
 
     color = 'geen' 
 
    } else if (secret.indexOf(userTry.charAt(i)) !== -1) { 
 
     //Present but wrong index 
 
     color = 'red'; 
 
    } else { 
 
     //Not present 
 
     color = 'black'; 
 
    } 
 
    
 
    //Add letters to list with correct colors 
 
    var li = document.createElement('li'); 
 
    li.innerHTML = userTry.charAt(i).fontcolor(color); 
 
    ul.appendChild(li); 
 
    } 
 
} 
 
window.onload = init;
<input type="text" id="userGuess" maxlength="6"/> 
 
<button type="submit" id="startButton">Start</button> 
 

 
<p style="color: red;" id="error"></p> 
 

 
<ul id="guessList"> 
 
    
 
</ul>

+0

沒問題:D對於這項工作來說有點兒樂觀嗎? – Weedoze

+0

是的,我只是說它不顯示,因爲我有不到15的聲譽 – Lopez78

+0

好的沒問題的隊友!用你的代碼玩得開心 – Weedoze

1

你看過string.indexOf()方法嗎?

if(secret.indexOf('[CHAR or STRING]') >= 0) { 
// Character or string is within secret string 
} else { 
// Character or string is NOT within secret string : index == -1 
} 
0

好了,我希望這種改變在代碼就能解決問題...

<script> 
 
var secret = 'ABCDEF'; 
 
function init(){ 
 
    var button = document.getElementById('startButton'); 
 
    button.onclick = myButtonClick; 
 
} 
 
function myButtonClick(){ 
 
    //alert('Button clicked'); 
 
    var userTry = document.getElementById('userGuess').value; 
 
    var ul = document.getElementById('guessList'); 
 
    var li = document.createElement('li'); 
 
    for (var i=0; i < secret.length; i++) { 
 
     var found = false; 
 
     if ((userTry.charAt(i))===(secret.charAt(i))) { 
 
      //if secret code and guess are a match, display letter in green 
 
      li.innerHTML += userTry.charAt(i).fontcolor('green'); 
 
      found = true; 
 
     } 
 

 
     //~~~~~~~~~~~~CHANGE HERE~~~~~~~~~~~~ 
 
     else { 
 
      for (var j=0; j < secret.length; j++) { 
 
       if ((userTry.charAt(i))===(secret.charAt(j))) { 
 
       found = true; 
 
       } 
 
      } 
 
      
 
      if (found===true) { 
 
       li.innerHTML += userTry.charAt(i).fontcolor('red'); 
 
      } 
 
      else { 
 
       //need to show user character is totally wrong 
 
       li.innerHTML += userTry.charAt(i).fontcolor('black'); 
 
      } 
 
     } 
 
     //~~~~~~~~~~~~CHANGE END~~~~~~~~~~~~ 
 
    } 
 
    ul.appendChild(li); 
 
    alert(userTry); 
 
} 
 

 
window.onload = init; 
 
</script>

好運!

+0

雙循環沒有必要 – Weedoze

+0

+你可以在第二個循環的'found = true'之後插入一個break – Weedoze

相關問題