简体   繁体   English

Javascript - for 循环没有正确过滤数组

[英]Javascript - for loop not filtering array correctly

I am making a hangman program where you choose a word and the program guesses which word it is one letter at a time.我正在制作一个刽子手程序,您可以在其中选择一个单词,然后该程序一次猜测哪个单词是一个字母。

It can correctly remove words containing the worng letters and guess optimally, but it can't see when correctly guessed letters are in the incorrect places, and deletes every word in the array.它可以正确删除包含磨损字母的单词并进行最佳猜测,但它无法看到正确猜测的字母何时出现在不正确的位置,并删除数组中的每个单词。

Here is my code:这是我的代码:

function wrongplace() {
    let newlistword = blankword.split(""); //blankword is the parts of the word the computer can see e.g. "_ _ _ _ a _ _"
    for (let i = 0; i < possible.length; i++) { //possible is the array containing all words that the computer thinks your word might be
        let templistword = possible[i].split("");
        for (let j = 0; j < templistword.length; j++) {
            if ((templistword[j] == newlistword[j]) || (newlistword[j] == "_" && (correct.includes(templistword[ 
                    j] == false)))) { //correct is the letters the computer has guessed correctly
                continue;
            }
            possible.splice(i, 1);
            i--;
            break;
        }
    }
}

What this is supposed to do is check every possible word, check every letter to either be the same as the guessed word, or the letter in the guessed word to be blank and the letter in the same position in the word it's checking to not be in the correctly guessed letters.这应该做的是检查每个可能的单词,检查每个字母是否与猜测的单词相同,或者猜测的单词中的字母为空,并且它检查的单词中相同位置的字母不是在正确猜测的字母中。

For example:例如:

With this input:有了这个输入:

var blankword = "____a__"

var possible = ['abandon', 'bombard', 'garland', 'volcano', 'volubly']

var correct = ["a"]

When I run console.log(possible);当我运行console.log(possible); , the array is empty when it should return ['bombard', 'volcano'] . ,当它应该返回['bombard', 'volcano']时,数组是空的。 Does anyone have any ideas on how to make it not delete everything?有没有人对如何使它不删除所有内容有任何想法?

Array.filter is a better way to solve the problem, here just to point out a little mistake in your code that made it not working: Array.filter是解决问题的更好方法,这里只是指出您的代码中的一个小错误导致它无法工作:

correct.includes(templistword[j] == false) // <----- here, you put `== false` in the parentheses, 
                                           //          which should be after it

correct this then your code will work fine.纠正这一点,然后您的代码将正常工作。

var blankword = "____a__"

var possible = ['abandon', 'bombard', 'garland', 'volcano', 'volubly']

var correct = ["a"]


function wrongplace() {
    const res = []
    let newlistword = blankword.split(""); //blankword is the parts of the word the computer can see e.g. "_ _ _ _ a _ _"
    //  newlistword = ["_", "_", "_", "_", "a", "_", "_"]
    for (let i = 0; i < possible.length; i++) { //possible is the array containing all words that the computer thinks your word might be
        let templistword = possible[i].split("");
        // templistword = ["a", "b", "a", "n", "d", "o", "n"]
        let j
        for (j = 0; j < templistword.length; j++) {
            if ((templistword[j] == newlistword[j]) || (newlistword[j] == "_" && (!correct.includes(templistword[j])))) { //correct is the letters the computer has guessed correctly
                continue;
            }
            break;
        }
        if (j === templistword.length){
            res.push(possible[i])
        }
    }
    return res;
}

wrongplace()  // ["bombard", "volcano"]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM