不显示关键字存在?程序中出了什么问题?


<script>
var words=['abstract',  'arguments',    'await',    'boolean',
'break',    'byte',     'case',     'catch',
'char',     'class',    'const',    'continue',
'debugger',     'default',  'delete',   'do',
'double',   'else',     'enum',     'eval',
'expor',    'extends',  'false',    'final',
'finally',  'float',    'for',  'function',
'goto',     'if',   'implements',   'import',
'in',   'instanceof',   'int',  'interface',
'let',  'long',     'native',   'new',
'null',     'package',  'private',  'protected',
'public',   'return',   'short',    'static',
'super',    'switch',   'synchronized',     'this',
'throw',    'throws',   'transient',    'true',
'try',  'typeof',   'var',  'void',
'volatile',     'while',    'with',     'yield'];
var length=words.length;
var input=prompt("enter keywords")
for(i=0;i<length;i++)
{
//alert(words[i]);
if(input==words[i]){
alert("keyword exist");
}
else{
alert("not found");
//break;
break;
}
}
</script>

不显示关键字存在?程序中出了什么问题?

您的问题是,您根据列表中不匹配的第一个单词做出最终决定。您将看到,如果您输入关键字abstract,这将导致一条消息说明您找到了它,然后对列表中的第二个单词做出最终决定。

您需要做的是检查所有单词,如果找到,则记录和破坏,并且只有在没有匹配的情况下,您才会输出"未找到"消息。

这样的事情应该就足够了:

var length = words.length;
var input = prompt("Enter word to check")
var foundIt = false;             // Assume not found.
for (i = 0; i < length; i++) {   // Check EVERY word.
if (input == words[i]) {     // If match,
alert("Found it");       //   log and exit loop.
foundIt = true;
break;
}                            // Otherwise check other words.
}
// If NO word in list matched, log the fact.
if (! foundIt) {
alert("Not found");
}

您可以将其更改为如下所示的内容:

var words=[
'abstract', 'arguments',    'await',    'boolean',
'break',    'byte',         'case',     'catch',
'char',     'class',        'const',    'continue',
'debugger', 'default',      'delete',   'do',
'double',   'else',         'enum',     'eval',
'expor',    'extends',      'false',    'final',
'finally',  'float',        'for',      'function',
'goto',     'implements',   'if',       'import',
'in',       'instanceof',   'int',      'interface',
'let',      'long',         'native',   'new',
'null',     'package',      'private',  'protected',
'public',   'return',       'short',    'static',
'super',    'synchronized', 'switch',   'this',
'throw',    'throws',       'true',     'transient',
'try',      'typeof',       'var',      'void',
'volatile', 'while',        'with',     'yield'];
var input="try";
var idx = words.indexOf(input);
console.log("Keyword [" + input + "] " + (idx == -1 ? "not " : "") + "found");

您不是在检查整个数组列表,而只是在验证第一个数组项。

请找到以下具有更新逻辑的小提琴。

var words = ['abstract', 'arguments', 'await', 'boolean',
'break', 'byte', 'case', 'catch',
'char', 'class', 'const', 'continue',
'debugger', 'default', 'delete', 'do',
'double', 'else', 'enum', 'eval',
'expor', 'extends', 'false', 'final',
'finally', 'float', 'for', 'function',
'goto', 'if', 'implements', 'import',
'in', 'instanceof', 'int', 'interface',
'let', 'long', 'native', 'new',
'null', 'package', 'private', 'protected',
'public', 'return', 'short', 'static',
'super', 'switch', 'synchronized', 'this',
'throw', 'throws', 'transient', 'true',
'try', 'typeof', 'var', 'void',
'volatile', 'while', 'with', 'yield'
];
var length = words.length,
input = prompt("enter keywords"),
found = false;
console.clear();
for (i = 0; i < length; i++) {
if (input == words[i]) {
found = true;
console.log("Found");
break;
}
}
if (!found) {
console.log("Not Found");
}
var flag = false;
var words=['abstract', 'arguments', 'await', 'boolean', 'break', 'byte', 
'case', 'catch', 'char', 'class', 'const', 'continue', 'debugger', 'default', 
'delete', 'do', 'double', 'else', 'enum', 'eval', 'expor', 'extends', 'false', 
'final', 'finally', 'float', 'for', 'function', 'goto', 'if', 'implements', 'import', 
'in', 'instanceof', 'int', 'interface', 'let', 'long', 'native', 'new', 'null', 
'package', 'private', 'protected', 'public', 'return', 'short', 'static', 'super', 
'switch', 'synchronized', 'this', 'throw', 'throws', 'transient', 'true', 'try', 
'typeof', 'var', 'void', 'volatile', 'while', 'with', 'yield']; 
var len = words.length;
var inp = prompt ("enter keywords");
for (var i=0;i<len;i++){
alert(words[i]);
if (inp == words[i]){
alert ("keyword found");
flag = true;
break;
}
}
if (!flag)
alert ("not found");

}

尝试运行上述脚本。

最新更新