简体   繁体   English

为什么ToSimplifyText保持不变?

[英]Why does the ToSimplifyText remain unchanged?

I tried to create a program which would remove the 4000 most used words from some text you put in using a prompt. 我试图创建一个程序,它会删除你使用提示输入的一些文本中最常用的4000个单词。 This I did with a loop which removes words you find on the list one by one until the index reaches the end of the list and you have removed all 4000 (see code below). 我做了一个循环,一个接一个地删除你在列表中找到的单词,直到索引到达列表的末尾,你已经删除了所有4000(见下面的代码)。 The problem is that nothing gets removed and my output is the same as input. 问题是没有任何东西被删除,我的输出与输入相同。 My input is the text "The Chungus" and "The" is the first word on the list. 我的输入是文本“Chungus”和“The”是列表中的第一个单词。

I have checked if every function and datatype is correct, and indeed if I remove the loop and substitute numbers for the CurrentSpaceIndex and CurrentWordStart, then the output is correct: "Chungus". 我检查了每个函数和数据类型是否正确,实际上如果我删除循环并替换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);

I expect the output to be Chungus instead of the unchanged The Chungus . 我希望输出是Chungus而不是The Chungus

You are doing it very hard way. 你这么做很辛苦。 You can do it in following steps: 您可以按以下步骤执行此操作:

  • Convert both strings to array of words using split() 使用split()将两个字符串转换为单词数组
  • Then filter() the words from the user input which is present in words array. 然后filter()来自用户输入的单词数组中的单词。
  • At last use join() and return the result. 最后使用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); 

Why Your code doesnot work? 为什么你的代码不起作用?

The reason is the use of Assigment Operator instead of comparison operator. 原因是使用Assigment Operator而不是比较运算符。

if (CurrentChar = " ")

should be 应该

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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM