我试图删除一个段落中的所有命题但是如果两个命题重复只有第一个从段落中删除,而不是第二个?
function checkForSameWords() {
var valueOfArray1 = textArea1.value.trim().split(" ");
var commons = ["a", "the", "of", "are", "is", "am", "with", "on", "at", "from", "to", "on", "must"];
removingProposition(valueOfArray1, commons);
}
function removingProposition(paragraph, proposition) {
for (var i = 0; i < paragraph.length; i++) {
for (var j = 0; j < preposition.length; j++) {
if (paragraph[i] === proposition[j]) {
var h = paragraph.splice(paragraph.indexOf(paragraph[i]), 1);
}
}
}
console.log(paragraph);
}
答案 0 :(得分:0)
在你的内部for循环中,你正在使用&#34; pr e 位置&#34;而不是&#34; pr o 位置&#34;。除此之外,代码似乎工作正常。
我使用随机句来测试函数,因为我不知道textarea1
的值是什么。
此外,&#34; pr e 位置&#34;是你要找的词,我在下面的代码中使用过它。
var exampleParagraph = "Once upon a time there was a princess who lived in a castle."
checkForSameWords(exampleParagraph);
function checkForSameWords() {
var valueOfArray1 = exampleParagraph.trim().split(" ");
var commons = ["a", "the", "of", "are", "is", "am", "with", "on", "at", "from", "to", "on", "must"];
removingPreposition(valueOfArray1, commons);
}
function removingPreposition(paragraph, preposition) {
for (var i = 0; i < paragraph.length; i++) {
for (var j = 0; j < preposition.length; j++) {
if (paragraph[i] === preposition[j]) {
var h = paragraph.splice(paragraph.indexOf(paragraph[i]), 1);
}
}
}
console.log(paragraph);
}
&#13;