Вот, пожалуйста.Надеюсь, это поможет вам.:)
var rightWords = ["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"
];
// Get elements
var form = document.getElementById("searchFormId");
var foundList = document.getElementById("foundWordsList");
var unfoundList = document.getElementById("unfoundWordsList");
// Variable to hold user entry
var userEntry;
// Found/unfound word arrays
var foundWords = [];
var unfoundWords = [];
function assignment62_2Part1() {
// Check if word was found
var valueFound = false;
// Reset output
foundList.innerHTML = "";
unfoundList.innerHTML = "";
// Get user response
userEntry = entry.value;
// Search and compare
var i;
for (i = 0; i < rightWords.length; i++) {
if (userEntry === rightWords[i]) {
valueFound = true;
break;
}
}
if (valueFound) {
foundWords.push(userEntry);
} else {
unfoundWords.push(userEntry);
}
// Output
for (i = 0; i < foundWords.length; i++) {
foundList.innerHTML += "<li>" + foundWords[i] + "</li>";
}
for (i = 0; i < unfoundWords.length; i++) {
unfoundList.innerHTML += "<li>" + unfoundWords[i] + "</li>";
}
return false;
}
<label class="formLabel" for="entry">Entry: </label>
<input id="entry" name="entry">
<input type="button" name="runExample" value="Enter" class="formatSubmit" onclick="assignment62_2Part1();" />
<!-- Ordered list of found words -->
<h4>Found Words</h4>
<ol id="foundWordsList"></ol>
<!-- Ordered list of unfound words -->
<h4>Unfound Words</h4>
<ol id="unfoundWordsList"></ol>
Вот более продвинутая и простая версия, которая поможет вам улучшить ваш код в будущем:)
var rightWords = ["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 unfoundWords = [];
btn.onclick = ()=>{
(rightWords.indexOf(entry.value) > 0 ? foundWords : unfoundWords)
.push("<li>" + entry.value + "</li>");
foundWordsList.innerHTML = foundWords.join('');
unfoundWordsList.innerHTML = unfoundWords.join('');
}
<label class="formLabel" for="entry">Entry: </label>
<input id="entry" name="entry">
<input type="button" id='btn' name="runExample" value="Enter" class="formatSubmit" />
<!-- Ordered list of found words -->
<h4>Found Words</h4>
<ol id="foundWordsList"></ol>
<!-- Ordered list of unfound words -->
<h4>Unfound Words</h4>
<ol id="unfoundWordsList"></ol>