gpt4 book ai didi

javascript - 搜索数组并制作找到和未找到单词的编号列表,Javascript

转载 作者:行者123 更新时间:2023-12-03 10:12:45 25 4
gpt4 key购买 nike

编写一个应用程序,在数组中搜索单词,并创建一个包含找到的单词的新数组和一个包含未找到的搜索单词的数组。每次输入新单词时,应用程序都会显示找到和未找到的单词的新数组。

提供的数组在我的代码中。我正在让列表正常工作,但我得到的单词都是“未定义”。 (我在 HTML 中设置了表单)有关我缺少什么以获得“未定义”结果的提示?谢谢!

//Global variable for array
var wordsCollection = ["JavaScript", "was", "developed", "by", "Brendan", "Eich", "at", "Netscape", "as",
"the", "in-page", "scripting", "language", "for", "Navigator", "2.", "It", "is", "a",
"remarkably", "expressive", "dynamic", "programming", "language.", "Because",
"of", "its", "linkage", "to", "web", "browsers", "it", "instantly", "became",
"massively", "popular.", "It", "never", "got", "a", "trial", "period", "in", "which",
"it", "could", "be", "corrected", "and", "polished", "based", "on", "actual", "use.",
"The", "language", "is", "powerful", "and", "flawed."];

function exercise11Part1()
{
var output;
var exercises8form;
var searchWord;
var index = "";
var valueFound = false;
var foundWords = [];
var notFoundWords = [];
var foundWordsList;
var notFoundWordsList;

exercises8form = document.getElementById("exercises8formId");

searchWord = exercises8form.wordSearch.value;

foundWordsList = document.getElementById("foundWordsListId");
notFoundWordsList = document.getElementById("notFoundWordsListId");

for (index = 0; index < wordsCollection.length; index++)
{
if (searchWord === wordsCollection[index])
{
valueFound = true;
break;
}
}

if (valueFound)
{
foundWords.push(wordsCollection[index]);
foundWordsList.innerHTML += "<li>" + foundWords[index] + "</li>";
}
else
{
notFoundWords.push(wordsCollection[index]);
notFoundWordsList.innerHTML += "<li>" + notFoundWords[index] + "</li>";
}

return false;
}

我仍然得到......不正确的输出。我错过了什么?

//Global variable for array
var wordsCollection = ["JavaScript", "was", "developed", "by", "Brendan", "Eich", "at", "Netscape", "as",
"the", "in-page", "scripting", "language", "for", "Navigator", "2.", "It", "is", "a",
"remarkably", "expressive", "dynamic", "programming", "language.", "Because",
"of", "its", "linkage", "to", "web", "browsers", "it", "instantly", "became",
"massively", "popular.", "It", "never", "got", "a", "trial", "period", "in", "which",
"it", "could", "be", "corrected", "and", "polished", "based", "on", "actual", "use.",
"The", "language", "is", "powerful", "and", "flawed."];

var foundWords = [];
var notFoundWords = [];

function exercise11Part1()
{
var exercises8form;
var searchWord;
var index = "";
var valueFound = false;

var foundWordsList;
var notFoundWordsList;

exercises8form = document.getElementById("exercises8formId");
searchWord = exercises8form.enteredString.value;

foundWordsListOutput = document.getElementById("foundWordsListId");
notFoundWordsListOutput = document.getElementById("notFoundWordsListId");

for (index = 0; index < wordsCollection.length; index++)
{
if (searchWord === wordsCollection[index])
{
foundWords.push(searchWord);
foundWordsList += "<li>" + foundWords[index] + "</li>";
}
else
{
notFoundWords.push(searchWord);
notFoundWordsList += "<li>" + notFoundWords[index] + "</li>";
}
}

foundWordsListOutput.innerHTML = foundWordsList;
notFoundWordsListOutput.innerHTML = notFoundWordsList;

return false;
}

最佳答案

这就是我要做的:

var foundWords = [],    // Don't declare these in the function,
notFoundWords = []; // That resets them each time you call the function.

function exercise11Part1() {
var exercises8form = document.getElementById("exercises8formId"),
searchWord = exercises8form.wordSearch.value,
foundWordsList = document.getElementById("foundWordsListId"),
notFoundWordsList = document.getElementById("notFoundWordsListId");

var index = wordsCollection.indexOf(searchWord);
if (index !== -1) {
foundWords.push(wordsCollection[index]);
foundWordsList.innerHTML += "<li>" + foundWords[index] + "</li>";
} else {
notFoundWords.push(wordsCollection[index]);
notFoundWordsList.innerHTML += "<li>" + notFoundWords[index] + "</li>";
}

return false;
}
如果在数组中找不到 searchWord,则

indexOf 返回 -1;如果在数组中找到该单词的索引,则返回 找到

关于javascript - 搜索数组并制作找到和未找到单词的编号列表,Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30032562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com