javascript - 为什么 ToSimplifyText 保持不变?

标签 javascript

我尝试创建一个程序,它可以从您使用提示输入的一些文本中删除 4000 个最常用的单词。这是我用一个循环完成的,它一个一个地删除你在列表中找到的单词,直到索引到达列表的末尾并且你已经删除了所有 4000 个(见下面的代码)。问题是什么都没有被删除,我的输出与输入相同。我的输入是文本“The Chungus”,“The”是列表中的第一个词。

我检查了每个函数和数据类型是否正确,事实上,如果我删除循环并用数字替换 CurrentSpaceIndex 和 CurrentWordStart,那么输出是正确的:“Chungus”。

var x = "The be and of a in to have to ... "; //It's a list of the 4000 most used words in english. 4000 words with a space between them. The length of the string is 29307 characters, so the last character will have index 29306..

var ToSimplifyText = prompt("Please enter the text you wish to simplify:", "");

var WordToRemove;

var CurrentSpaceIndex = 0;

var CurrentWordStart = 0;

var CurrentChar;


while(CurrentSpaceIndex < 29307){
    CurrentChar = x.charAt(CurrentSpaceIndex);
    if (CurrentChar = " "){
    WordToRemove = x.substring(CurrentWordStart, CurrentSpaceIndex);
    ToSimplifyText = ToSimplifyText.replace(WordToRemove, "");
    CurrentWordStart = CurrentSpaceIndex + 1;
    }
    CurrentSpaceIndex = CurrentSpaceIndex + 1;
}
alert(ToSimplifyText);

我希望输出是 Chungus 而不是不变的 The Chungus

最佳答案

你做得很辛苦。您可以按照以下步骤进行操作:

  • 使用 split()
  • 将两个字符串转换为单词数组
  • 然后 filter() 来自用户输入的单词,它存在于 words 数组中。
  • 最后使用join()并返回结果。

var x = "The be and of a in to have to"; //It's a list of the 4000 most used words in english. 4000 words with a space between them. The length of the string is 29307 characters, so the last character will have index 29306..
var ToSimplifyText = prompt("Please enter the text you wish to simplify:", "");

const removeWords = (str,words) => str.split(' ').filter(x => !words.includes(x)).join(' ');

ToSimplifyText = removeWords(ToSimplifyText,x.split(' '))

alert(ToSimplifyText);

为什么你的代码不起作用?

原因是使用赋值运算符而不是比较运算符。

if (CurrentChar = " ")

应该是

if (CurrentChar === " ")

var x = "The be and of a in to have to"; //It's a list of the 4000 most used words in english. 4000 words with a space between them. The length of the string is 29307 characters, so the last character will have index 29306..

var ToSimplifyText = prompt("Please enter the text you wish to simplify:", "");

var WordToRemove;

var CurrentSpaceIndex = 0;

var CurrentWordStart = 0;

var CurrentChar;


while(CurrentSpaceIndex < x.length){
    CurrentChar = x.charAt(CurrentSpaceIndex);
    if (CurrentChar === " "){
    WordToRemove = x.substring(CurrentWordStart, CurrentSpaceIndex);
    console.log(WordToRemove)
    ToSimplifyText = ToSimplifyText.replace(WordToRemove, "");
    CurrentWordStart = CurrentSpaceIndex + 1;
    }
    CurrentSpaceIndex = CurrentSpaceIndex + 1;
}
alert(ToSimplifyText);

关于javascript - 为什么 ToSimplifyText 保持不变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55691117/

相关文章:

javascript - 如何在 node.js 中使用回调作为参数发出 api 请求?

javascript - 尝试从本地文件获取 JSON 数据时出现语法错误

javascript - 在 Vanilla javascript 中鼠标滚轮上的平滑垂直滚动?

javascript - 将字符串转换为 JSON

javascript - 悬停时具有透明背景的菜单后面的渐变背景

javascript - 在JS中从json文件接收信息

javascript - Node.js 中的 Hash_hmac 等效项

javascript - 如何根据ReactJS上的选择来检索Material-UI <TableRow/>的信息?

javascript - Onkeydown javascript 数学

javascript - 使用 Jasmine spies 测试函数的调用