简体   繁体   English

看看string是否开始匹配另一个字符串

[英]see if string is starting to match another string

I have a list of keywords in an array and when the user types one of them correctly, I run the pass() function. 我有一个数组中的关键字列表,当用户正确键入其中一个关键字时,我运行pass()函数。 I check if what the user has typed after each key has been typed ( on key up ) is equal to anything in the array. 我检查用户在键入每个键后键入的内容(在键上)是否等于数组中的任何内容。

I do this by on each key up, use a for loop to iterate through the array and if it matches at item in the array, then run the pass function, but if it is longer or equal to in length and not equal to an item in the array and is the end of the array has been reached then run a fail function. 我在每个键上执行此操作,使用for循环遍历数组,如果它匹配数组中的item,则运行pass函数,但如果它长度或等于长度且不等于项目在数组中并且已经到达数组的末尾然后运行失败函数。

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

var keywords = new Array('', 'help', 'search', 'clock', 'folio', 'pong', 'simon');
for(var i=0; i<=(keywords.length)-1; i++){
if($('#code').val()===keywords[i]){
    pass(i);
}
else if(($('#code').val().length>=keywords[i].length) && (i === (keywords.length)-1)){
    fail();
}
}

Now, the problem that I am encountering is premature fail() execution. 现在,我遇到的问题是过早的fail()执行。 When the user types one of the longer keywords, the moment it is longer than one of the shorter keywords fail is executed. 当用户键入较长的关键字之一时,执行比较短的关键字之一更长的时间。

Also, for some research on typing 'help' and 'search' both pass and fail run ? 另外,对于一些关于打字“帮助”和“搜索”的研究,传递和失败都是运行?

Anyway, what I am wondering is there is there a way to improve my code to better so what I am trying to do and properly work? 无论如何,我想知道是否有一种方法可以更好地改进我的代码,以便我正在尝试做什么并正常工作?

Is it possible to check if what the user is typing is matching one of the keywords and if it looks like they are spelling one of they keywords then not fail(), but the moment they mess up and spell something that is equal to length, but not the keyword then fail(). 是否有可能检查用户输入的内容是否与其中一个关键字匹配,如果看起来他们正在拼写其中一个关键字然后没有失败(),但是当他们陷入困境并拼写等于长度的东西时,但不是关键字然后失败()。

For example: sear would cause a fail in my previous code, but in what I am now trying to do it wouldn't until the user types something like searck. 例如:sear会导致我之前的代码失败,但是在我现在尝试做的事情中,直到用户输入类似searck的内容。

You need to only call fail() when the for loop is done and nothing matched. 只需在for循环完成且没有匹配时调用fail()

I can't tell what you were trying to accomplish with the else if logic. 我无法告诉你, else if使用else if逻辑,你要用什么来完成。 Here's a version that calls fail() only if there were no matches. 这是一个只有在没有匹配时调用fail()的版本。 It also takes the $('#code').val() out of the for loop for efficiency sakeand breaks out of the for loop when it finds a match since there's no reason to continue. 为了提高效率,它还会从for循环中取出$('#code').val() ,并在找到匹配时突破for循环,因为没有理由继续。

var keywords = new Array('', 'help', 'search', 'clock', 'folio', 'pong', 'simon');
var val = $('#code').val();
var fullMatch = false, partialMatch = false;

for (var i = 0; i < keywords.length; i++) {
    if (val === keywords[i]) {
        fullMatch = true;
        pass(i);
        break;
    } else if (val && !partialMatch) {
        if (keywords[i].indexOf(val) === 0) {
           partialMatch = true;
        }
    } 
}
if (!fullMatch && !partialMatch) {
    fail();
}

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

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