为什么不可能这样:让array [j] = array [j] + 1;

时间:2019-05-05 11:50:22

标签: javascript arrays

如果单词出现在数组中。创建变量,该变量存储单词在数组内发生的时间。当我运行它时,我得到错误的意外令牌。如果newStory [i]等于overusedWords [j],则创建一个变量overusedWords [j]并为其添加+1。

let story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey.  The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side.  An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson.  Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.';
let overusedWords = ['really', 'very', 'basically']
const newStory = story.split(" ");

//Here we find the number of times overusedWords are used specifically in story
for (let i = 0; i <= newStory.length; i++) {
  for(let j = 0; j <= overusedWords.length; j++){
	if(newStory[i] === overusedWords[j]){
	  let overusedWords[j] = overusedWords[j] + 1;
	};
  };
};

2 个答案:

答案 0 :(得分:3)

您不能创建名称包含[字符

的变量

您将变量用作应用程序中值的符号名称。变量的名称(称为标识符)符合某些规则。

JavaScript标识符必须以字母,下划线(_)或美元符号($)开头;后续字符也可以是数字(0-9)。因为JavaScript区分大小写,所以字母包括字符“ A”至“ Z”(大写)和字符“ a”至“ z”(小写)。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types

let overusedWords[j] = overusedWords[j] + 1;

您可以将j索引处的更新数组overusedWords[j] = overusedWords[j] + 1;更改为overusedWords

在您的代码中也应该只检查i < newStory.length而不是i <= newStory.length,这是循环数组以防止索引越界的一种模式。

答案 1 :(得分:2)

这是因为overusedWords数组中的每个元素都是一个字符串。 overusedWords[j] + 1只是字符串连接,不会做任何有用的事情来实现您的目标。

元素“每个”的迭代只会产生“ really11”。

请改为执行以下操作:

const story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey.  The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side.  An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson.  Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.';
const overusedWords = ['really', 'very', 'basically'];

const result = {};

const newStory = story.split(" ");

for (let i = 0; i < newStory.length; i++) {
  for(let j = 0; j < overusedWords.length; j++){
    if(newStory[i] === overusedWords[j]){
      if (result.hasOwnProperty(overusedWords[j])) {
        result[overusedWords[j]] = result[overusedWords[j]] + 1;
      } else {
        result[overusedWords[j]] = 1;
      }       
    };
  };
};

console.log(result);

基本上,我们创建一个对象,该对象存储每个关键字在该story中出现的次数。