简体   繁体   English

如何检查此代码中的字符串中是否包含猜测的字符?

[英]How do I check if a guessed character is in a string in this code?

What I'm trying to do is if the user guesses the correct letter in the correct spot, it will output the letter in green. 我要尝试做的是,如果用户在正确的位置猜到了正确的字母,它将以绿色输出该字母。 If the user guesses the letter that's in the answer, but that's in the wrong spot, it will output in red. 如果用户猜出答案中的字母,但是在错误的位置,它将以红色输出。 And if the user guesses a letter that's not in the answer it will output in black. 而且,如果用户猜测答案中没有的字母,则会以黑色输出。 I'm struggling to figure out how to make it so that it will check if the letter is in the code but not the right place. 我正在努力弄清楚如何制作它,以便它可以检查字母是否在代码中,但不在正确的位置。

<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>

Have you looked into the string.indexOf() method? 您是否研究过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
}

 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> 

ok, so i hope this change in code will solve the problem... 好的,所以我希望代码中的更改能够解决问题...

 <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> 

best luck for you! 祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查字符串中的字符是否是 JavaScript 中的特定字符? - How do I check whether a character in a string is a specific character in JavaScript? 如何使用javascript打印在子手游戏中已经猜到的字母? - How do I print the letters already guessed in the hangman game with javascript? 如何检查字符串中的最后一个字符? - How can I check the last character in a string? 如何检查字符串是否包含至少一个数字,字母和既不是数字也不是字母的字符? - How do I check if a string contains at least one number, letter, and character that is neither a number or letter? 使用JQuery,如何检查字符串是字母字符,然后是八位数字? - Using JQuery, how do I check that a string is a letter character followed by eight digits? 如何检查字符串是否仅由相同长度的字符组组成? - How do I check if a string is made up exclusively of same-length character groups? 如何匹配字符开头没有的字符串? - How do I match a string that is not preceeded by a character? 如何检查字符串是否等于unicode字符? - How do you check if string is equal to unicode character? 在特定字符之后,如何匹配字符串中的第一个字符? - How do I match the first character in string, AFTER a certain character? 如何检查字符串的字符是否已经是数组的元素? - How can I check if a character of a string is already a element of an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM